Having multiple accounts on a system enables the users to use the same system. Every user has its own home directory, documents, and settings, which are not accessible by others. This post describes how to add a user in CentOS as well as how to remove it in case you no longer need it It also describes how to set a password for a user account, verify the user account creation, add and remove a user from a group.
Note: The method shown here has been tested on CentOS 8. Also note that, you will need to either log in as a root user or run the commands as sudo.
Adding User in CentOS
You can add a new user in CentOS using the adduser command.
1. To add a new user, use the following command syntax:
$ sudo adduser <user_name>
Now you’ll be asked to enter the sudo password. Enter it and then the required user account will be created.
Note: I have run the command as sudo because I am logged in as a non-root user.
2. Then to set a password for this new user account, use the passwd command as follows:
$ sudo passwd <user_name>
Now set a password for this user account. You will be asked to re-type it to avoid any typing error.
3. To verify the user account creation, use the command below:
$ id -u <user_name>
If the <user_name> exists, you will see its id in the output. Otherwise, it will show ‘no such user” in the output.
Removing User in CentOS
If you no longer need a user in CentOS, it is better to remove it. You can remove a user from the CentOS system using the userdel command.
1. To only remove a user account but not its home directory, use the command below:
$ sudo userdel <user_name>
To remove a user account and also its home directory, use the command below:
$ sudo userdel -r <user_name>
Now you’ll be asked to provide the sudo password. Enter it and then the required user account will be removed.
2. After removing the user account, you can also verify it using the following command:
$ id -u <user_name>
If the <user_name> has been removed, you will not see the user id in the output.
Adding User to a Group
When a user is created, it is assigned a group with the same name as of user. This group is created along with user creation. After creating a user account, you may want to add it in other groups. You can add a user to a group using two ways as described below.
Method #1 Using usermod command
For adding a user to a group, use the below syntax:
$ sudo usermod -aG <group_name> <user_name>
The above command will add the user-specified by <user_name> to a group specified by <group_name>. For instance, to add a user named “ummmara” to a group named “wheel”, the command would be:
$ sudo usermod -aG wheel ummmara
Note: Sudo rights are assigned to a user when it is added to “wheel” group.
Method #2 Using gpasswd command
Another command to add a user to a group is gpasswd. Here is the syntax of the command:
$ sudo gpasswd -a <user_name> <group_name>
The above command will add the user-specified by <user_name> to a group specified by <group_name>. For instance, to add a user named “ummmara” to a group named “wheel”, the command would be:
$ sudo gpasswd -a ummmara wheel
After adding a user account to a group, you can verify it by typing groups followed by user name:
$ groups <user_name>
This command will display the groups a user-specified by <user_name> belongs to. For instance, the output below shows the user “ummmara” belongs to two groups “ummmara” and “wheel”.
Removing User from a Group
In order to remove a user from a certain group, gpasswd command is used. Here is the syntax of the command:
$ sudo gpasswd -d <user_name> <group_name>
The above command will remove the user-specified by <user_name> from the group specified by <group_name>.
For instance, to remove a user “ummmara” from the group “wheel”, the command would be:
$ sudo gpasswd -d ummmara wheel
This command will remove the sudo privileges from the user “ummmara”.
In this post, we described how to add a user as well as how to remove it in case you no longer need it. We also described how to add and remove a user to and from a group. If you are using Ubuntu distribution, visit how to add or remove users in Ubuntu.