Signup/Sign In

Adding User to Sudoers file (manage privileges)

sudoers is a special file in the /etc directory, and has the path /etc/sudoers. It is a file used to track which users, and groups have access to the sudo command, or root level access to the system via sudo.

Most Linux users at some point of time have encountered and utilised the sudo command.

This is because either the application can be run only by a user in the sudo group, or by the root user and a normal user can satisfy either condition only through sudo.

Sometimes we need to give certain users, or groups access to applications that can only be run by root, or via sudo, and when that happens we need to know how to add a user to sudoers, and give them access to sudo.

When we are adding users to sudoers, we can do this in two ways. One is by manually editing the sudoers file (via visudo), or by using the usermod command.

Adding users manually to sudoers file in Linux

We need to first open the sudoers file, and there is a command built specifically to ensure that any edits made are compliant with the sudoers specification and that no mistakes are made in editing. The program built for this is the visudo program.

To run visudo, we need a user with sudo access or we need the root user. By default, visudo uses vi/vim as its editor, and nano on Ubuntu, but the default editor can be changed using multiple different various methods. There should be no issues even if you change your editor, as long as multiple changes or unknown formatting changes are not made.

Default contents of the file are similar to the screenshot provided below.

sudoers screencap

The place where root ALL=(ALL:ALL) ALL, that's where we will make our edits for our users. For example, to give root access to the user dakksh, we would add the following line dakksh ALL=(ALL:ALL) ALL, after the root line. This makes dakksh sudo-capable.

visudo add user to sudoers

Adding Users with the usermod command in Linux

Though we have the visudo command, and it is an official method of adding users to sudoers, or giving a user, or a group sudo access, it is a complex method and requires proficiency, as well as a good handle over the format of the sudoers file. The other method of adding a user to sudoers, is to add a user to the sudo group.

It is well known that every Linux distribution comes with a default sudo group and a user can be added to that sudo group. So what we can do is, to use the usermod command with the following syntax to add a user to the sudo group.

sudo usermod -a -G sudo <userName>

The above command can be broken down into the following parts:

  • sudo: The first one is running usermod with root access, while the second specifies what group it is that we are adding.
  • -a: Append, otherwise usermod overwrites the group list of a user.
  • -G: A comma-separated list of groups to which the user is to be added.
  • <userName>: The name of the user whose groups are being modified (the angle brackets are specifying it is a parameter)

For example, we want to add the user dakksh to the group sudo, after deleting the above line from the sudoers file, we would run:

sudo usermod -a -G sudo dakksh

sudo useradd user to sudo group

Now both ways to give a user sudo access has been discussed and depending on the use case, and the amount of freedom to be offered, it can be seen how to add a user to sudo.

Conclusion

This tutorial has discussed how to give a user sudo access, using either:

  • visudo: Add user to sudoers file manually, deciding how much freedom is to be given to the user, and what applications can be run, or what groups. This method offers a lot of freedom but is complex.
  • usermod: Add user to sudo group, restricting the amount of freedom on what all permissions are offered to them, but straightforward.


About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.