Managing Linux Users and Groups | useradd usermod passwd
useradd, usermod, userdel, groupadd, passwd, sudo, /etc/passwd, /etc/shadow
Managing Linux Users and Groups
Linux is a multi-user operating system, and user and group management is a critical system administration task. Every process runs as a specific user, and permissions are enforced based on user identity and group membership. Understanding how to add, modify, and remove users and groups is fundamental to Linux basics.
User Account Files
Linux stores user information in /etc/passwd, which contains one line per user with fields for username, password placeholder, user ID (UID), group ID (GID), comment, home directory, and shell. Encrypted passwords are stored separately in /etc/shadow, which is readable only by root.
cat /etc/passwd | grep alice
cat /etc/shadow | grep alice
id aliceCreating Users with useradd
The useradd command creates a new user account. The -m flag creates the user's home directory, -s sets the login shell, -G adds the user to supplementary groups, and -c adds a comment (usually the user's full name).
sudo useradd -m -s /bin/bash -c "Alice Smith" alice
sudo useradd -m -G sudo,developers -s /bin/bash bob
sudo passwd aliceAfter creating a user, set their password with the passwd command. Without -m, the home directory is not created automatically on some distributions.
Modifying Users with usermod
The usermod command modifies an existing user account. Use -aG to append the user to a group without removing them from other groups. Omitting the -a flag would replace all group memberships.
sudo usermod -aG sudo alice
sudo usermod -aG docker alice
sudo usermod -s /bin/zsh alice
sudo usermod -l newname oldnameDeleting Users
The userdel command removes a user. The -r flag also removes the user's home directory and mail spool.
sudo userdel alice
sudo userdel -r bobManaging Groups
The groupadd command creates new groups. The gpasswd command manages group membership. The groups command shows which groups a user belongs to.
sudo groupadd developers
sudo gpasswd -a alice developers
groups alice
cat /etc/group | grep developerssudo and the sudoers File
The sudo command allows permitted users to run commands as root. Users in the sudo group (on Ubuntu/Debian) or wheel group (on CentOS) can use sudo. The sudoers configuration is at /etc/sudoers and should be edited with visudo to prevent syntax errors.
sudo apt update
sudo visudo