Skip to content

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.

sudo allows an authorized user to execute a command with elevated privileges:

Terminal window
# Execute a command with superuser privileges
sudo apt update
# Execute a command as a specific non-root user (e.g. postgres)
sudo -u postgres psql
# View commands you are permitted to run
sudo -l
# View allowed commands for another user (as an administrator)
sudo -l -U alice
  • 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 sudo invocation 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/sudoers with a standard text editor like nano or vim! A single syntax error (like a missing colon or misspelled keyword) can instantly lock every administrator out of sudo, requiring an emergency bootloader recovery session.

Always edit sudoer rules using the visudo command:

Terminal window
sudo visudo

visudo 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.

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:

Terminal window
sudo visudo -f /etc/sudoers.d/devops-team

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 |

# Full unrestricted superuser privileges
alice ALL=(ALL:ALL) ALL
# Restrict bob to restarting and reloading the Apache service only
bob ALL=(root) /usr/bin/systemctl restart httpd, /usr/bin/systemctl reload httpd
# Allow carol to run updates without prompting for a password
carol ALL=(ALL) NOPASSWD: /usr/bin/apt update, /usr/bin/apt upgrade
# 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 nginx

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/*

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.

Bypasses password prompts for automated accounts (CI/CD runners, monitoring daemons):

# Allow Prometheus node-exporter to read system stats
prometheus ALL=(ALL) NOPASSWD: /usr/local/bin/node_exporter

Apply environment constraints globally or per user/group:

# Enforce secure path to prevent binary hijacking
Defaults 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 groups
Defaults:%deployers !requiretty
  1. Always use absolute binary paths: Never write systemctl restart nginx. An attacker could create a malicious script named systemctl in /tmp and prepend it to $PATH.
  2. Never allow shells or interactive editors via sudo without restrictions: Avoid granting sudo /bin/vi or sudo /usr/bin/find, as users can easily spawn a root shell from within them (:!/bin/bash or find -exec /bin/sh).
  3. 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 by root:root with permissions 700 or 755. If standard users can edit the script, they gain root access!