Users need root or administrative privileges to install new software on a system through the command line. When a new user creates an on a Linux system, you grant some Sudo privileges to run administrative commands. The ‘sudo’ command access allows trusted users to execute the administrative commands as another user. So, you can grant some specific privileges and add these trusted users in a sudoers group or a file.
In this article, we will explain a method of how to add a user to sudoers using the terminal on the Debian 10 system.
Method 1: Add User to a Sudoers Group on Debian 10
The most frequently used method to assign sudo privileges to a user is to add a user into the sudoers group. The members of the sudoer group can run all administrative commands as root. When a command runs with sudo privileges, a password confirmation prompt shows on the terminal. The sudo user authenticates this process using the password.
You can add an existing user into the sudoers group or if you want then create a new user and add this user into the sudoers group. Here, we assume that a user is already created. So, add this already existing user into the sudoers group by using the below-mentioned syntax:
$ usermod -aG sudo user_name
In the above command, sudo is the group name and user_name is the name of the user. For example, we are adding a user named ‘karim’ into the sudoers group. Therefore, the above command will change into the following form:
$ usermod -aG sudo karim
The above-explained method is enough for granting sudo privileges to a user. To verify that the user is now added into the sudoers group, type the following terminal command:
$ sudo whoami
Enter the password. If the user is added into the sudoers group then, the ‘root’ will display in the result. Otherwise, it will display an error on the terminal screen.
Method 2: Add user into the sudoers file
All sudo’ and group’ rules are defined into the sudoers file that you can locate at
‘/etc/sudoers’ location in your system. In this file, we define certain rules and set of the privileges which we assigned to a particular user.
Using two different approaches you can grant access to a sudo user. Either, you can configure the ‘/etc/sudoers’ file on your system and edit it or you can create a new file in the ‘/etc/sudoers.d’ directory. To modify the sudoers file, type the below-mentioned command:
$ sudo visudo
Now, at the end of the configuration file add the following rules as follows:
user_name ALL=(ALL) NOPASSWD:ALL
Change the user name with your user which is shown in the following screenshot:
Allow Limiting command access to sudo user
You can also give some limiting sudo privileges to a user. For example, we want to grant specific privileges of ‘useradd’ and ‘adduser’ commands to the user ‘karim’.
karim ALL=(ALL) NOPASSWD: /usr/sbin/useradd,/usr/sbin/adduser
Save the above-explained rules at the end of the sudoers file. After that, the user can only run the ‘adduser’ and ‘useradd’ commands.
You can also create a new file in the ‘/etc/sudoers.d’ directory and define some set of rules.
We have explained how to add users into the sudoers group in this article. Moreover, we have also explained how to edit rules in the sudoers configuration file through the command. You can customize the sudoers file based on the user requirements.