How to Add User to a Sudoers in Debian
In Linux, a user who has been granted the ability to execute commands with superuser privileges is known as a “sudoers”. This allows them to perform tasks that require elevated privileges, such as installing software, modifying system configuration, and managing system services. To become a sudoers, a user must typically be added to the “sudo” group or be listed in the /etc/sudoers file. When executing a command with sudo, the user is prompted for their password to confirm their identity and authorize the command. This post discusses possible methods to add a user to sudoers in Debian (one of the most used Linux-based operating systems).
Approach #1: Using the chmod CommandNow, what practically does adding a user to sudoers means? We have a user named “linuxgenie”. Let’s update the system using this user’s account.
The output has thrown an error that the user is not in the sudoers group, which means the “linuxgenie” cannot use sudo. Simple procedural steps are provided below to add this user to the sudo group. Step 1: The “chmod” command with the following syntax is used to add a user to a sudo group.
$ sudo apt update
The “usermod” is the commands keyword, while the “a” shows the add, and the “G” represents the group. After that, the group name comes (sudo in this case) and the username. The username is “linuxgenie” in our case, so the command below will add it to the sudoers group.
$ usermod -aG sudo <username>
Note: Ensure that the command is run as a root user or user with sudo privileges. Step 2: Once the user is added, test the addition by running a sudo command through the “linuxgenie” user. Before that, make sure you are switched to a user account for which you are doing the testing, and in this case, it is “linuxgenie”. Here, we are upgrading the packages list.
$ sudo usermod -aG sudo linuxgenie
The command is executed, which shows the user has been successfully added to the group.
$ sudo apt upgrade
Approach #2: Through the “/etc/sudoers” FileThe “/etc/sudoers” file also contains the users that have the sudo privileges. A user can directly be added to it to have sudo privileges. We have a user named “genieuser”, which will be added to this file. Step 1: Open the file with editing privileges with the following command.
Step 2: Now, add a line with the following syntax at the end of the file.
$ sudo nano /etc/sudoers
In our case, the username is “genieuser”, and the added line is shown in the screenshot below. Once done, save the file, and the “genieuser” can now be used with the sudo keyword.
$ username ALL=(ALL: ALL) ALL