How to Add a User to Sudoers on Debian

After completing the installation, Debian users would soon find out that the sudo command set up during the initial installation on other operating systems such as Ubuntu does not work to the shock of new users new to Debian. This is partly for a security measure due to the nature of the sudo account having security privileges. Debian believes you should be setting this up manually to harden your system. For those unsure what sudo is, this command allows you to run programs with the security privileges of another user, which is primarily the root account.

Adding your existing username and adding anyone else that needs access to privilege commands is a straightforward process with a few commands.

In the following tutorial, you will learn to add a user to the sudoers group on any Debian system.

Prerequisites

  • Recommended OS: Debian 11 Bullseye (Debian Unstable, Testing, 10 or lower will work also)
  • User account: root access

Updating Operating System

First, before anything, update your Debian operating system to make sure all existing packages are up to date:

sudo apt update && sudo apt full-upgrade -y

Install Sudoers

In the event you have removed sudoers, or it is missing, reinstall the package with the below command:

sudo apt install sudo

Advertisement


Changing to Root (su)

To create new sudo users, you need to login into the Debian root account. When you initially installed Debian, the installation required you to set a root password. You will need this to continue:

To change to root, use the following command su:

su

You will be prompted for the root password. Once entered, you will see the username has changed to root.

How to Add a User to Sudoers on Debian

Create and User Account on Debian

The first step is learning how to add a new user account. Ideally, you can grant permission to an existing account, but you will learn to add a user from scratch for the tutorial.

First, create the user account <example username>, replacing the example with the user name you want to add.

sudo adduser <example username>

Example:

sudo adduser josh

After typing this command and pressing the enter key, a prompt will appear for the new user’s password and require you to confirm it. Remember, for anyone with sudoers access, and a powerful password should be used with uppercase, lowercase, symbols, numeric value, and special characters.

Example of prompt:

How to Add a User to Sudoers on Debian

Now that you have set the password, you will be prompted for additional information regarding the username you are adding. Fill out the details as best as possible, and you can skip the details if you prefer by pressing the enter key.

Example of prompt:

How to Add a User to Sudoers on Debian

Once done, type Y and press the enter key to proceed.

Confirm the user has been added by running the following command:

cat /etc/passwd

Example output:

How to Add a User to Sudoers on Debian

For future reference, the information about the new and existing users is stored in the file /etc/password.


Advertisement


Add New User To Sudoers Group on Debian

In the next part of the tutorial, now that you have learned how to add a user, you can give the new user that you named or an existing username sudoers access by typing the following command:

sudo usermod -aG sudo <example username>

Example using our name we created:

sudo usermod -aG sudo josh

It is a good idea to check if the username was successfully added to the sudoers group. To do this, run the id command:

id <username>

Example using our name we created:

id josh

Example output:

How to Add a User to Sudoers on Debian

An alternative is to use the gpasswd command as follows:

gpasswd -a <example username> sudo

Example using our name we created:

gpasswd -a josh sudo

Example output:

adding josh to group sudo

Confirm & Test New Sudo User

Now that you have successfully added the user you wanted to have sudo access to, it is time to test the account. This can be done again by using the su command but by adding the username along with the command.

Login to sudo user as follows:

su <example username>

Example using our name we created:

su josh

Now, confirm the username with the sudo command along with whoami:

sudo whoami

You will then be prompted to type in the sudo username you are using and its password:

How to Add a User to Sudoers on Debian

Once entered and confirmed, you will see the following output:

How to Add a User to Sudoers on Debian

Congratulations, you have added a new username to the sudoers group.


Advertisement


Comments and Conclusion

In the tutorial, you have learned how to add users to your Debian operating system and add sudo permissions. Overall, this tutorial should work for all Debian systems and is recommended only to give permissions to trusted users as they will have access to sensitive files with the access given by sudo.

Leave a Comment