×


Add and Remove User in CentOS 8 - Best Method ?

When using a fresh Linux server, adding and removing users is often one of first things you'll need to do.

Basically, to have multiple accounts on a system enables users to use the same system. Every user has its own home directory, documents, and settings, which are not accessible by others. 

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux System queries.

In this context, we shall look into how to add a user in CentOS as well as how to remove it in case you no longer need it You will also learn how to set a password for a user account, verify the user account creation, add and remove a user from a group.


How to add 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.


How to remove 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.


How to add 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 methods as described below.


1. Using usermod command

To add 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 "linuxapt" to a group named "wheel", the command would be:

$ sudo usermod -aG wheel linuxapt

Note: Sudo rights are assigned to a user when it is added to "wheel" group.


2. 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 "linuxapt" to a group named "wheel", the command would be:

$ sudo gpasswd -a linuxapt 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.


How to Remove 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 "linuxapt" from the group "wheel", the command would be:

$ sudo gpasswd -d linuxapt wheel

This command will remove the sudo privileges from the user "linuxapt".


[Need assistance in Installing Missing Software packages on Ubuntu Linux System ? We can help you. ]


Conclusion

This article covers 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

You may need to create separate account for every user want to connect this system. To add new users in system there are two commands available in your system, useradd and adduser. adduser command is the enhanced version of useradd command. adduser command uses useradd command in backend.