How to Add and Delete Users on CentOS 7

Introduction

CentOS, like all other Linux variants, is a multi-user operating system. Each user may have varied permission levels and specific settings for a variety of command-line and GUI apps. One of the most critical skills a Linux user should have is, the ability to add and remove users.

One of the most important skills a Linux user should have is, the ability to add and remove users.

In this tutorial, you will understand how to add and remove users on CentOS 7 computers in this tutorial. We will also address a few FAQs on how to add and remove users on CentOS 7.

Prerequisites

To create and remove users, you must be logged in as root or as a user with sudo rights.

How To Add User in CentOS

The useradd command-line utility in CentOS can be used to establish a new user account.

You would run the following commands to create a new user account named "username":

sudo adduser username

There is no output from the command above. It will construct the new user's home directory (/home/username) and copy files from the /etc/skel directory to it. The user can write, edit, and delete files and folders in their home directory.

After that, you'll need to provide the new user a password so that they may log in. Use the passwd command to do so:

sudo passwd username

You'll be asked to type in and confirm your password. Make sure your password is strong.

Output

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

Sudo's access is allowed to members of the group wheel by default on CentOS.

Add the newly created user to the wheel group if you want them to have administrative rights:

sudo usermod -aG wheel username
💡
If you’re logged in as root, you don’t have to prepend each command with sudo.

How To Delete a User in CentOS

You can delete the user account using the deluser command-line utility if it is no longer required.

Run the following command to deactivate the user without removing the user's files:

sudo userdel username

The -r flag is used to destroy the user's home directory and mail spool:

sudo userdel -r username

The userdel command produces no output when it succeeds.

If the user has sudo capabilities, he or she will be removed from the wheel group, as well as any other groups to which the user belongs.

FAQs to Add and Delete Users on CentOS 7

What options can I use with the useradd command? 

Some commonly used options with useradd include -m to create a home directory, -G to add the user to supplementary groups, and -s to specify the login shell.

How do I set a password for a user on CentOS 7? 

To set a password for a user, use the passwd command followed by the username. For example, sudo passwd johndoe will prompt you to set a password for the user "johndoe".

How can I assign a specific user ID (UID) to a new user? 

To assign a specific UID to a new user, use the -u option with the useradd command. For instance, sudo useradd -u 1001 johndoe will create the user "johndoe" with UID 1001.

What is the command to delete a user in CentOS 7?

To delete a user, use the userdel command followed by the username. For example, sudo userdel johndoe will remove the user "johndoe".

Can I remove a user's home directory while deleting the user? 

Yes, the userdel command has an option -r which removes the user's home directory and its content. You can use sudo userdel -r johndoe to delete the user "johndoe" along with their home directory.

What command do I use to change the username of an existing user? 

To change a username, use the usermod command followed by the old and new usernames. For example, sudo usermod -l newuser olduser changes the username "olduser" to "newuser".

How can I find out the groups a user belongs to on CentOS 7? 

To see the groups a user belongs to, use the groups command followed by the username. Running groups johndoe will display the groups that "johndoe" is a member of.

Conclusion

You learnt how to add and remove users in CentOS in this tutorial. Any other Linux distribution can use the same commands.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.