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/passwd
file:cat /etc/passwd
Each line in this file represents a user.
- View information about a specific user: You can use the
id
command to view user information such as UID, GID, and group memberships:id username
- Show user login history: You can use the
last
command to show the login history of users:last
2. Adding Users
- Add a new user: To add a new user, you can use the
useradd
command. 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-G
flags:sudo useradd -m -s /bin/bash -G groupname username
-m
creates the home directory for the user.-s /bin/bash
sets the shell for the user.-G groupname
adds 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
passwd
command:sudo passwd username
- Modify user details (like shell, home directory): To modify a user’s details, use the
usermod
command. 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
-a
appends the group to the user’s list of groups.-G
is followed by the name of the group.
4. Deleting Users
- Delete a user: To delete a user, you can use the
userdel
command:sudo userdel username
This 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
-r
option:sudo userdel -r username
5. Managing Groups
- Create a new group: Use the
groupadd
command 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
usermod
command:sudo usermod -aG groupname username
- Delete a group: To delete a group, use the
groupdel
command:sudo groupdel groupname
- Change a user’s primary group: Use the
usermod
command 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
passwd
command with the-l
option:sudo passwd -l username
- Unlock a user account: To unlock a user account, use the
passwd
command with the-u
option: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
lastlog
command 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
chage
command 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
chage
command: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
sudo
group: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.
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!