Step by Step: How to Add User to Sudoers on Ubuntu 18.04

In this tutorial, I will take you through Step by Step procedure to add user to sudoers on Ubuntu 18.04. In many cases you might have seen that a non-privileged user has been given sudo access to perform root actions. This is required in many Organization where some tasks cannot be performed without having root access. In those cases usually Linux Admin or System Admin provides sudo access to non privileged user to perform root operations as they cannot provide root user access.

Step by Step: How to Add User to Sudoers on Ubuntu 18.04 1

Add User to Sudoers

Also Read: How to Change Date/Time in RedHat/CentOS 7

Step 1: Create New User 

First we need to create an user which can be given sudo access to perform administrative tasks. We will create a user test for our example. This needs to be done either through root user or any other user having sudo access. Here I am using root user to create test user.

root@localhost:~# useradd -m test

-m: Create the user’s home directory if it does not exist. The files and directories contained in the skeleton directory (which can be defined with the -k option) will be copied to the home directory.

By default, if this option is not specified and CREATE_HOME is not enabled, no home directories are created.

Step 2: Set User Password

You can set test user password using passwd command. Provide a strong password when asked and press enter. Retype the same password and press enter again.

root@localhost:~# passwd test
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Step 3: Verify User 

You can verify user from /etc/passwd file. Here you can grep test user from /etc/passwd file and check if the User is Created or not along with his home directory. You can also verify other details as shown below.

Advertisements
root@localhost:~# cat /etc/passwd | grep -i test
test:x:1000:1000:test,,,:/home/test:/bin/bash

test: Name of the User

1000: User Id

1000: Group Id

test,,,,: Comments Section

/home/test: Test User Home directory

/bin/bash: Shell assigned to the User

Step 4: Modify User Group

Now you need to add user to the sudo group using usermod command.

root@localhost:~# usermod -aG sudo test

-a: Add the user to the supplementary group(s). Use only with the -G option.

-G: A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option.

If the user is currently a member of a group which is not listed, the user will be removed from the group. This behaviour can be changed via the -a option, which appends the user to the current supplementary group list.

Now switch to test user and verify if the user is added into the sudo group by running whoami command.

root@localhost:~# su - test
test@localhost:~$ sudo whoami
root

Step 5: Add User to sudoers using visudo

In most of the cases adding user to the sudo group does the job but it is better to add the user in /etc/sudoers file in case it did not work like below:-

root@localhost:~# visudo /etc/sudoers

................................................

test ALL=(ALL:ALL) ALL 

.................................................

NOTE:

Please note that you need to open /etc/sudoers file using visudo command to make sure there is no configuration error in the file.

Step 6: Check Sudo Access

Now you can try running some command and check if the sudo access is working or not.

test@localhost:~$ sudo service nginx start
[sudo] password for test:

User is not in the Sudoers File

Sometimes you might see an error user is not in the sudoers file. To resolve this error, you need to check in the /etc/sudoers file if the User is added or not.

root@localhost:~# grep -i test /etc/sudoers
test ALL=(ALL:ALL) ALL

 

Popular Search

  • Linux Add User
  • Ubuntu Add User
  • Add User to Sudoers File
  • User is not in the Sudoers File
  • Linux Add User to Sudoers
  • etc sudoers

Also Read: How to Download Entire Repository from CentOS Offline

Advertisements

Leave a Reply