sudo is the mechanism that gates privilege escalation on every Linux host. Used carefully, it gives you fine-grained, auditable access: this user can restart this specific service on this specific host, full stop. Used carelessly — typically a single %sudo ALL=(ALL:ALL) NOPASSWD: ALL — it is a single keystroke from any operator’s shell to root. This article documents the operational patterns we apply across the fleet.
How to verify
Inventory who can do what right now.
sudo -ll # list rules for current user (long form)
sudo -lU alice # list rules that apply to alice
ls -la /etc/sudoers.d/
sudo grep -rE '^[^#]' /etc/sudoers /etc/sudoers.d/
grep -E '^(%sudo|%wheel|%admin)' /etc/sudoers
sudo -lU <user> is the single most useful query: it prints exactly what that user is allowed to run, with which target user, and whether a password is required.
What’s happening
sudo reads its policy from /etc/sudoers plus every file in /etc/sudoers.d/ (the latter is enabled by an #includedir directive in /etc/sudoers). Each line in those files is either a Defaults directive (global behaviour) or a rule of the form:
who where=(target_user[:target_group]) [tag1, tag2 ...] command_list
For example:
ops ALL=(root) NOPASSWD: /bin/systemctl restart nginx, /bin/systemctl reload nginx
alice web-01,web-02 = (root) /usr/bin/journalctl
The where is the host (relevant only if you ship a fleet-wide sudoers); the target_user is whom the command will run as; NOPASSWD skips the password prompt; the command list is the literal set of binaries this rule permits.
The two operational concerns are: keeping the policy auditable (no gigantic blanket grants), and editing safely (one bad file in /etc/sudoers.d/ breaks sudo for everyone until single-user-mode rescue).
The procedure
-
Always edit with
visudo. It validates syntax before saving. For files in/etc/sudoers.d/, usevisudo -f.sudo visudo # edit /etc/sudoers sudo visudo -f /etc/sudoers.d/20-app-restartvisudoaborts the save if the file has a syntax error, which is the only thing standing between you and a broken sudo on every host. -
Keep
/etc/sudoersas the distro shipped it. Put all customization in drop-ins under/etc/sudoers.d/. The drop-in directory makes individual changes auditable and revertable without surgery on the monolithic file. -
Grant by least privilege. Resist
ALL=(ALL) NOPASSWD: ALLeven for operator accounts. The pattern we use:# /etc/sudoers.d/30-ops-base # Operators get full sudo with password %ops ALL=(ALL:ALL) ALL # /etc/sudoers.d/40-app-automation # CI deploy bot can only restart its own services, no password deploy ALL=(root) NOPASSWD: /bin/systemctl restart app.service, \ /bin/systemctl reload nginx, \ /usr/bin/journalctl -u appThe deploy bot can do exactly two things plus a log read. If the deploy account is compromised, the blast radius is narrow.
-
Use Cmnd_Alias for clarity. When the same command list appears in multiple rules, factor it.
Cmnd_Alias APP_RESTART = /bin/systemctl restart app.service, /bin/systemctl reload nginx Cmnd_Alias LOG_READ = /usr/bin/journalctl -u app, /bin/cat /var/log/app/*.log deploy ALL=(root) NOPASSWD: APP_RESTART sre ALL=(root) APP_RESTART, LOG_READEasier to audit, easier to extend.
-
Force a password prompt on every command if the account is a primary operator.
Defaultsdirective:Defaults timestamp_timeout=15 Defaults:%ops timestamp_timeout=5The default is 15 minutes — once you
sudo, you have 15 minutes of grace. For higher-sensitivity accounts, cut it to 5 minutes or 0. -
Configure sudo to log to the journal and to a dedicated file. sudo writes to
auth.log/secureby default, but a dedicated log makes audit easier.Defaults logfile=/var/log/sudo.log Defaults log_year, log_host Defaults !syslog # if you do not want syslog in addition Defaults loglinelen=0 # do not truncateThen:
sudo tail -F /var/log/sudo.log sudo journalctl -t sudo --since today -
Test before declaring victory. After editing a drop-in, verify it parses and applies.
sudo visudo -c # check syntax of all sudoers files sudo -lU deploy # show what deploy can do sudo -lU alicevisudo -cis the dry-run check. It exits non-zero if any file is invalid. -
For automation, use distinct accounts not human accounts. A CI runner that needs to restart an application should not be running as a human operator. A dedicated
deployservice account with a narrowly-scoped sudoers entry is the auditable answer. -
Audit periodically. Once a quarter (or after a personnel change), enumerate the policy across the fleet.
ansible all -m shell -a "sudo -ll 2>&1 | head -30"Any rule that grants more than the role needs is a finding; tighten it.
-
Remove rules cleanly. When an operator leaves, removing their key is half the job — removing their sudoers entry is the other half.
sudo rm /etc/sudoers.d/30-alice sudo visudo -c sudo -lU alice # should now say "not allowed"
Common pitfalls
- Editing
/etc/sudoerswithnanoorvimdirectly. One syntax error and you cannotsudoto fix it. Alwaysvisudo. - A sudoers drop-in with mode
0644or0664— sudo silently ignores it. Must be0440, ownerroot:root. NOPASSWD: ALLfor an operator account — the password prompt is your last line of defence against an unattended terminal.- Forgetting that
sudocaches authentication per terminal —sudo -Kinvalidates the cache for the current session if you need to test re-prompting. - Wildcards in command paths can be tricky:
NOPASSWD: /bin/systemctl restart *lets the user restart any service including critical ones; tighten the wildcard or list explicit services. - Adding a
Defaults env_keepfor variables likeLD_LIBRARY_PATH— opens a privilege-escalation hole. Only addenv_keepfor variables you have audited.
In our managed engagements, sudoers entries are checked into the same Git repo that holds the operator-key inventory and reviewed at the same cadence. New entries require a pull request with two approvers. The result is that a question like “who, on which host, can restart Postgres without a password” has a one-line answer pulled straight from the source of truth — that auditability is part of the Managed Operations access posture.