Sudo stands for superuser do. It is a command used in Unix-like systems that allows a regular user to temporarily execute a program with root privileges.
This option gives you administrator-level permissions to run programs on your machine. It is an alternative to using the
su command. However, bear in mind there are significant differences between sudo and su.
This guide shows you how to create a user with sudo privileges, add a user to a sudo group, and verify sudo access on Debian.
Creating a Debian Sudo User
Creating a Debian sudo user involves a few simple steps. This procedure does not require you to edit the sudoers file. If you have an existing user that you want to grant sudo privileges, skip Step 2.
Step 1: Log in as the Root User
Before you can add a user to your system, log in to your server as the root user:
Replace ip_address with the IP address of your server.
Step 2: Add a New User in Debian
As the root user, create a new user with the adduser command. Append the desired user account name to the command:
For example, we added a new account sudo_user with the command:
The output looks like this:
The sudo command creates a home directory for the user and copies the necessary files. To complete the process, enter the password for the user account and retype to confirm it.
Remember that setting a strong password is extremely important for accounts with sudo access.
The terminal also prompts you to change the user information. Fill in the details or hit Enter to leave the fields blank.
Step 3: Add User to the Sudo Group
Users with root privileges can add any account to the sudo group. On Debian and Ubuntu systems, everyone in this group automatically gets sudo access.
Run the following command to add a user to the sudo group:
usermod -aG sudo username
The command consists of the following parts:
- usermod is the tool that modifies a user account.
- -aG is the option that tells the command to add the user to a specific group. The -a option adds a user to the group without removing it from current groups. The -G option states the group where to add the user. In this case, these two options always go together.
- sudo is the group we append to the above options. In this case, it is sudo, but it can be any other group.
- username is the name of the user account you want to add to the sudo group.
To verify the new Debian sudo user was added to the group, run the command:
getent group sudo
The output lists all users in the group.
Verify Sudo Access in Debian
To make sure the new user has sudo privileges:
1. Switch to the user account you just created by running the following command (and replacing username with the name of your user):
su - username
2. Run any command that requires superuser access. For example,
sudo whoami should tell you that you are the root.
To run a command with root access, type in sudo and enter the desired command.
For example, to view details for the root directory, run the
ls tool as:
sudo ls -la /root
Enter the user’s password, and the terminal shows the contents of the root directory. You only need to enter the password once in the same session.
Now you know how to add a user with sudo privileges on Debian.
By following this guide, you can use the new account to run commands as a superuser. Remember to be careful when you execute sudo commands.