Chat Zalo Chat Messenger Phone Number Đăng nhập
How To Add and Delete Users on a CentOS 7 Server - DigitalOcean

How To Add and Delete Users on a CentOS 7 Server – DigitalOcean

When

you

start using a new Linux server, adding and removing users is often one of the first things you’ll need to do. In this guide, you will learn how to create user accounts, assign sudo privileges, and delete users on a CentOS 7 server.

Prerequisites

To complete this tutorial, you will need:

  • A CentOS 7 server with a non-root sudo-enabled user. If you are logged in as root, you can remove the sudo portion of all the following commands. For guidance, see our tutorial Initial Server Setup with CentOS 7.

Add users

Throughout this tutorial we will work with the user sammy. Please replace it with the username of your choice.

You can add a new user by typing:

sudo adduser sammy

Next, you will need to give your user a password so that they can log in. To do this, use the passwd command

: sudo passwd sammy

You will be prompted to enter the password twice to confirm it. Now your new user is set up and ready to use! You can now log in as that user, using the password you set.

Granting sudo privileges to a user If your new user must

have the ability to execute commands with root (administrative) privileges, you must grant the new user

access

to sudo

.

We can do this by adding the user to the wheel group (which gives sudo access to all its members by default).

To do this, use the command

usermod: sudo usermod -aG wheel sammy

Now your new user can run commands with administrative privileges. To do so, simply type sudo before the command you want to run as an administrator: sudo

some_command

You will be asked to enter your user account password (not the root password). Once the correct password has been submitted, the command you entered will be executed with root privileges.

Managing

users with Sudo privileges

To see which users are part of the wheel group (and therefore have sudo), you can use the lid

function. lid is typically used to show which groups a user belongs to, but with the -g flag, you can reverse it and show which users belong to a group: sudo lid -g wheel sammy output(

uid=1001)

The output will show you the user names and UIDs associated with the group. This is a good way to confirm that the previous commands were successful and that the user has the privileges they need.

Deleting users

If you have a user account

that you no longer need, it is best to

delete the old account. If you want to delete the user

without deleting any of its files

, type: sudo userdel sammy

If you want to delete the user’s home directory along with the user account itself, type:

sudo userdel -r sammy

with either command the user will be automatically removed from any group to which he or she has been added including the wheel group if you were granted sudo privileges. If you later add another user with the same name, you will have to add themselves back to the wheel group to gain sudo access.

Conclusion

You should now have a good understanding of how to add and remove users from your CentOS 7 server. Effective user management will allow you to separate users and give them only the access necessary for them to do their jobs. You can now move on to configuring your CentOS 7 server for any software you need, such as a LAMP or LEMP web stack.

For more information on how to set up sudo, see our guide on how to edit the sudoers file.

Contact US