How to Add and Delete Users on CentOS 8

Like Linux, CentOS 8 is a multi-user operating system, and it is a best practice to add and delete users on CentOS8 who are sharing the system and its resources. The users can have different levels of permissions, settings, and privileges.

Adding users to CentOS 8 operating system can be useful when several users share the system and resources. In contrast, you might also want to delete those users on your machine who are no longer sharing the resources. 

In this article, we will explain how to add or delete users on CentOS 8 operating system. The post will also discuss how you can assign sudo privileges to the users and manage them. 

What You’ll Need

As this is an administrative task, you will need a non-root account with sudo privileges, or you must log in from the root account to add and delete the users on CentOS 8. 

To check if you are logged in from an account with sudo rights, run the following command:

sudo -l

If you see the following output, you are good to go.

Check Sudo User

How to Add Users in CentOS 8

This section discusses how you can add users to CentOS 8 operating system using different commands. We will also look at how you can assign sudo privileges to your user.

1. Add Users in CentOS 8 Using the useradd Command

In CentOS 8, you can use the useradd command to create a new user account. The syntax for the command is:

sudo useradd <username>

For example, to create a new user account named “Jerry” you would run:

sudo useradd jerry

The useradd command only accepts the username in lowercase letters.

If the user was added successfully, it will not produce any output. However, the users are created in the admin’s home directory located at /home/username

Secondly, you will assign the password to your new user, so that they can log in to their account. To perform this step, execute the passwd command.

For example: 

sudo passwd jerry
add users on centos 8

From the above output, we can see that you will be asked to confirm the password for the user’s account. It is crucial to use a strong password for the user account. 

2. Add Users Using the adduser Command

Like the useradd command, you can also use the adduser command. Both are very similar, but the latter provides a more interactive way to add users. In addition, it is recommended in the useradd manual page to use the adduser command. 

For example:

sudo adduser Thomas
add users on centos 8

The command will prompt you to enter and retype the password.

Changing password for user thomas.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

In addition, it will also ask you to enter the additional information, which you can leave blank by pressing the “Enter” key. 

add or delete users on centos 8

3. Check the Added User to CentOS 8

To ensure that the user was added successfully to the machine, you can view the contents of the passwd file. To view the contents of the file, use the cat command. 

For example:

sudo cat /etc/passwd | grep <user>

or you can also use the same command with the cut filter.

Check the added user

4. Grant Sudo Privileges in CentOS 8

On CentOS 8 operating system, the newly created users get sudo privileges by default. However, if you want to assign the administrative privileges to the newly created user, use the usermod command. 

For example:

sudo usermod -aG wheel thomas

5. Manage Users with Sudo Privilege in CentOS 8

Now that you have granted sudo privileges to the users, you might want to check how many users are there in the group. To check this, use the lid command.

The lid command indicates which group consists of which users. In contrast, using this command with the –g flag will reverse the output. As a result, you will see which users belong to which group. 

For example:

sudo lid -g wheel

How to Delete Users in CentOS 8

Sometimes, it is crucial to delete the user accounts from the machine when they no longer require access or share the resources. When you do not need a user anymore, you can remove it from the operating system by using the userdel command. The syntax for the command is shown below:

sudo userdel <username>

For example, to remove the user “thomas”, you would execute the following command:

sudo userdel Thomas

If the command runs successfully, it will not display any output. 

The userdel command removes the user without removing its files. These files still exist in the system. However, the command removes the user from all the groups. 

To remove the user along with its home directory, use the –r flag with the userdel command. 

For example:

sudo userdel -r thomas

In contrast, you can also use the deluser command to delete the user from CentOS 8.

For example:

sudo deluser thomas

This command will also remove the user without removing its home directory. To remove the home directory, use the option “ –remove-home” with the deluser command.

sudo deluser --remove-home Thomas

You will get a similar output when you execute the deluser command successfully.

delete users on centos8

To delete all the files along with the home directory, use the “–remove-all-files parameter”.

For example:

sudo deluser --remove-all-files thomas

Delete the User from the Sudoers File

Lastly, you can also remove the user from the sudoers file. Since you deleted the user using the userdel or deluser command, it is likely to exist in the sudoers file. Use the visudo command to delete the user. 

sudo visudo

You will see the following output on the screen.

add or delete users on centos8

Just locate the username that you want to delete and erase it. After that, press “Ctrl+X” to exit from the file. Type “Y” and hit Enter key to save. 

In this article, you have seen how to add and delete users easily on CentOS 8 using various commands. The same commands are used in other Linux distributions. Knowing how to add or remove users from the machine is an administrative task that is crucial for effective user management. Furthermore, it is an essential skill that every Linux user should know. 

If this guide helped you, please share it.

Leave a Reply
Related Posts