sudo
is a command line program that allows trusted users to run commands as root or other users.
We will show two ways to escalate sudo privileges for users. The first way is to add users to the sudoers file. This file contains the following information:
The second option is to add the user to the sudo user group in the sudoers
file. By default, Debian-based distributions, such as Ubuntu, Linux Mint, and members of the sudo user group have sudo permissions.
On Ubuntu, the easiest way to grant sudo permissions to a user is to add the user to the "sudo" user group. Members of this group can execute any command as root, and use the sudo
command to authenticate with their own password when prompted.
We assume that the user already exists, if you want to create a new user, check this Guide
To add a user to the user group, run the following command as root or other sudo user. Make sure you replace "username" with the username you want to authorize:
usermod -aG sudo username
Authorizing sudo permissions in this way is effective for most user scenarios.
To ensure that the user has sudo permissions, run the whoami
command:
sudo whoami
You will be prompted for a password. If the user has sudo privileges, this command will print "root":
root
If you get any error message: "user is not in the sudoers file", it means this user does not have sudo permission.
The sudo permissions of users and user groups are defined in the file /etc/sudoers
. Adding a user to this file allows you to customize access commands and configure custom security policies.
You can configure the user's sudo access rights by modifying the sudoers file or creating a configuration file in the /etc/sudoers.d
directory. All files in the directory will be included in the sudoers file.
Always use visudo
to edit the /etc/sudoers
file. This command will detect the syntax error of the file when saving the file. If there are any errors, the file will not be saved. If you use a text editor to edit this file, a syntax error may cause you to lose sudo access.
Usually, visudo
uses vim to open the /etc/sudoers
file. If you haven't used Vim, and you want to use nano
as the default editor to edit this file, run:
EDITOR=nano visudo
For example, you want to allow users to run sudo commands without prompting for a password. To do this, you can open the /etc/sudoers
file:
visudo
Scroll to the end of the file and add the following:
username ALL=(ALL) NOPASSWD:ALL
Save the file and exit the editor. Don't forget to change "username" to the username you want to authorize.
Another typical example is to only allow users to run specified commands through sudo. For example, to allow only mkdir
and rmdir
to run, you can do this: /etc/sudoers
username ALL=(ALL) NOPASSWD:/bin/mkdir,/bin/rmdir
Instead of editing the sudo file directly, you can also accomplish this by creating the same file in the /etc/sudoers.d
folder. Add the same rules as added to the sudoer file:
echo "username ALL=(ALL) NOPASSWD:ALL"| sudo tee /etc/sudoers.d/username
This approach will make sudo authority management better maintained. The name of the file is not important. The usual practice is that the file name is the same as the user name.
Authorizing users with sudo permissions on Ubuntu is very simple, you only need to add users to the "sudo" user group.
Recommended Posts