Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 8. Managing sudo access
System administrators can grant sudo access to allow non-root users to execute administrative commands that are normally reserved for the root user. As a result, non-root users can execute such commands without logging in to the root user account.
8.1. User authorizations in sudoers Link kopierenLink in die Zwischenablage kopiert!
The /etc/sudoers file specifies which users can use the sudo command to execute other commands. The rules can apply to individual users and user groups. You can also define rules for groups of hosts, commands, and even users more easily by using aliases. Default aliases are defined in the first part of the /etc/sudoers file.
When a user enters a command with sudo for which the user does not have authorization, the system records a message that contains the string <username> : user NOT in sudoers to the journal log.
The default /etc/sudoers file provides information and examples of authorizations. You can activate a specific example rule by uncommenting the corresponding line. The section with user authorizations is marked with the following introduction:
## Next comes the main part: which users can run what software on ## which machines (the sudoers file can be shared between multiple ## systems).
## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
You can create new sudoers authorizations and modify existing authorizations by using the following format:
<username> <hostname.example.com>=(<run_as_user>:<run_as_group>) <path/to/command>
<username> <hostname.example.com>=(<run_as_user>:<run_as_group>) <path/to/command>
Where:
-
<username>is the user that enters the command, for example,user1. If the value starts with%, it defines a group, for example,%group1. -
<hostname.example.com>is the name of the host on which the rule applies. -
The section
(<run_as_user>:<run_as_group>)defines the user or group as which the command is executed. If you omit this section,<username>can execute the command as root. -
<path/to/command>is the complete absolute path to the command. You can also limit the user to only performing a command with specific options and arguments by adding those options after the command path. If you do not specify any options, the user can use the command with all options.
You can apply the rule to all users, hosts, or commands by replacing any of these variables with ALL.
With overly permissive rules, such as ALL ALL=(ALL) ALL, all users can run all commands as all users on all hosts. This presents serious security risks.
You can specify the arguments negatively by using the ! operator. For example, !root specifies all users except root. Note that allowing specific users, groups, and commands is more secure than disallowing specific users, groups, and commands. This is because allow rules also block new unauthorized users or groups.
Avoid using negative rules for commands because users can overcome such rules by renaming commands with the alias command.
The system reads the /etc/sudoers file from beginning to end. Therefore, if the file contains multiple entries for a user, the entries are applied in order. In case of conflicting values, the system uses the last match, even if it is not the most specific match.
To preserve the rules during system updates and for easier fixing of errors, enter new rules by creating new files in the /etc/sudoers.d/ directory instead of entering rules directly to the /etc/sudoers file. The system reads the files in the /etc/sudoers.d directory when it reaches the following line in the /etc/sudoers file:
#includedir /etc/sudoers.d
#includedir /etc/sudoers.d
Note that the number sign (#) at the beginning of this line is part of the syntax and does not mean the line is a comment. The names of files in that directory must not contain a period and must not end with a tilde (~).
8.2. Granting sudo access to a user Link kopierenLink in die Zwischenablage kopiert!
System administrators can allow non-root users to execute administrative commands by granting them sudo access. The sudo command provides users with administrative access without using the password of the root user.
When users need to perform an administrative command, they can precede that command with sudo. If the user has authorization for the command, the command is executed as if they were root.
Be aware of the following limitations:
-
Only users listed in the
/etc/sudoersconfiguration file can use thesudocommand. -
The command is executed in the shell of the user, not in the root shell. However, there are some exceptions such as when full
sudoprivileges are granted to any user. In such cases, users can switch to and run the commands in root shell. For example: -
sudo -i -
sudo su -
Prerequisites
- You have root access to the system.
Procedure
As root, open the
/etc/sudoersfile.visudo
# visudoCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
/etc/sudoersfile defines the policies applied by thesudocommand.In the
/etc/sudoersfile, find the lines that grantsudoaccess to users in the administrativewheelgroup.## Allows people in group wheel to run all commands %wheel ALL=(ALL) ALL
## Allows people in group wheel to run all commands %wheel ALL=(ALL) ALLCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Make sure the line that starts with
%wheelis not commented out with the number sign (#). - Save any changes, and exit the editor.
Add users you want to grant
sudoaccess to into the administrativewheelgroup.usermod --append -G wheel <username>
# usermod --append -G wheel <username>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace
<username>with the name of the user.
Verification
Verify that the user is in the administrative
wheelgroup:id <username>
# id <username> uid=5000(<username>) gid=5000(<username>) groups=5000(<username>),10(wheel)Copy to Clipboard Copied! Toggle word wrap Toggle overflow
8.3. Enabling unprivileged users to run certain commands Link kopierenLink in die Zwischenablage kopiert!
As an administrator, you can allow unprivileged users to enter certain commands on specific workstations by configuring a policy in the /etc/sudoers.d/ directory. This is more secure than granting full sudo access to a user or giving someone the root password for the following reasons:
- More granular control over privileged actions. You can allow a user to perform certain actions on specific hosts instead of giving them full administrative access.
-
Better logging. When a user performs an action through
sudo, the action is logged with their user name and not just root. -
Transparent control. You can set email notifications for every time the user attempts to use
sudoprivileges.
Prerequisites
- You have root access to the system.
Procedure
As root, create a new
sudoers.d/directory under/etc/:mkdir -p /etc/sudoers.d/
# mkdir -p /etc/sudoers.d/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a new file in the
/etc/sudoers.d/directory:visudo -f /etc/sudoers.d/<filename>
# visudo -f /etc/sudoers.d/<filename>Copy to Clipboard Copied! Toggle word wrap Toggle overflow The file opens automatically.
Add the following line to the
/etc/sudoers.d/<filename>file:<username> <hostname.example.com> = (<run_as_user>:<run_as_group>) <path/to/command>
<username> <hostname.example.com> = (<run_as_user>:<run_as_group>) <path/to/command>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<username>with the name of the user. -
Replace
<hostname.example.com>with the URL of the host. -
Replace
(<run_as_user>:<run_as_group>)with the user or group as which the command can be executed. If you omit this section,<username>can execute the command as root. -
Replace
<path/to/command>with the complete absolute path to the command. You can also limit the user to only performing a command with specific options and arguments by adding those options after the command path. If you do not specify any options, the user can use the command with all options. To allow two and more commands on the same host on one line, you can list them separated by a comma followed by a space.
For example, to allow
user1to execute thednfandrebootcommands onhost1.example.com, enteruser1 host1.example.com = /bin/dnf, /sbin/reboot.
-
Replace
Optional: To receive email notifications every time the user attempts to use
sudoprivileges, add the following lines to the file:Defaults mail_always Defaults mailto="<email@example.com>"
Defaults mail_always Defaults mailto="<email@example.com>"Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Save the changes, and exit the editor.
Verification
To verify if a user can run a command with
sudoprivileges, switch the account:su <username> -
# su <username> -Copy to Clipboard Copied! Toggle word wrap Toggle overflow As the user, enter the command with the
sudocommand:sudo <command>
$ sudo <command> [sudo] password for <username>:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the user’s
sudopassword.If the privileges are configured correctly, the system displays the list of commands and options. For example, with the
dnfcommand, it shows the following output:… usage: dnf [options] COMMAND …
… usage: dnf [options] COMMAND …Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the system returns the error message
<username> is not in the sudoers file. This incident will be reported, the file for<username>in/etc/sudoers.d/does not exist.If the system returns the error message
<username> is not allowed to run sudo on <host.example.com>, the configuration was not completed correctly. Ensure that you are logged in as root and that the configuration was performed correctly.If the system returns the error message
Sorry, user <username> is not allowed to execute '<path/to/command>' as root on <host.example.com>., the command is not correctly defined in the rule for the user.
8.4. Applying custom sudoers configuration by using RHEL system roles Link kopierenLink in die Zwischenablage kopiert!
You can use the sudo RHEL system role to apply custom sudoers configuration on your managed nodes. That way, you can define which users can run which commands on which hosts, with better configuration efficiency and more granular control.
Prerequisites
- You have prepared the control node and the managed nodes.
- You are logged in to the control node as a user who can run playbooks on the managed nodes.
-
The account you use to connect to the managed nodes has
sudopermissions on them.
Procedure
Create a playbook file, for example,
~/playbook.yml, with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The settings specified in the playbook include the following:
users- The list of users that the rule applies to.
hosts-
The list of hosts that the rule applies to. You can use
ALLfor all hosts. commands-
The list of commands that the rule applies to. You can use
ALLfor all commands.
For details about all variables used in the playbook, see the
/usr/share/ansible/roles/rhel-system-roles.sudo/README.mdfile on the control node.Validate the playbook syntax:
ansible-playbook --syntax-check ~/playbook.yml
$ ansible-playbook --syntax-check ~/playbook.ymlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Note that this command only validates the syntax and does not protect against a wrong but valid configuration.
Run the playbook:
ansible-playbook ~/playbook.yml
$ ansible-playbook ~/playbook.ymlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
On the managed node, verify that the playbook applied the new rules.
cat /etc/sudoers | tail -n1 <user_name> <host_name>= <path_to_command_binary>
# cat /etc/sudoers | tail -n1 <user_name> <host_name>= <path_to_command_binary>Copy to Clipboard Copied! Toggle word wrap Toggle overflow