What it is
A collection of commands for managing users and groups on a Linux system, useful for system administrators and anyone needing to control access to resources.
Installation
User management commands are typically built into the Linux operating system and do not require separate installation.
Core Concepts
- User: An entity that can log into the system and interact with resources. Each user has a unique username and a User ID (UID).
- Group: A collection of users. Users can belong to multiple groups, and group memberships determine permissions for shared resources. Each group has a unique Group ID (GID).
- Primary Group: The default group a user belongs to.
- Supplementary Groups: Additional groups a user is a member of.
- Home Directory: A personal directory for each user, typically located at
/home/<username>. - Shell: The command-line interpreter that a user interacts with upon login.
Commands / Usage
User Management
Creating Users:
sudo useradd johndoe- Creates a new user account named
johndoewith default settings.
- Creates a new user account named
sudo useradd -m -d /home/jane -s /bin/bash jane- Creates a new user
jane, creates their home directory/home/jane, and sets their default shell to/bin/bash.
- Creates a new user
sudo useradd -g users -G developers,testers -c "John Doe" jdoe- Creates user
jdoe. Sets their primary group tousers, adds them to supplementary groupsdevelopersandtesters, and sets the comment field to "John Doe".
- Creates user
sudo useradd -s /sbin/nologin serviceuser- Creates a user
serviceuserwith no login shell, often used for running services.
- Creates a user
Modifying Users:
sudo usermod -l janedoe jane- Renames the user
janetojanedoe.
- Renames the user
sudo usermod -d /home/newjane -m janedoe- Changes the home directory of
janedoeto/home/newjaneand moves existing files.
- Changes the home directory of
sudo usermod -s /bin/zsh janedoe- Changes the default shell for
janedoeto/bin/zsh.
- Changes the default shell for
sudo usermod -aG sudo janedoe- Adds
janedoeto thesudogroup without removing them from other supplementary groups.
- Adds
sudo usermod -G developers janedoe- Sets
janedoe’s supplementary groups to onlydevelopers. Existing supplementary groups are removed.
- Sets
sudo usermod -c "Jane Doe, Marketing" janedoe- Updates the comment (GECOS) field for
janedoe.
- Updates the comment (GECOS) field for
Deleting Users:
sudo userdel johndoe- Deletes the user account
johndoebut leaves their home directory and mail spool.
- Deletes the user account
sudo userdel -r johndoe- Deletes the user account
johndoeand also removes their home directory and mail spool.
- Deletes the user account
Viewing User Information:
id johndoe- Displays the UID, GID, and group memberships for the user
johndoe.
- Displays the UID, GID, and group memberships for the user
finger johndoe- Shows detailed information about the user
johndoe, including login name, real name, terminal, login time, etc. (may need to be installed:sudo apt install fingerorsudo yum install finger).
- Shows detailed information about the user
getent passwd johndoe- Retrieves the entry for
johndoefrom the password database (which includes user information).
- Retrieves the entry for
getent group developers- Retrieves the entry for the
developersgroup from the group database.
- Retrieves the entry for the
Password Management
Setting/Changing Passwords:
sudo passwd johndoe- Allows the current user (with
sudo) to change the password forjohndoe.
- Allows the current user (with
passwd- Allows the logged-in user to change their own password.
sudo passwd -d johndoe- Deletes the password for
johndoe, making the account accessible without a password (use with extreme caution).
- Deletes the password for
sudo passwd -l johndoe- Locks the password for
johndoe, preventing login.
- Locks the password for
sudo passwd -u johndoe- Unlocks the password for
johndoe.
- Unlocks the password for
Group Management
Creating Groups:
sudo groupadd developers- Creates a new group named
developers.
- Creates a new group named
sudo groupadd -g 1005 webteam- Creates a new group
webteamwith a specific GID of1005.
- Creates a new group
Modifying Groups:
sudo groupmod -n webteam webdevelopers- Renames the group
webteamtowebdevelopers.
- Renames the group
sudo groupmod -g 1006 webdevelopers- Changes the GID of the
webdevelopersgroup to1006.
- Changes the GID of the
Deleting Groups:
sudo groupdel developers- Deletes the group
developers. This will fail if any user has this group as their primary group.
- Deletes the group
Adding/Removing Users from Groups (Manual):
sudo gpasswd -a johndoe developers- Adds
johndoeto thedevelopersgroup.
- Adds
sudo gpasswd -d johndoe developers- Removes
johndoefrom thedevelopersgroup.
- Removes
Permissions and Ownership
Changing File/Directory Ownership:
sudo chown janedoe:developers /var/www/html- Changes the owner of
/var/www/htmltojanedoeand the group owner todevelopers.
- Changes the owner of
sudo chown -R wilson:wilson /home/wilson/projects- Recursively changes the owner and group of all files and directories within
/home/wilson/projectstowilson.
- Recursively changes the owner and group of all files and directories within
sudo chown janedoe: /var/www/html- Changes only the owner of
/var/www/htmltojanedoe, leaving the group unchanged.
- Changes only the owner of
Changing File/Directory Permissions:
sudo chmod 755 /usr/local/bin/myscript.sh- Sets permissions for
myscript.shto rwxr-xr-x (owner: read, write, execute; group: read, execute; others: read, execute).
- Sets permissions for
sudo chmod u+x /usr/local/bin/myscript.sh- Adds execute permission for the owner (
u+x) tomyscript.sh.
- Adds execute permission for the owner (
sudo chmod g-w /data/shared_file.txt- Removes write permission for the group (
g-w) fromshared_file.txt.
- Removes write permission for the group (
sudo chmod o=r /data/public_info.txt- Sets permissions for others (
o=) to only read (r) forpublic_info.txt.
- Sets permissions for others (
sudo chmod -R a+rX /srv/app/config- Recursively adds read permission for all (
a+r) to/srv/app/configand its contents.X(capital X) grants execute permission only if it’s a directory or if execute permission is already set for any user.
- Recursively adds read permission for all (
Common Patterns
-
Adding a user and immediately setting their password:
sudo useradd -m newuser sudo passwd newuser- Create the user with a home directory, then set their password interactively.
-
Creating a user for a web server with restricted access:
sudo useradd -r -s /sbin/nologin -d /var/www/myapp www-data sudo chown -R www-data:www-data /var/www/myapp sudo chmod -R 755 /var/www/myapp- Create a system user (
-r) with no login shell (-s /sbin/nologin) and a specific home directory. Then, set ownership and permissions for the web content.
- Create a system user (
-
Granting a user sudo privileges:
sudo usermod -aG sudo username- Appends the user to the
sudogroup, allowing them to run commands withsudo.
- Appends the user to the
-
Checking who is logged in and their groups:
who | awk '{print $1}' | sort | uniq | xargs -I {} id {}- Lists all unique logged-in users and then displays their
idinformation.
- Lists all unique logged-in users and then displays their
-
Finding all files owned by a specific user:
sudo find / -user olduser -print- Searches the entire filesystem for files owned by
olduser.
- Searches the entire filesystem for files owned by
-
Changing ownership of all files in a directory to a new user and group:
sudo chown -R newuser:newgroup /path/to/directory- Recursively changes ownership for all items within the specified directory.
Gotchas
useraddvsadduser:useraddis a low-level utility that creates the user account but might not set up a home directory or prompt for a password by default.adduser(on Debian/Ubuntu systems) is a higher-level script that is more interactive and user-friendly, often creating the home directory, setting up skeleton files, and prompting for user information and password.usermod -Goverwrites supplementary groups: When usingusermod -G, it replaces the user’s existing supplementary groups with the ones specified. Useusermod -aGto append groups.- Permissions after
userdel -r: Whileuserdel -rremoves the user’s home directory, files they owned in other locations might still exist and become orphaned (owned by a non-existent UID/GID). You might need to usefindandchownto reassign ownership. - UID/GID conflicts: Manually assigning UIDs or GIDs with
useradd -uorgroupadd -gcan lead to conflicts if the ID is already in use by another user or group, potentially causing permission issues. - Log out/in required: Changes to group memberships often require the user to log out and log back in for the new group permissions to take effect.
/etc/passwd,/etc/shadow,/etc/group: These files store user and group information. Direct editing is generally discouraged and risky; use the provided commands instead./etc/shadowcontains hashed passwords and is only readable by root.- System users: Users created with
useradd -r(or often with UIDs below 1000) are typically considered system users and may have different default permissions or no home directories.