How to Add User to Sudoers in Ubuntu Server 21.04

Louis SanchezAugust 15th 2021, 3:01

Add User to Sudoers

Step 1 : To give additional users access to sudo, all you would need to do is add them to the sudo group

sudo usermod -aG sudo myuser

Configure sudo

Step 2 : User with sudo privileges access to everything, and that may or may not be what you want.

To actually configure sudo, we use the visudo command.

sudo visudo /etc/sudoers

By default, Ubuntu opens up the nano text editor when you use visudo. With nano, you can save changes using Ctrl + W, and you can exit the text editor with Ctrl + X.

Step 3 : This is the line of configuration that enables sudo access to anyone who is a member of the sudo group

%sudo   ALL=(ALL:ALL) ALL

Step 4 : You don't have to enable access by group. You can actually call out a username instead.

root    ALL=(ALL:ALL) ALL

The first ALL : means that root is able to use sudo from any Terminal.

The second ALL : means that root can use sudo to impersonate any other user.

The third ALL : means that root can impersonate any other group.

The last ALL : refers to what commands this user is able to do

Step 5 : I'll give some additional examples.

username    ALL=(ALL:ALL) /usr/sbin/reboot,/usr/sbin/shutdown

Step 6 : We're allowing user username to execute the reboot and shutdown commands. If user username tries to do something else (such as install a package / update), he will receive an error message:

Step 7 : We're allowing username to impersonate other users. we could just remove the (ALL:ALL) from the line altogether to prevent username from using the -u option of sudo to run commands as other users:

username    ALL= /usr/bin/apt