Add user to sudoers

What is the importance of learning how to add user to sudoers?

For one, it’s to grant elevated access or permission to a user so that the user can carry unique functions that standard users would not have access to.

These include sensitive tasks such as viewing the contents found in your root directory, for example. If you want to edit the contents in the root directory, sudo access is required as well.

For this reason, we’ll teach you how to add user to sudoers or grant sudo privileges to a user in this guide.

Getting Started

Before we teach you how to add user to sudoers, you’ll have to first meet certain prerequisites. Also, there are two ways to add user to sudoers:

  • Add user to the sudoers file (The sudoers file is a file that contains all the information used to specify which users and groups should be granted with access or sudo privileges).
  • Add user to sudo group (On Ubuntu and Linux Mint, sudo group members have sudo access by default).

As for the prerequisites, you’ll need the following:

  • A system that runs a supported Ubuntu version
  • Access to the command line or terminal window (Ctrl + Alt + T)
  • Access to a root account or account with root/sudo privileges

We can now dive into learning how to add a new user to the sudo group and sudoers file.

How to Add User to Sudoers File

Before you can add a group or user to the sudoers file, you have to access the sudoers file first since this is where you’ll be able to customize sudo access for the user you want to add.

While in the sudoers file, you can personalize command access and configure security policies to your liking.

If you plan to proceed with this method, always use visudo when editing the /etc/sudoers file. This ensures that the /etc/sudoers file is locked and prevents any modifications that may cause syntax errors in the process.

To open the /etc/sudoers file, you’ll have to type this command:

visudo

This will take you to your text editor installed in your system, Vim. Here, you’ll be able to modify changes and grant sudo access to the user such as allowing the user to run sudo commands without the need for a password.

If you don’t feel comfortable using Vim and have Nano installed in your system, you can change the default editor by running this command:

EDITOR=nano visudo

Once you open the /etc/sudoers file, look for the line: “User privilege specification.” This is the typical expression found on a Debian system. If you’re running CentOS, look for: “Allow root to run any commands anywhere.” 

Below, you’ll find the command syntax:

root ALL=(ALL) ALL

To provide sudo access or add user to sudo, this will be the basic syntax you should follow:

username host_list = (users) command

where,

username – The user or new user you want to provide sudo privileges to

host_list – Refers to the hosts that the user is granted sudo access

(users) – Refers to which username can execute the command

command – Refers to the command/s that the user is allowed to perform as the root user

If you want the user to execute a sudo command without the need for a password, the command will be as follows:

Dave ALL=(ALL) NOPASSWD:ALL

where,

Dave refers to the username you want to be able to execute a sudo command.

If you want to allow a particular group to run any sudo command as any user in any host, then type:

%admins ALL=(ALL) ALL

Note this will grant the group access to run a sudo command, but will still need a password. To authenticate users to run sudo commands without a password, make sure to refer to the “NOPASSWD:ALL” example.

Afterward, make sure to save your changes and quit the text editor. Also, don’t forget to edit the username syntax of the username you want to grant sudo access to.

Now, what if you want the user to run specific commands like rmdir and mkdir? To do this, type:

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

How to Add Another User to Sudo Group

Adding another user to the sudo group is easier than if you want to add the user to the sudoers file. Users part of the sudo group can execute any sudo command and authenticate themselves with the password they provide.

Before we hop in, let’s assume you haven’t created a new user yet. To do this, type on the command line:

sudo adduser username

The command line will prompt a few questions to answer such as password, full name, work phone, home phone, etc.

Once this is done, verify your new user’s information by typing “Y” and from there, you’ll have successfully created the user.

Now, we can add the user you created to the sudo group. The command syntax will follow the adduser sudo command:

usermod -aG sudo username

If the username is Dave, type:

usermod -aG sudo Dave

Be sure you leave no space when typing adduser. If you type add user sudo, this will prompt an error message.

After you’ve granted access, make sure this new user has sudo privileges by typing:

sudo whoami

You’ll be asked for the password, and if the user has sudo privileges, you’ll be prompted with the output message:

root

Conclusion

We hope this guide was able to help you learn in adding users to both the sudoers file and sudo group! For any more questions, let us know in the comments section below!