OSI & TCP/IP MODEL

How to Add User to Sudoers in Ubuntu

 


Adding User to the sudo Group 

usermod -aG sudo username

Granting sudo access using this method is sufficient for most use cases.


To ensure that the user has sudo privileges, run the whoami command:


sudo whoami

You will be prompted to enter the password. If the user has sudo access, the command will print “root”:


root

If you get an error saying “user is not in the sudoers file”, it means that the user doesn’t have sudo privileges.


Adding User to the sudoers File

The users' and groups' sudo privileges are defined in the /etc/sudoers file. Adding the user to this file allows you to grant customized access to the commands and configure custom security policies.


You can configure the user sudo access by modifying the sudoers file or by creating a new configuration file in the /etc/sudoers.d directory. The files inside this directory are included in the sudoers file.


Always use visudo to edit the /etc/sudoers file. This command checks the file for syntax errors when you save it. If there are any errors, the file is not saved. If you open the file with a text editor, a syntax error may result in losing the sudo access.



Typically, visudo uses vim to open the /etc/sudoers. If you don’t have experience with vim and you want to edit the file with nano , change the default editor by running:


EDITOR=nano visudo

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:


visudo

Scroll down to the end of the file and add the following line:



/etc/sudoers

username  ALL=(ALL) NOPASSWD:ALL

Copy



Save a file and quit the editor . Do not forget to change “username” with the username you want to grant access to.

Another typical example is to allow the user to run only specific commands via sudo . For example, to allow only the mkdir and rmdir commands, you would use:


/etc/sudoers

username ALL=(ALL) NOPASSWD:/bin/mkdir,/bin/rmdir

Copy

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. Add the same rule as you would add to the sudoers file:


echo "username  ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/username

Comments