Some commands in Linux require admin privileges that regular users might not have. In such cases, we use the sudo command to run any commands or tools as root.
Debian and its derivates usually grant members of the sudo group sudo access. In this article, we’re going over how you can add users to the sudoers files to give them root access.
Also read: How to install Kali Linux on Virtualbox?
How to add a user to the Sudo group?
A rather quick and easy way of giving a user on the machine root access is to add the user to the ‘sudo’ group instead of adding them to the sudoers file.
All you have to do is run the following command.
usermod -aG sudo username
In the above command, username is the name of the user you want to add to the sudo group. For most use cases, granting sudo access using this method would be sufficient.
To check if the user has been added to the group, use this command,
You’ll be prompted for your password after you hit enter. If the user running the command has sudo access, the command will print ‘root’ otherwise, you’ll get an error saying the user isn’t in the sudoers file.
Also read: How to remove a Directory in Linux?
How to add a user to the sudoers file?
Now for the main attraction, you can configure user access by editing the sudoers file or by creating a new configuration in the /etc/sudoers.d directory.
We’ll be using the visudo command to edit the sudoers file. This command ensures that there are no syntax errors. If there are, the file isn’t saved. You can modify the file with a regular text editor as well, but if you make a mistake, you can end up losing sudo access yourself.
By default, visudo uses the editor specified by the EDITOR environment variable, which is vim. If you’re like to use the file with some other editor, nano, for example, you can change this variable by using the following command.
Now you can configure user access in the editor of your choice. Let’s say you want to run sudo commands without entering a password every time; here’s how to do that.
Step 1: Type the following command in the terminal and press enter.
Step 2: Scroll to the end of the file and append the following line, replacing username with the name of the user.
username ALL=(ALL) NOPASSWD:ALL
Save the file and quit the editor. You can now use sudo commands without having to enter your password.
Another thing that you can do is to allow a user to run only specified commands via sudo. Here’s how.
username ALL=(ALL) NOPASSWD:/bin/mkdir, /bin/rm
The above command will only let the specified user run the mkdir and rm commands via sudo.
Instead of editing the sudoers file, you can make your job more manageable by creating a separate file containing these rules. Aforementioned, this file needs to be placed in the /etc/sudoers.d directory.
echo "username ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/username
The above command will let the username (replace with the user’s name) run all commands without a password. Since it’s a separate file now, you can easily modify this user’s sudo access at any time.
Also read: Mint vs Ubuntu: Linux distro comparison