Create a New sudo-enabled User in Ubuntu 18.04/20.04/22.04 LTS & CentOS 7

Create a New sudo-enabled User in Ubuntu/CentOS RedHat RHEL

Introduction

The sudo command temporarily elevates privileges, allowing users to complete sensitive tasks without logging in as the root user.

The following steps will demonstrate how to set up a new user with sudo access on Ubuntu / CentOS / RedHat (RHEL) without having to change the /etc/sudoers file on your server. If you want to set up sudo for an existing user, go to Step 3.

Step 1 : Access Your Server

Connect to root user with SSH:

$ ssh root@your_server_ip_address

Step 2 : Adding a new user

Use the adduser command in order to add a new user to your system:

$ adduser bob

Replace bob with the username that you want to create.

Then, enter some data on the new user. Accepting the defaults is fine, but don’t forget to leave all of this information blank.

Adding user `bob' ...
Adding new group `bob' (1001) ...
Adding new user `bob' (1001) with group `bob' ...
Creating home directory `/home/bob' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for bob
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] y

Step 3 : Add user to sudo group

To add a user to the sudo group, use the usermod command:

$ usermod -aG sudo bob

Make sure to change bob once more to the username you just added. On Ubuntu/CentOS, the sudo group’s members all have full sudo access by default.

Step 4 : Testing the access

Use the su command to switch to the new user account before testing the new sudo permissions:

$ su - bob

Check that you can use sudo as the new user by adding sudo before the command you wish to run with superuser privileges:

$ sudo command

You may list the items in the /root directory, for instance, which is typically only accessible by the root user:

$ sudo ls -la /root
[sudo] password for bob:
total 28
drwx------  3 root root 4096 Aug 12 18:31 .
drwxr-xr-x 19 root root 4096 Aug 22 16:42 ..
-rw-------  1 root root 1068 Aug 22 15:29 .bash_history
-rw-r--r--  1 root root 3106 Dec  5  2019 .bashrc
drwxr-xr-x  3 root root 4096 Aug 12 17:23 .local
-rw-r--r--  1 root root  161 Dec  5  2019 .profile
-rw-r--r--  1 root root    0 Aug 12 18:23 .scmversion
-rw-------  1 root root  738 Aug 12 18:20 .viminfo

Conclusion

In this article, we set up a new user account and granted sudo access to it by included it in the sudo group.

LEAVE A COMMENT