As a Linux user, you might wish to know how to add and delete users in CentOS 7. After launching a new Linux server, when you start using your favorite distribution, adding and removing users is what you would find necessary to know about. All Linux distributions, including CentOS, are multi-user operating systems. Distinct command-line and GUI applications can have different permission levels and settings for each user.
After buying CentOS VPS, you are recommended to not give various users access to the root account. So, join us with this article to be able to add, delete and list users in CentOS 7. Also, some additional commands will be taught to help you to be an expert in user account changing in Linux.
Prerequisites to Add and Delete Users in CentOS
To let this tutorial work correctly, consider the below options and then move on.
- A server running CentOS.
- A non-root user with
Note: Keep in mind to replace your own username with the one we will use in this article called ‘’OperaVPS’’.
Add a new User Account in CentOS 7
Let’s go through this guide and learn some simple commands to meet the purpose of reading this content. Firstly, the way you can add users will be explained.
Run the following command to add a new user:
sudo adduser OperaVPS
To let your user log in, use the command below to give a password to your user:
sudo passwd OperaVPS
You can use the new user after typing the password twice and confirming it. You must log in as your new user using the password you have set up after running the above command.
How to make a User with Sudo Privileges in CentOS
If you consider having a user with the ability to execute commands with administrative privileges, you need to grant
sudo Privileges to your user. To do this, use the command below to give access to all the user members.
sudo usermod -aG wheel OperaVPS
Running the above command enables your new user to execute commands with administrative privileges. When you want to execute a command as an administrator, you just need to type
sudo ahead of that.
Which users have
After granting Sudo Privileges to your desired user, you can manage the users with Sudo Privileges. To do this, you should be able to see which users are part of the wheel group. So, run the command below to view the usernames and UIDs that are associated with the group:
sudo lid -g wheel
Change or Rename Username and UID
Simply, you can use the commands below to change or rename the username and UID in CentOS 7.
$ sudo usermod -l login-name old-name # rename 'OperaVPS' user to 'OperaLinux' # $ sudo usermod -l OperaLinux OperaVPS
How to list user accounts of CentOS Linux
You can use the following command to display a list of all Linux user accounts.
And to view the information of the currently logged-in user, run:
$ id $ id userNameHere $ id root $ id OperaVPS
Also, you can use the following command to see the owner and group of a file named /etc/hosts:
ls -l /path/to/file ls -l /home/OperaVPS/Documents/resume.pdf ls -l /etc/hosts
to see the owner of the process, type:
ps top htop top -u OperaVPS ps -au
Lock and Unlock User accounts in CentOS 7
To lock the user account, run:
sudo usermod -L OperaVPS
Use the command below to unlock the user account:
sudo usermod -U OperaVPS
Delete User Account in CentOS 7
So, far, you learned how to add/create a user account in CentOS 7 and you reviewed the way you can grant them Sudo Privileges. In the latest part of this article, let’s see how to delete a user account in CentOS 7.
Due to some security reasons or when you no longer need a user account, you can delete it. Run the command below to remove the user without deleting any of their files:
sudo userdel OperaVPS
And to remove the user’s home directory along with the user account itself, run:
sudo userdel -r OperaVPS
Any group that the user was added to, including the wheel group if they had
sudo access will be automatically removed with either command. In order to grant them
sudo access, you will need to add them to the wheel group once more if you later add another user with the same name.
How to specify home directory at my desired location plz?
Use the following command to do this:
# adduser username --shell=/bin/bash
In this article, you learned How to Add, Delete and List Users in CentOS 7. After reading this article, you must be able to add or remove users from your CentOS 7 server easily. In this way, you can separate users and give them only access. Also, if you wish to configure your CentOS 7 server for LAMP or LEMP we stack, all is ready now. This tutorial work for any other Linux distribution.