Managing users in Linux is an essential aspect of system administration.
Linux provides a number of commands for creating, deleting, and modifying user accounts.
Below is a guide to some basic Linux user management commands that are commonly used:
Table of Contents
Key Commands for Linux user management
1. Viewing Current Users and Information
- List all users: To list all users in the system, you can view the contents of the
/etc/passwdfile:cat /etc/passwdEach line in this file represents a user.
- View information about a specific user: You can use the
idcommand to view user information such as UID, GID, and group memberships:id username - Show user login history: You can use the
lastcommand to show the login history of users:last
2. Adding Users
- Add a new user: To add a new user, you can use the
useraddcommand. This will create a new user account:sudo useradd username - Add a new user with a home directory and other options: You can specify options like the home directory, shell, and group with the
-m,-s, and-Gflags:sudo useradd -m -s /bin/bash -G groupname username-mcreates the home directory for the user.-s /bin/bashsets the shell for the user.-G groupnameadds the user to a group.
- Set a password for the user: After creating the user, you need to set a password:
sudo passwd username
3. Modifying User Accounts
- Change a user’s password: To change the password of a user, use the
passwdcommand:sudo passwd username - Modify user details (like shell, home directory): To modify a user’s details, use the
usermodcommand. Here are a few examples:- Change the user’s home directory:
sudo usermod -d /new/home/directory username - Change the user’s default shell:
sudo usermod -s /bin/zsh username
- Change the user’s home directory:
- Add the user to a new group: To add a user to an additional group:
sudo usermod -aG groupname username-aappends the group to the user’s list of groups.-Gis followed by the name of the group.
4. Deleting Users
- Delete a user: To delete a user, you can use the
userdelcommand:sudo userdel usernameThis will remove the user but keep their home directory and files.
- Delete a user and their home directory: If you want to delete the user and their home directory, use the
-roption:sudo userdel -r username
5. Managing Groups
- Create a new group: Use the
groupaddcommand to create a new group:sudo groupadd groupname - Add a user to an existing group: To add a user to an existing group, use the
usermodcommand:sudo usermod -aG groupname username - Delete a group: To delete a group, use the
groupdelcommand:sudo groupdel groupname - Change a user’s primary group: Use the
usermodcommand to change a user’s primary group:sudo usermod -g groupname username
6. Locking and Unlocking User Accounts
- Lock a user account: To prevent a user from logging in, use the
passwdcommand with the-loption:sudo passwd -l username - Unlock a user account: To unlock a user account, use the
passwdcommand with the-uoption:sudo passwd -u username
7. Viewing and Managing User Processes
- View processes running by a user: To see which processes a particular user is running:
ps -u username - Kill processes owned by a user: If you need to kill all processes belonging to a specific user:
sudo pkill -u username
8. Viewing User’s Last Login Time
- Check when the user last logged in: Use the
lastlogcommand to see the last login of each user:sudo lastlog
9. Other User Management Commands
- List groups for a user: To list the groups to which a user belongs:
groups username - Show a user’s account expiration date: Use the
chagecommand to display user account expiration details:sudo chage -l username - Set a user account expiration date: You can set an account expiration date using the
chagecommand:sudo chage -E YYYY-MM-DD username
10. Sudo (Superuser) Permissions
- Add a user to the sudo group: To give a user the ability to execute commands with sudo, you can add them to the
sudogroup:sudo usermod -aG sudo username - Check if a user has sudo privileges: You can check if a user has sudo privileges by using:
sudo -l -U username
Summary of Key Commands
| Command | Description |
|---|---|
useradd username | Add a new user |
passwd username | Set or change a user’s password |
usermod -d /new/home username | Change a user’s home directory |
usermod -s /bin/bash username | Change a user’s shell |
usermod -aG groupname username | Add user to an additional group |
userdel username | Delete a user account |
userdel -r username | Delete a user and their home directory |
groupadd groupname | Create a new group |
groupdel groupname | Delete a group |
passwd -l username | Lock a user account |
passwd -u username | Unlock a user account |
ps -u username | List processes for a specific user |
sudo usermod -aG sudo username | Grant sudo privileges to a user |
These commands are essential for managing users and groups in a Linux system, which is an important aspect of system administration. Be sure to use these commands carefully, especially when modifying or deleting user accounts, to avoid losing important data or access.




