Oct 9, 2023 4 min read

How to Add Users to Sudoers on Ubuntu

Add users to Sudoers on Ubuntu with our step-by-step tutorial. It is a powerful command used in Unix-based operating systems.

Add Users to Sudoers on Ubuntu
Table of Contents

Introduction

Before we begin talking about how to add users to Sudoers on Ubuntu, let's briefly understand – What is Sudo?

Sudo stands for "superuser do" and is a powerful command used in Unix-based operating systems. It allows regular users to execute specific commands with elevated privileges, enabling them to perform administrative tasks without needing to log in as the root user.

Sudo provides an extra layer of security by restricting the use of root privileges to authorized individuals only. It is widely used for system management, software installation, and other critical operations. Understanding how to properly use Sudo is crucial for any Unix user to ensure system security and efficiency.

In this tutorial, you will add users to Sudoers on Ubuntu. We will also address a few FAQs on how to add users to Sudoers on Ubuntu.

Advantages of Sudo

  1. Security: Sudo provides a secure way to grant temporary root access, reducing the risk of unauthorized system modifications.
  2. Accountability: Sudo logs all executed commands, allowing administrators to track and audit user activity.
  3. Granular Control: Admins can define fine-grained access privileges, specifying which commands and resources users are allowed to access.
  4. Simplicity: Sudo simplifies user management by centralizing access control in a single configuration file.
  5. Ease of Use: Users don't need to remember the root password; they can execute privileged commands by using their own passwords.

Adding User to the sudo Group

Adding a user to the "sudo" group is the most straightforward method on Ubuntu for providing them with sudo access. Participants in this group will be required to enter their password whenever they use Sudo to run a command as root.

If the user doesn't already exist, we'll assume that they do. Check out this manual if you need to create a new user.

The following command must be executed as root or another user with Sudo permissions to add the user to the group. Make sure "username" is replaced with the desired user's actual name.

usermod -aG sudo username

For the majority of scenarios, granting sudo access using this technique is enough.

Run the whoami command to see whether the user has sudo privileges:

sudo whoami

The password must be entered when asked. The command will print "root" if the user has sudo access:

Output

root

The user does not have sudo rights if you see the message "user is not in the sudoers file."

Adding User to the sudoers File

Access to the Sudo command is controlled by entries in the /etc/sudoers file, which are assigned to users and groups. By adding the user to this file, you may set up unique permissions for them to run certain tasks and establish other security settings.

The sudoers file, or a new configuration file in the /etc/sudoers.d directory, is where you set permissions for users to run Sudo commands. The sudoers file references the contents of this directory.

To make changes to /etc/sudoers, the recommended editor is visudo. When you save the file, any syntax mistakes will be caught by this command. The file is not saved if there are any mistakes. A syntax mistake in a text editor might cause you to lose Sudo privileges if you open the file.

When you use visudo, the /etc/sudoers file will open in vim. If you're unfamiliar with vim but want to use nano instead, you may switch to it by typing:

EDITOR=nano visudo

Say you want the user to be able to execute sudo commands without being prompted for a password. Open the /etc/sudoers file to accomplish that:

visudo

Add the following line at the end of the file by scrolling down:

username  ALL=(ALL) NOPASSWD:ALL

You should save your work and close the editor. Make sure "username" is replaced with the actual username you wish to permit.

Allowing the user to execute just certain commands using sudo is another common use case. To restrict permissions to only mkdir and rmdir, for instance, you may use:

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

You may achieve the same result without modifying the sudoers file by adding a new file with the authorization rules to the /etc/sudoers.d directory. The identical rule that you would put to the sudoers file should be included here:

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

The administration of the sudo rights is easier to maintain using this method. The file's name is not crucial. It is customary for the filename to correspond to the username.

FAQs to Add users to Sudoers on Ubuntu

Can I add multiple users to the sudoers file at once? 

Yes, you can add multiple users to the sudoers file by repeating the username ALL=(ALL:ALL) ALL line for each user you want to add.

How can I grant a user specific administrative privileges?

You can grant a user specific administrative privileges by modifying the sudoers file using the visudo command. For example, to allow a user to run only a specific command as root, use username ALL=(ALL:ALL) /path/to/command.

Are there any graphical tools to add users to sudoers in Ubuntu? 

Yes, Ubuntu provides a tool called "Users and Groups" that allows you to add users to the sudo group, granting them sudo privileges. You can access it by searching for "Users and Groups" in the application menu.

How do I remove a user from the sudoers file? 

To remove a user from the sudoers file, open the sudoers file using sudo visudo command. Then, delete the corresponding line that grants privileges to that user. Save the file and exit.

Is it possible to restrict sudo access for certain commands? 

Yes, you can restrict sudo access for specific commands by modifying the sudoers file. Use the syntax username ALL=(ALL:ALL) /path/to/command to grant access only to the specified command.

What happens if I make a mistake in the sudoers file?

 If you make a mistake in the sudoers file and save it, it may prevent sudo from functioning correctly. To avoid this, always use sudo visudo to edit the file, as it performs syntax checks before saving changes.

Can regular users add themselves to the sudoers file? 

No, regular users cannot add themselves directly to the sudoers file. Only administrators with sudo privileges can modify the sudoers file and add or remove users.

Conclusion

On Ubuntu, granting a user access to sudo is as easy as adding the user to the "sudo" group.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.