sudo is a command-line program that allows trusted users to execute commands as root or another user. In this guide, We will show you two ways to grant
sudo privileges to a user. The first one is to add the user to the sudoers file. The second option is to add the user to the sudo group specified in the sudoers file.
1./ Adding User to the sudo Group
On Ubuntu, the easiest way to grant
sudo privileges to a user is by adding the user to the
sudo group. Members of this group can execute any command as root via
sudo and they will prompted to authenticate using their password.
So, to add the user to the group run the command below as root or another sudo user. Make sure you change username with the name of the user that you want to grant permissions to.
$ usermod -aG sudo username
2./ Adding User to the sudoers File
You can grant
sudo privileges to a user by modifying the
sudoers file or by creating a new configuration file in the
01- Let’s say you want to allow the user to run
sudo commands without being asked for a password. To do that, open the
/etc/sudoers file by running the below command:
02- Now, scroll down to the end of the file and add the following line:
username ALL=(ALL) NOPASSWD:ALL
Let’s suppose that we want to allow the user to run only specific commands via
sudo. For example, to allow only the
rmdir commands you can add the below line:
username ALL=(ALL) NOPASSWD:/bin/mkdir,/bin/rmdir
Instead of editing the sudoers file, you can accomplish the same by creating a new file with the authorization rules in the
/etc/sudoers.d directory. However use the below command to create the new rule file:
$ echo "username ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/username
That’s it! you have learned how to grant sudo access to a user on Ubuntu server by using two methods, adding the user to the sudo group or by editing the sudoers file.