Sudo command allows users to execute the programs with the privileges of other users, especially the default root user. As long as the username is present in the sudoers file, sudo command can be used to execute commands with root user privileges. It runs a single command and requires only the current user password to switch to the root user. This largely helps in increasing system security as well as accidental changes were done to the system.
The below steps can be followed to create sudo users with Sudo privileges for CentOS, Ubuntu & Debian.
CentOS
Access the server using SSH.
# ssh root@server_ip_address
Create a new user account using the command useradd
. You need to replace newuser
with the real username that you want to create.
# useradd newuser
Create a strong password
for the user.
# passwd newuser
*You will be prompted to set and confirm the new user password
. Make sure that the password for the new account is as strong as possible.
The output will be the same as below.
# passwd newuser
Changing password for user newuser.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
Add User to the Wheel Group
.
By default, the wheel group is a special user group that allows all members in the group to run all commands on CentOS systems. So, need to add the new user to this group so it can run commands as superuser
.
# usermod -aG wheel newuser
Switch to newly created user
.
# su - newuser
Ubuntu & Debian
Access the server using SSH.
# ssh root@server_ip_address
Create a new user account using the command adduser
. You need to replace newuser
with the real username that you want to create:
# adduser newuser
*You will be prompted to set and confirm the new user password
. Make sure that the password for the new account is as strong as possible.
Adding user `newuser’…
Adding new group `newuser’ (1001) ….
Adding new user `newuser’ (1001) with group `newuser’ …
Creating home directory `/home/newuser’ …
Copying files from `/etc/skel’ …
New password:
Retype new password:
passwd: password updated successfully
Make sure to use a strong password for your account. The next step is to enter basic information about the user’s account such as name, phone number, etc.
Enter the user information for newuser
Enter the new value, or press ENTER for the default
Full Name [ ]:
Room Number [ ]:
Work Phone [ ]:
Home Phone [ ]:
Other [ ] :
Is the information correct? [ Y/n ]
Add User to the sudo group
.
# usermod -aG sudo newuser
Switch to newly created user
.
# su - newuser
Fedora
Access the server using SSH.
# ssh root@server_ip_address
Create a new user account using the command useradd
. You need to replace newuser
with the real username that you want to create:
# useradd newuser
Create a strong password for the user.
# passwd newuser
*You will be prompted to set and confirm the new user password
. Make sure that the password for the new account is as strong as possible.
Add User to the Wheel Group
.
# sudo usermod -aG wheel newuser
Switch to newly created user
.
# su - newuser
Examples for testing Sudo user
The following examples show how a user having sudo permission can execute a command which can only be executed by the root user.
Creating a user named user1
and adding the user to sudo
.
Testing the working of Sudo, normal user accounts do not have the privilege to add or remove directories under the ownership of the root user. Using Sudo user it is possible to add or remove directories under the ownership of the root user.
Installing packages using sudo, normal users do not have the privilege to install applications on the server. Using Sudo user it is possible to install applications on the server.
Normal user does not have the privilege to start-stop services.
Using Sudo user can start stop services.
Related Tutorials