Granting permissions to users for accessing Linux systems like Debian 12 must be done with care as giving unnecessary permissions can compromise the system’s security. On Debian and other Linux systems, the users can be segregated based on different groups. The group with administrator rights is named as sudo group in most of the Linux distributions. The users in this group are allowed to execute every type of command as they have full access to it.
How To Add Users in Sudoers File in Debian 12
The sudoers file is the configuration file for the users having admin rights. It controls the behavior of the sudo command, which allows authorized users to run commands with administrative privileges. On Debian when you create a new user and execute any command using sudo you will probably get an error the username is not in sudoers file:
To grant a user admin rights or to add it to the sudoers file, this guide will explain four ways. To follow any of the methods mentioned below other than the GUI you need to login to the administrative account via the terminal:
su -
1: Manually Assigning Permissions in Debian 12
The first method to add a user in the sudoers file is by manually opening it in the editor and adding the permission for execution of all types of commands:
[user-name] ALL=(ALL:ALL) ALL
After you have added the permissions for the respective user save the sudoers file and then exit the root account. To verify try execution of the apt package update using the sudo command:
Furthermore, you can verify the permissions by listing information about the current user’s sudo permissions according to the sudoers file:
sudo -l
2: Through usermod Command in Debian 12
Another way to add a user in the sudoers file in Debian 12 is by using the usermod command which is primarily used for editing any user data or permissions. On most Linux distributions, the sudo group is the one having admin privileges, so use the usermod command to add the respective user to the sudo group:
usermod -aG sudo [user-name]
To verify if the user is added to the sudoer file list the users added to the sudo group:
groups [user-name]
3: Through adduser Command in Debian 12
To add a user or to any group or to create a new user in Debian normally the adduser command is executed. A user in Debian can be added to the sudoers file by using the adduser command:
adduser [user-name] sudo
Further to check if the user is added successfully use the getent utility for listing users present in the sudo group:
getent group sudo
4: Through Debian User Settings
If you do not prefer to use commands for adding a user in the sudoers file then you can do it through user settings in Debian. This method is recommended for users who are new to the Debian system and are not very familiar with command execution. Simply navigate to user settings and then unlock them by entering the authentication password:
Next, select the user to whom you want to add to the sudoers file and then turn on the administrator option:
Keep in mind that if there are multiple users on your system then the user you want to add to the sudoers file must be logged out.
Conclusion
When a non-sudo user tries to execute a command using sudo in Debian the error of username is not in sudoers file. To add a user in the sudoers file in Debian 12 there are four ways which include adding the permissions manually in the sudoers file, using the usermod command, using adduser command, or using the system user settings. The easiest way to add a user in the sudoer file is by using the adduser command. Remember to switch to the root account before adding the user to the sudoers file.