Sep 1, 2023 4 min read

How to Add Users to Sudoers on Debian

Add Users to Sudoers on Debian with our step-by-step tutorial. It is a powerful command often used on Linux and Unix-based operating systems.

Add Users to Sudoers on Debian
Table of Contents

Introduction

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

Sudo is a powerful command often used on Linux and Unix-based operating systems. It provides a secure way to carry out system-related actions without needing to log in as the root user, which could pose security risks.

Sudo is commonly used to execute commands that require root access, such as installing software, modifying system settings, or managing files. Understanding how to use sudo effectively can help users maintain system security and perform administrative tasks efficiently.

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

Advantages of Sudo

  1. Enhanced security: Sudo improves security by limiting the use of administrative privileges to specific actions, reducing the risk of unauthorized access.
  2. Accountability: Sudo logs all commands executed, allowing administrators to track and audit user activities.
  3. Fine-grained control: Sudo provides granular control over which users can execute specific commands or access certain files.
  4. Simplified administration: Sudo centralizes privilege management, simplifying administrative tasks and reducing the reliance on the root account.
  5. Ease of use: Sudo allows users to perform privileged actions without needing to switch accounts, making it convenient and user-friendly.

Adding User to the sudo Group

Adding a user to the "sudo" group is the quickest and easiest way to grant them sudo privileges. Members of this group can run any command as root using sudo and will be prompted to enter their password when doing so.

We'll assume the user you want to add to the group already exists.

To add a user to the sudo group, run the command below as root or another sudo user:

usermod -aG sudo username
💡
If you got an error like usermod: command not found then to fix this add this command export PATH=$PATH:/usr/sbin to the end of your .bashrc or .zshrc file and reload your shell.

Make sure to replace "username" with the name of the user whose access you want to grant.

Type the below command to confirm the user has been added to the group:

sudo whoami

The password entry prompt will appear. The command will print "root" if the user has sudo access. If not, you will receive a message that the user "is not in the sudoers file."

Adding User to the sudoers File

The sudo privileges of users and groups are defined in the  /etc/sudoers  file. This file allows you to customize command access and set custom security policies.

Edit the sudoers file or create a new configuration file in the /etc/sudoers.d directory to configure user access. The sudoers file includes the files in this directory.

To edit the /etc/sudoers file, always use the visudo command. When you save a file, this command checks it for syntax errors. The file is not saved if there are any errors. If you use a regular text editor to edit the file, a syntax error may result in the loss of sudo access.

The EDITOR environment variable, which is by default set to vim, tells visudo which editor to use.. Change the variable by running the following command, if you want to edit the file with nano:

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:

username  ALL=(ALL) NOPASSWD:ALL

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

Another common example is allowing the user to use sudo to run only specific commands. To allow only the mkdir and rmdir commands, for example, use:

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

Instead of editing the sudoers file, you can accomplish the same thing by creating a new file in the /etc/sudoers.d directory with the authorization rules. Add the following rule, just as you would in the sudoers file:

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

This approach makes sudo privilege management more manageable. The file name is unimportant, but it is common practice to name the file after the username.

FAQs to Add users to Sudoers on Debian

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

Yes, you can add multiple users to the sudoers file in Debian. Simply separate each username with a comma when specifying the users in the sudoers file.

How can I add a user to the sudo group in Debian?

To add a user to the sudo group in Debian, use the command: "sudo adduser username sudo". Replace "username" with the relevant user's name. This command grants the user sudo privileges.

Is there any graphical tool available to add users to sudoers in Debian?

 Yes, Debian offers a graphical tool called "Users and Groups" that allows you to manage user privileges. Open the tool, select the desired user, click "Properties", navigate to the "User Privileges" tab, and enable the "Can execute system administration tasks" option.

How can I verify if a user has sudo access in Debian? 

To verify if a user has sudo access in Debian, run the command: "sudo -l -U username", replacing "username" with the specific user's name. It will display the user's sudo privileges and allowed commands.

Can I remove sudo access for a specific user in Debian? 

Yes, to remove sudo access for a specific user in Debian, use the command: "sudo deluser username sudo". Replace "username" with the relevant user's name. This command removes the user from the sudo group.

What if I accidentally lock myself out of sudo access in Debian? 

If you accidentally lock yourself out of sudo access, reboot your system into recovery mode. Select the "root" option to gain a root shell, then modify the sudoers file to restore your access.

Can I give passwordless sudo access to a user in Debian? 

Yes, you can give passwordless sudo access to a user in Debian. Edit the sudoers file using "sudo visudo" and add the line: "username ALL=(ALL) NOPASSWD: ALL", replacing "username" with the relevant user's name. This allows the user to execute sudo commands without entering a password.

Conclusion

To give a user sudo access in Debian, simply add 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.