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.
How to 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
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. More on useradd Man Page.
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.
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
root@localhost:~# usermod -aG sudo test
-a: Add the user to the supplementary group(s). Use only with the -G option. More on usermod Man page.
-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. More on usermod Man page.
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
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 .................................................
visudocommand to make sure there is no configuration error in the file.
Step 6: Check Sudo Access
Now you can try running some command which requires
sudo access 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
How to Set MariaDB root password in RHEL/CentOS 7/8 Using Simple mysql_secure_installation
How to Install AWS CLI in Linux (RedHat/CentOS 7/8) Using 6 Easy Steps
11 Useful LVM(lvcreate, pvcreate and vgcreate) command examples on Linux
How to Install and Configure FTP Server(vsftpd) in Linux (RedHat/CentOS 7/8) Using 8 Easy Steps
20 Useful Linux History Command Examples | Bash Command History
How to Install PHP on Ubuntu 18.04
How to Install Ruby on Ubuntu 18.04 with Easy Steps
How to Install Ruby on CentOS/RedHat 7 in 5 Easy Steps
33 Practical Examples of ulimit command in Linux/Unix for Professionals
How to Add User to Sudoers List
How to Add User to Sudoers in Ubuntu
How to Limit CPU Limit of a Process Using CPULimit in Linux (RHEL/CentOS 7/8)
How to Install Rust Programming Language in Linux Using 6 Best Steps
How to add User to Sudoers & Add User to Sudo Group in Ubuntu