This tutorial focuses on the ways to add users and delete users on CentOS 8 operating systems.
When starting with a fresh CentOS 8 instance, adding and deleting users is one of the most basic tasks that you may have to do.
Adding user can be quite useful.
As your server grows in size, you may want to add new users. You may also be tempted to have specific permissions to them, like sudo rights for example.
On the other hand, you may want to delete some users on your host. For security reasons, it is better to leave user accounts only for people actually using the server on a daily basis.
In this tutorial, we are going all the ways to add and delete users on CentOS 8.
In order to add new users on CentOS 8, you need to be a sudo user, meaning that you have to belong to the wheel group.
If you are curious on how to add new users to the sudoers on CentOS 8, you can follow the tutorial we wrote on the subject.
To check your sudo rights, run the following command
$ sudo -l
If you see the following output, you are good to go.
Adding a user on CentOS 8 using adduser
The first way to add users on CentOS 8 is to use the adduser command.
The adduser is very similar to the useradd command but it provides a more interactive way to add users on your CentOS 8 server.
In general, you want to use adduser rather than useradd (as recommended by the useradd man page itself)
To add a user on CentOS 8, run this command
$ sudo adduser user Adding user 'user' Adding new group 'user' (1007) Adding new user 'user' (1005) with group 'user' Creating home directory '/home/user' Copying files from '/etc/skel'
You will be asked to choose a password for the user
New password: <type your password> Retype new password: <retype your password> Changing the user information for ricky
Then you will be asked to specify some specific information about your new user.
You can leave some values blank if you want by pressing Enter.
Enter the new value, or press ENTER for the default Full Name : Room Number : Work Phone : Home Phone : Other :
Finally, you will be asked if the information provided is correct. Simply press “Y” to add your new user.
Is the information correct? [Y/n] Y
Now that your user is created, you can add it to the wheel group (also called sudoers) in order to make it an administrator user.
Add a user on CentOS 8 using the GNOME desktop
If you installed Centos 8 with a GNOME desktop, there is also a way to create a user directly from the UI environment.
In the Applications tab, search for “Settings“.
In the Settings window, click on the “Details” option.
Select the “Details” option, then click on “Users”.
On the top right corner of the window, click on the “Unlock” button.
Enter your password, and a “Add User” option should now be displayed in the top menu.
In the next window, you are asked whether you want to create a privileged account or not (either with sudo rights or not).
Make sure to fill the full name field and the username field.
You may choose to assign a password, or you can let the user decide on its password on its next logon.
When you are done, click on “Add” in order to create the user.
Congratulations, your account was successfully created.
Adding a user on CentOS 8 using useradd
$ sudo useradd <username>
To assign a password to the user, you can use the -p flag but it is not recommended as other users will be able to see the password.
To assign a password to a user, use the passwd command.
$ sudo passwd <username> New password: Retype new password: passwd: password updated successfully
Check that your user was added
In order to check that your user was created on Linux, run the following command.
$ cat /etc/passwd | grep <user> <user>:x:1000:1000:user,,,:/home/user:/bin/bash
$ cat /etc/passwd | grep <user> | cut -d: -f1 <user>
If there are no entries for this command, make sure to create your user using the adduser command.
Deleting a user on CentOS 8 using deluser
In order to delete a user on CentOS 8, you have to use the deluser command.
$ sudo deluser <username>
In some cases, you may want to remove a user with its home directory.
In this case, you have to add the –remove-home option.
If you don’t specify this option, files belonging to this user will remain intact on the filesystem.
$ sudo deluser --remove-home <username> Looking for files to backup/remove Removing user 'user' Warning: group 'user' has no more members. Done.
To delete all the files associated with a user, use the –remove-all-files parameter.
$ sudo deluser --remove-all-files <username>
Deleting a sudo user with visudo
If you removed a sudo user on CentOS 8, it is very likely that your user still exists in the sudoers file.
To delete a user from the sudoers file, run visudo.
$ sudo visudo
Find the line corresponding to the user you just deleted, and remove this line.
<username> ALL=(ALL:ALL) ALL
Save your file, and your user should not belong to the sudo group anymore.
Deleting a user using the GNOME Desktop
From the users panel we used to create a user before, find the “Remove user” option at the bottom of the window.
Note : you need to unlock the panel to perform this operation.
When clicking on “Remove User“, you are asked if you want to keep the files owned by this user. In this case, I will choose to remove the files.
As you can see, adding and deleting users on CentOS 8 is pretty straightforward.
If you are interested about Linux administration, we have a complete section with detailed articles on the subject.
Make sure to read it in order to grasp complex concepts of Linux System Administration.