Linux commands to manage users and groups
Linux is a multi-user operating system. It means that many user can work on the system simultaneously.
You can find out user information from /etc/passwd file.
Each Linux user should belong to a specific group.
You can find out groups information from /etc/group file.
Here is the snapshot of typical passwd and group file in Linux.
Each line in passwd file contains below information.
Each line in group file contains below information.
Below is the list of commands to manage users and groups in linux system.
- useradd – add new user into the system
- userdel – delete the specific user from the system
- usermod – modify the user details
- groupadd – add new group in the system
- groupdel – delete specific group from the system
- groupmod – modify the group details
- passwd – change or set the password for given user.
- finger user – Prints details about the user. Please note that you will to install finger package for this.
- whoami – gives name of the user currently logged into the system
- id – shows the user id, group id and group details for given user.
Setting up new user in Linux
You can create new user and set password for that user using 2 commands below. Below commands will create new user – “shaun”
sudo useradd shaun
sudo passwd shaun
Modifying user in Linux
You can modify user using usermod command. This command has many options that allow you to update the user.
- usermod -d /home/newdir/ shaun -> New home directory for the user shaun
- usermod -e 2017-12-30 shaun -> Set the expiry date for the user shaun
- usermod -g newg shaun -> change the primary group of user shaun
- usermod -aG secondaryg shaun -> append new group to user shaun
- usermod -l mark shaun -> change login name of shaun to mark
- usermod -L shaun -> lock user shaun
- usermod -U shaun -> unlock user shaun
- usermod -d /var/shaun/ -m shaun ->move home directory of shaun to new location
- usermod -s /bin/sh shaun -> Change the default shell of user shaun
- usermod -u 777 shaun -> Change the user id of shaun
You can also use multiple options in single line.
- usermod -d /home/kk/ -s /bin/bash -e 2018-02-22 -c “This is abc” -u 4 -aG g1 shaun
- usermod -u 111 -g 444 shaun
If you want to view the expiry date of user account, you can use below command.
- chage -l shaun
Note that you can change the account expiry details using chage command as well. For example, if you want to change the account expiry of user shaun, you can use below command.
Deleting the user using userdel command in Linux
You can delete a user using below command.
sudo userdel <username>
passwd command in Linux
When you execute passwd command, you will have to enter current password. If you want to change the password for other user, you can use below command. Note that you should be a root user to be able to change password of other users.
A root user can also disable the password of other users using below command.
passwd -d <username>
If you want to change the passwords of multiple users at the same time, you can use below command followed by list of user name and their passwords.
id command in Linux
id command will display your user id, group id and the groups you belong to. If you want to see other user details you can use below syntax.
group commands in Linux
You can use below commands to manage groups in Linux.
To add new group say “g1”, you can use below command.
sudo groupadd g1
To rename the group from say “g1” to “newg1”, you can use below command.
groupmod -n newg1 g1
To change the id of group, you can use below command. Here we are changing the id of group g1 to 1010
groupmod -g 1010 g1
To delete the group say “newg1”, you can use below command.
sudo groupdel newg1
Switching user accounts
“su” command is used to switch user account.
Switch to a different user account using su command. Super user can switch to any other user without entering their password.
sudo is similar command but only allows execution of one command at a time with special privileges.