In this article, we will see how to create a new user and provide sudo privileges on CentOS. As mentioned above we can use the sudo user to perform any admin tasks in linux by default admin user is called root
. sudo user doesn’t need to login as root user to do that.
The sudo user is important part as per security aspect also it allow us to run some programs or command with admin privileges. Sudo is powerful enough to do any task that an admin can do.
Read Also: How to Add and Delete Users on CentOS 8
Create a Sudo User on CentOS
First we need to create a new user on CentOS if you already created any user or you already have a user that you want to assign sudo
privileges, By default in CentOS there is group called wheel
. You simply need to add your user into this group and that user will have sudo access. You can directly go to step 4
if you want to provide sudo privileges to already existed user.
Below are the steps to create an user and then assign that user to wheel group to provide him sudo privileges.
1. Login to server
You need to login your CentOS server as below.
ssh root@server_ip_address
2. Create a new user
Now run this simple command to add a new user. replace username
with your user name
useradd username
3. Set user password
Now when user has been created above. we need to set a password for the user so to do that we use passwd
command, so run the below command and don’t forgot to replace username
with the user name you added above:
passwd username
As soon as you will run above command you will be asked to put password and confirm password, So use a strong password.
Note
:- whatever you will type will not be visible
Changing password for user username. New password: Retype new password: passwd: all authentication tokens updated successfully.
4. Now, Add the user to the sudo
group
In CentOS server there is group wheel
that used to grant sudo access directly to user just by adding the user to wheel
group. To do that just run below command and replace username
with the user you want to give sudo access:
usermod -aG wheel username
How to use Sudo
Well, We have created a user and assigned sudo access so now use the sudo power we need to login as that user first. So to do that run below command:
su - username
Now to use sudo power, simply add sudo
keyword before the command as like below syntax:
sudo [COMMAND]
Lets see an example, Here below is the command to list /root/
files with sudo power:
sudo ls -l /root
At very first time you run the sudo command from the new user account you will get some message like below and it will ask your for user account password. Put Password and it will take you ahead and will run your command.
We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things: #1) Respect the privacy of others. #2) Think before you type. #3) With great power comes great responsibility. [sudo] password for username:
Conclusion
It was very easy to setup a user and assign that user sudo privilege. As above we successfully achieve this on CentOS server and now we are ready to run any admin task.
Let us know below if you are facing any issue further in comments
Leave a Reply