09. Sudo & Privileges
Logging in directly as the root user is an anti-pattern that violates the Principle of Least Privilege and leaves no auditable trail. Instead, Linux administration standardizes on sudo (Superuser Do) to delegate granular, traceable administrative rights.
09.1 The sudo Command & Execution Model
Section titled “09.1 The sudo Command & Execution Model”sudo allows an authorized user to execute a command with elevated privileges:
# Execute a command with superuser privilegessudo apt update
# Execute a command as a specific non-root user (e.g. postgres)sudo -u postgres psql
# View commands you are permitted to runsudo -l
# View allowed commands for another user (as an administrator)sudo -l -U aliceAuthentication vs Elevation
Section titled “Authentication vs Elevation”- When executing a command with
sudo, you are prompted for your own user password, not the root password. - Successful authentication grants a temporary cached credential token (default: 15 minutes) before prompting again.
- Every
sudoinvocation is logged to/var/log/auth.log(Debian/Ubuntu) or/var/log/secure(RHEL), recording the timestamp, executing user, target user, and command.
09.2 The /etc/sudoers Architecture & visudo
Section titled “09.2 The /etc/sudoers Architecture & visudo”Sudo privilege rules are governed by /etc/sudoers and modular files inside /etc/sudoers.d/.
[!CAUTION] Never edit
/etc/sudoerswith a standard text editor likenanoorvim! A single syntax error (like a missing colon or misspelled keyword) can instantly lock every administrator out ofsudo, requiring an emergency bootloader recovery session.
The visudo Safeguard
Section titled “The visudo Safeguard”Always edit sudoer rules using the visudo command:
sudo visudovisudo locks the file, opens it in your default editor, and strictly validates syntax before committing changes to disk. If an error is detected, it flags the exact line and prevents saving.
Managing Modular Files (/etc/sudoers.d/)
Section titled “Managing Modular Files (/etc/sudoers.d/)”Best practice is to leave the main /etc/sudoers untouched and create modular rules per team or role inside /etc/sudoers.d/. Validate them using:
sudo visudo -f /etc/sudoers.d/devops-team09.3 Understanding Sudoers Syntax
Section titled “09.3 Understanding Sudoers Syntax”Every rule follows a standardized four-part syntax:
[User or %Group] Host=(RunAsUser:RunAsGroup) [Tag:] Commands| Component | Meaning | Examples |
||||
| User or %Group | The entity receiving permissions (% denotes a group) | alice, %devops, %wheel |
| Host | Which machines this rule applies to | ALL (or specific hostname/IP) |
| (RunAsUser:RunAsGroup) | Who the command can be run as | (ALL:ALL), (root), (postgres) |
| Commands | Comma-separated list of allowed absolute binary paths | /usr/bin/systemctl, ALL |
User Privilege Examples
Section titled “User Privilege Examples”# Full unrestricted superuser privilegesalice ALL=(ALL:ALL) ALL
# Restrict bob to restarting and reloading the Apache service onlybob ALL=(root) /usr/bin/systemctl restart httpd, /usr/bin/systemctl reload httpd
# Allow carol to run updates without prompting for a passwordcarol ALL=(ALL) NOPASSWD: /usr/bin/apt update, /usr/bin/apt upgradeGroup Privilege Examples
Section titled “Group Privilege Examples”# Any member of the 'ops' group has full root access%ops ALL=(ALL:ALL) ALL
# Deployers can manage the nginx service, but cannot run any other admin command%deployers ALL=(root) /usr/bin/systemctl restart nginx, /usr/bin/systemctl status nginx09.4 Multilevel Tiered Access Control
Section titled “09.4 Multilevel Tiered Access Control”Enterprise environments enforce separation of duties by structuring access into operational tiers:
# Tier 1: System Administrators (Full Root Access)%wheel ALL=(ALL:ALL) ALL
# Tier 2: Service Managers (Service controls & journalctl inspection)%svcmanagers ALL=(root) /usr/bin/systemctl *, /usr/bin/journalctl
# Tier 3: Support & Log Viewers (Read-only log inspection)%logviewers ALL=(root) /usr/bin/journalctl, /usr/bin/tail -f /var/log/*Rule Evaluation Order (Top-Down)
Section titled “Rule Evaluation Order (Top-Down)”Sudo evaluates rules sequentially from top to bottom. If a user matches multiple rules, the last matching rule takes precedence. Place specific restrictions after broad allowances, or structure rules carefully to prevent unintended privileges.
09.5 Sudo Directives & Security Hardening
Section titled “09.5 Sudo Directives & Security Hardening”1. The NOPASSWD Tag
Section titled “1. The NOPASSWD Tag”Bypasses password prompts for automated accounts (CI/CD runners, monitoring daemons):
# Allow Prometheus node-exporter to read system statsprometheus ALL=(ALL) NOPASSWD: /usr/local/bin/node_exporter2. Sudo Defaults (Defaults)
Section titled “2. Sudo Defaults (Defaults)”Apply environment constraints globally or per user/group:
# Enforce secure path to prevent binary hijackingDefaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
# Require a separate authentication timestamp per terminal (prevents hijacking)Defaults timestamp_type = tty
# Adjust timeout (default 15 minutes, set to 5 minutes)Defaults timestamp_timeout = 5
# Custom defaults for specific groupsDefaults:%deployers !requiretty3. Sudoers Security Rules
Section titled “3. Sudoers Security Rules”- Always use absolute binary paths: Never write
systemctl restart nginx. An attacker could create a malicious script namedsystemctlin/tmpand prepend it to$PATH. - Never allow shells or interactive editors via sudo without restrictions: Avoid granting
sudo /bin/viorsudo /usr/bin/find, as users can easily spawn a root shell from within them (:!/bin/bashorfind -exec /bin/sh). - Protect script binaries: If a sudo rule grants access to a script (e.g.
/usr/local/bin/backup.sh), ensure the script is strictly owned byroot:rootwith permissions700or755. If standard users can edit the script, they gain root access!