Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Special Files and Directories

The /etc/sudoers file

Reference for /etc/sudoers: user specification syntax, aliases, NOPASSWD, Defaults, includedir drop-ins, and how to edit safely with visudo.

/etc/sudoers is the policy file read by sudo. It answers four questions: who (a user or group) may run which commands, as which target user, on which host, and whether a password is required. If no rule allows the request, sudo refuses and logs the attempt.

A syntax error in this file can lock every administrator out, so it must only be edited with visudo, which opens an editor, checks the syntax on save and refuses to install a broken file. Local additions belong in separate files under /etc/sudoers.d/, each checked with visudo -cf FILE.

Terminal
who  where = (as_whom)  [TAGS:]  commands

Try it

The sandbox has no sudo, visudo or root, so these examples are read-only for the real tool. The second one splits a rule into its parts with Bash.

Bash Looking for the sudo setup (read-only on real systems)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Parsing user rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

A user rule has the form WHO HOSTS=(RUNAS) TAGS: COMMANDS.

Part Description
WHO A user name, %group, #uid, +netgroup or a User_Alias.
HOSTS Host name, ALL or a Host_Alias. Useful when one file is shared by many machines.
(RUNAS) Target user, optionally :group. (ALL:ALL) means any user and any group. A bare (root) limits it to root.
TAGS NOPASSWD:, PASSWD:, SETENV:, NOEXEC: and others, applying to the commands that follow.
COMMANDS Comma-separated full paths, optionally with fixed arguments, a directory ending in /, or ALL. ! negates.

Other lines: Defaults option or Defaults:user option to set behavior (env_reset, timestamp_timeout=5, logfile=...); User_Alias, Runas_Alias, Host_Alias and Cmnd_Alias to name lists; @includedir /etc/sudoers.d (older files write #includedir, which is not a comment) to load drop-ins.

Edge cases: the last matching rule wins, so put broad rules first and exceptions later. Files in sudoers.d whose name contains a . or ends with ~ are skipped, and they must be mode 0440.

Usage notes

  • Specify full paths and exact arguments. /usr/bin/systemctl restart nginx allows only that; /usr/bin/systemctl * allows far more than it seems because a wildcard also matches spaces and extra arguments.
  • Do not allow programs that start shells (editors, less, find, vim): they let the user escape to a root shell. Use sudoedit for file editing.
  • NOPASSWD is convenient for automation but means a stolen session is a root session. Limit it to specific commands.
  • Groups: on Debian and Ubuntu the administrators are in sudo, on Fedora, RHEL and Arch in wheel.
  • Never edit with a plain editor. A mistake gives parse error and sudo stops working for everyone. Keep a root shell open while you test changes.
  • Logging: each use is logged (/var/log/auth.log or the journal), which is a main reason to prefer sudo over sharing the root password.
  • Comments: # starts a comment, except in #include, #includedir and #uid forms.