Sudo permissions allow users to execute commands with elevated privileges. These permissions are set in the sudoers file to prevent accidental misuse of commands or to allow users to execute administrative commands. This article shows how to add users to sudoers
.
The sudoers file is located in the directory /etc/sudoers
or in the /etc/sudoers.d/
.
- Become root by running
su
. - Run the following command, replacing
username
with the actual username:
usermod -aG wheel username
- Log out and back in again.
- Become root by running
su
. - Run the following command, replacing
username
with the actual username:
usermod -aG sudo username
- Log out and back in again.
- On Debian you can verify if the user was added:
getent group sudo
The command usermod
consists of the following components:
-
usermod
command modifies a user account. -
-aG
is an option that adds the user to a specific group. The-a
option adds a user to the group. It does that without removing them from the current groups. The-G
option is used to state where to add the existing user. -
wheel
orsudo
are the groups that are appended to the options mentioned above. -
username
is the user account you want to add to the sudo group.
This is a group configuration and it consists of adding users to the existing sudo
group, promoting simplicity and ease of management.
- Edit the
sudoers
file using thevisudo
command:
sudo visudo
This command ensures that you are editing the sudoers file with the appropriate syntax checking.
- Locate the section that begins with
%sudo
or%admin
. This is the section where you grant sudo privileges to members of a specific group.
#User privilege specification
root ALL=(ALL:ALL) ALL
%sudo ALL=(ALL:ALL) ALL
- Add the following line to the sudoers file, replacing username with the actual username:
username ALL=(ALL:ALL) ALL
- Save the changes and exit the editor. After that, you need to validate the syntax of the sudoers file. Use the following command:
sudo visudo -c
The command checks for syntax errors and provides feedback if any issues are detected.
This method is User-Specific Configuration
: for more granular control, especially in environments with a limited number of users requiring elevated privileges, specifying privileges for individual users can be preferred.
Attention: Never use this for real servers since it's considered a security risk. However, for developers running a RHEL VM on your machine, this is a reasonable thing to do.
You can configure sudo
to not ask for a password.
The default /etc/sudoers
file contains two lines for group wheel
; the NOPASSWD:
line is commented out. Uncomment that line and comment out the wheel line without NOPASSWD
. For example:
## Allows people in group wheel to run all commands
# %wheel ALL=(ALL) ALL
## Same thing without a password
%wheel ALL=(ALL) NOPASSWD: ALL