The essentials

Quick reference

One focused task per row. Jump to the related section for complete, working examples.

UseSyntaxExamples
List your privilegessudo -lView examples
List detailed privilegessudo -llView examples
Inspect another user's policysudo -U releasebot -llView examples
Validate the complete policysudo visudo --checkView examples
Parse one policy fragmentsudo visudo --check \ --file=/etc/sudoers.d/release-operatorsView examples
Safely edit a policy fragmentsudo visudo --file=/etc/sudoers.d/release-operatorsView examples
Define an operator aliasUser_Alias RELEASE_OPERATORS = %release-operatorsView examples
Define an exact command aliasCmnd_Alias API_STATUS = /usr/bin/systemctl --no-pager \ status api.serviceView examples
Grant a read-only service checkRELEASE_OPERATORS ALL = (root) API_STATUSView examples
Grant one noninteractive actionreleasebot ALL = (root) NOPASSWD: /usr/bin/systemctl \ restart api.serviceView examples
Invalidate your timestampsudo -kView examples
Set a controlled command pathDefaults:%release-operators \ secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"View examples
Enable I/O logging for a groupDefaults:%release-operators log_input,log_outputView examples
List recorded sessionssudo sudoreplay --listView examples
Test one exact authorizationsudo -U releasebot -l /usr/bin/systemctl restart \ api.serviceView examples

sudoers is executable security policy: a small matching mistake can grant a root-capable escape path or lock administrators out. Delegate the narrowest exact command and run-as identity that satisfies the operational task, keep privileged programs and their configuration outside the delegate's write control, and treat NOPASSWD, SETENV, shells, editors, pagers, package managers, and unrestricted wildcards as high-risk. Edit with visudo, validate the complete include graph, inspect the effective policy, and retain an independently tested recovery session for every rollout.

Step by step

Detailed examples

01

Inspect effective privileges before and after a change

sudoers combines user, host, run-as, command, tag, and Defaults matches across the main file and included policy sources. Group membership and external directory services can also affect the result. Use sudo -l as the target user where possible; inspecting another user with -U requires authorization. Detailed -ll output is especially useful on newer sudo releases because it can identify the source entry that matched.

Read-only effective-policy review
sudo -l
sudo -ll
# An authorized administrator can inspect the automation identity:
# sudo -U releasebot -ll
Back to quick reference ↑
02

Edit with locking and validate the whole include graph

visudo locks policy during interactive editing and refuses to install syntax it considers invalid. Check mode returns zero only on success. A selected file check is useful while developing a fragment, but the final gate must validate the default policy and all includes, including ownership and mode checks. Keep a known-good root session open, deploy atomically through configuration management, and never choose visudo's force-save path after a reported syntax error.

Fragment edit and whole-policy validation workflow
sudo visudo --file=/etc/sudoers.d/release-operators
sudo visudo --check --file=/etc/sudoers.d/release-operators
sudo visudo --check
Back to quick reference ↑
03

Bind an exact task to an exact run-as identity

A privilege specification should name who may run which absolute executable, on which host, and as which target identity. Restrict arguments whenever the program has unrelated subcommands or flags. Exact sudoers argument matching includes whitespace and metacharacter rules, so verify the result with sudo -l. The service unit, executable, working directories, configuration, and any files it loads must not be writable by the delegate; otherwise a narrow restart permission can become arbitrary root execution.

Narrow service-observation policy
User_Alias RELEASE_OPERATORS = %release-operators
Cmnd_Alias API_STATUS = /usr/bin/systemctl --no-pager status api.service
RELEASE_OPERATORS ALL = (root) API_STATUS
Back to quick reference ↑
04

Use NOPASSWD only for a controlled automation identity

By default, sudo authenticates the invoking user and caches a credential timestamp according to policy. NOPASSWD removes that interactive barrier; it does not make a command intrinsically safe. Reserve it for noninteractive identities with constrained login, narrowly writable inputs, exact commands, monitoring, and revocation. Operators can use sudo -k to invalidate their cached timestamp. Avoid granting general shells or ALL merely to make an automation workflow convenient.

Constrained automation rule
# releasebot must not be able to modify api.service, its executable, or loaded configuration.
releasebot ALL = (root) NOPASSWD: /usr/bin/systemctl restart api.service
# Interactive operators can invalidate their own cached credential with: sudo -k
Back to quick reference ↑
05

Treat environment control and program escapes as privilege

SETENV and unsafe env_keep entries can let a caller influence library loading, interpreters, configuration, or child commands. Use env_reset and a controlled secure_path, and pass only task-specific variables proven safe. Editors, pagers, shells, interpreters, debuggers, archive tools, and package managers commonly provide command escapes. NOEXEC can reduce some dynamically linked child execution but is not a security boundary and can be bypassed; prefer a purpose-built root helper with strict input validation when an application is too powerful to delegate directly.

Conservative group-scoped defaults
Defaults:%release-operators env_reset
Defaults:%release-operators secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Do not preserve loader, interpreter, pager, or editor variables for privileged commands.
Back to quick reference ↑
06

Log delegated activity without mistaking logs for prevention

sudo logs policy decisions, and optional I/O logging records terminal streams for later sudoreplay review. Input logs may capture passwords, tokens, personal data, or regulated content, so restrict access, retention, forwarding, and storage before enabling them. Logging supports detection and accountability but does not neutralize an overpowered command. Alert on denials and unusual targets, review effective privileges periodically, and remove stale delegation promptly.

I/O logging policy and inventory
# Policy line: Defaults:%release-operators log_input,log_output
sudo sudoreplay --list
sudo visudo --check
Back to quick reference ↑
07

Avoid blacklist-shaped policy and rehearse rollback

A broad grant followed by exclusions such as ALL except a named shell is unsafe: users can often reach equivalent interpreters, command escapes, writable scripts, or alternate paths. Wildcards in command arguments are also easy to overmatch because they can span whitespace. Build an allowlist from the operational procedure, test allowed and denied invocations using the exact packaged sudo version, validate all includes, and roll out with a preserved administrative session and a version-controlled known-good policy.

Production rollout checklist
1. Review the allowlist, run-as target, arguments, writable inputs, and escape surface.
2. Parse the fragment, then validate the complete policy with visudo --check.
3. Verify allowed and denied cases with sudo -l using the target identity.
4. Keep independent recovery access until the deployed policy is proven.
Back to quick reference ↑

Sources and further reading

References

Authoritative documentation used to verify and expand this cheat sheet.

  1. Sudo ProjectSudoers Manualsudo.ws
  2. Sudo ProjectVisudo Manualsudo.ws
  3. Sudo ProjectSudo Manualsudo.ws
  4. Sudo ProjectSudoreplay Manualsudo.ws
  5. Sudo ProjectSudo source repositorygithub.com

Help us improve

Found a typo or missing example?

Tell us what would make this cheat sheet clearer, more complete, or more useful.

Share feedback