User and Group Management in Linux

A guide to Linux user and group management covering /etc/passwd and /etc/group file structures, and commands like useradd, usermod, groupadd, and passwd.

In Linux systems, the concepts of users and groups are used to manage access permissions to files and processes.

User Account Information

User account information is primarily stored in the /etc/passwd file. This file contains basic information for all user accounts on the system, with each line corresponding to one user.

Each line in /etc/passwd consists of 7 fields separated by colons :.

[Username]:[Password]:[User ID (UID)]:[Group ID (GID)]:[Comment]:[Home Directory]:[Login Shell]
  • Password: For security reasons, the actual password hash is not stored here directly. Instead, x is displayed, and the password hash is stored in the /etc/shadow file. The /etc/shadow file is accessible only by the root user in read-only mode.

User Information Editing Commands

  • useradd: Adds a new user account.
    sudo useradd -m -s /bin/bash newuser # Create home directory and set bash as shell
    sudo passwd newuser # Set password
    
  • usermod: Modifies an existing user account.
    sudo usermod -aG sudo newuser # Add newuser to the sudo group
    sudo usermod -l new_name old_name # Change username
    
  • userdel: Deletes a user account.
    sudo userdel -r olduser # Delete including home directory
    
  • passwd: Changes a user’s password.
    passwd # Change your own password
    sudo passwd username # Change another user's password (requires root privileges)
    

Group Account Information

Group account information is primarily stored in the /etc/group file. This file contains basic information for all groups on the system, with each line corresponding to one group.

Each line in /etc/group consists of 4 fields separated by colons :.

[Group Name]:[Password]:[Group ID (GID)]:[Members]
  • Password: Group passwords are rarely used. Typically, x is displayed.
  • Members: A comma-separated list of usernames belonging to the group.

Group Information Editing Commands

  • groupadd: Adds a new group.
    sudo groupadd newgroup
    
  • groupmod: Modifies an existing group.
    sudo groupmod -n new_group_name old_group_name # Change group name
    
  • groupdel: Deletes a group.
    sudo groupdel oldgroup
    
  • id: Checks the groups that the current user belongs to.
    id
    # Example: uid=1000(username) gid=1000(username) groups=1000(username),4(adm),27(sudo)
    

Understanding and properly using these commands enables effective user and group management in Linux systems.