5.4. Delegating Administrative Access
Chapter 3 used sudo from the invoking side: one
elevated command, a cached authentication, a root shell with -i. This
section is about the other side - writing the policy that decides who may
run what. The policy lives in /etc/sudoers and the drop-ins under
/etc/sudoers.d, and it is never edited directly with a text editor: a
syntax error in a live policy file can lock every administrator out of
elevation. visudo exists to make that impossible.
5.4.1. Managing sudo Policy with visudo
The stock policy is small. Selected lines from /etc/sudoers on this
installation:
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Defaults use_pty
%sudo ALL=(ALL:ALL) ALL
@includedir /etc/sudoers.d
The %sudo line is the standard delegation on a freshly installed system:
every member of the sudo group may run every command, as every user and
group, after authenticating with their own password
(Chapter 2 showed the installer putting the first
user there). The Defaults lines shape each invocation: env_reset clears
the environment, secure_path fixes the command search path (so sudo id
finds /usr/bin/id no matter what the caller's PATH contains),
mail_badpass notifies the mail alias on failed passwords, and use_pty
gives sudo a pseudo-terminal it can log. The last line is the extension
point: drop-in files in /etc/sudoers.d are parsed after the main file,
each a fragment of the same policy.
A rule's anatomy is fixed:
who host = (runas) options: commands
| Field | Meaning | Example |
|---|---|---|
who |
User name, %group, or a User_Alias. |
%project |
host |
Host names where the rule applies. | ALL |
(runas) |
User (and group) the command may run as. | (root) |
options |
NOPASSWD, SETENV, and similar. |
NOPASSWD: |
commands |
Full command paths, or a Cmnd_Alias. |
CH05_INSPECT |
When several rules match, the last matching rule decides - and because
drop-ins are read after the main file, a drop-in refines the stock rules for
the commands it names. This is the tool for limited delegation: grant a team
a short list of commands instead of the sudo group's everything.
Build one. The example system's john account has no sudo membership
(Section 5.1.1 explained why), so the policy below is the entirety of what
john and his teammate can elevate - which makes its limits visible.
Contents of /etc/sudoers.d/ch05-ops:
Cmnd_Alias CH05_INSPECT = /usr/bin/id, /usr/bin/whoami
%project ALL=(root) NOPASSWD: CH05_INSPECT
The alias names two commands by full path; the rule lets every member of
project run them as root without a password. Create the file, fix its mode
to the policy's requirement (root-owned, not writable by others - 0440),
and validate it before relying on it:
# cat > /etc/sudoers.d/ch05-ops <<'EOF'
Cmnd_Alias CH05_INSPECT = /usr/bin/id, /usr/bin/whoami
%project ALL=(root) NOPASSWD: CH05_INSPECT
EOF
The here-document writes the file and prints nothing. Then:
# chmod 0440 /etc/sudoers.d/ch05-ops
# visudo -cf /etc/sudoers.d/ch05-ops
/etc/sudoers.d/ch05-ops: parsed OK
visudo -cf parses the named file without installing anything: it reports
parsed OK, or the syntax error with its line and column. A full audit of
the installed policy uses -c alone, which checks the main file and every
drop-in:
# visudo -c
/etc/sudoers: parsed OK
/etc/sudoers.d/README: parsed OK
/etc/sudoers.d/ch05-ops: parsed OK
/etc/sudoers.d/vagrant: parsed OK
Every fragment parsed. (The vagrant drop-in is this example guest's
provisioning policy - a prebuilt-image artifact of the kind
Chapter 3 warned about, not a reader baseline.) From
john's ordinary shell, the delegation is now usable without any password:
$ sudo -n id
uid=0(root) gid=0(root) groups=0(root)
$ sudo -n whoami
root
-n never prompts - it succeeds when a NOPASSWD rule matches and fails
otherwise, which makes it both the convenient form for this policy and a
clean probe of what is allowed. The authoritative view is the listing:
$ sudo -n -l
Matching Defaults entries for john on trixie:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin, use_pty
User john may run the following commands on trixie:
(root) NOPASSWD: /usr/bin/id, /usr/bin/whoami
sudo -l prints the rules that match the invoking user, with the defaults
in force. This listing is the whole of john's elevation: two inspection
commands. On a system where john is also in the sudo group, the listing
shows that rule too, and commands outside the alias fall back to it - the
restricted rule merely takes precedence for the commands it names, because
it matches last.
NOPASSWD deserves a deliberate choice, not a habit. Without it, sudo
authenticates the invoking user with their password, which ties each
elevated action in the audit trail to a human who re-proved identity;
Chapter 3 described the fifteen-minute cache that
keeps this workable. With it, elevation is available to scripts and to
sessions that hold no human at the keyboard - at the price that anyone (or
anything) controlling that account can exercise the rule without further
evidence. The pattern suits a narrow, low-risk command set; it should not
be the shape of %sudo.
5.4.2. Limiting Commands and Preserving Auditability
The alias listed /usr/bin/id and /usr/bin/whoami by absolute path, and
the paths are mandatory: sudoers matches the command as executed, so a
rule for /usr/bin/id does not cover a copy of the same binary living
elsewhere. A command named without arguments matches any arguments the
caller supplies; to pin a rule to one specific invocation, name the
arguments in the rule. These details are what make a limited list
limited.
Try something outside the list:
$ sudo -n cat /etc/shadow
sudo: a password is required
The command is not covered by any NOPASSWD rule, so -n failed rather
than prompting. Interactive sudo in this situation asks for the invoking
user's password and then refuses the command with a distinct message
(Sorry, user john is not allowed to execute ... as root on trixie); -n
simply fails before the prompt, which is the probe's purpose. The refusal
produced no journal record - sudo writes its audit line once it has
authenticated the invoker and made a decision, so an aborted -n attempt
never reaches that point. The commands that did run are all there:
# journalctl -t sudo --no-pager -n 80 | grep -E "COMMAND=/usr/bin/(id|whoami)"
Nov 18 16:15:48 trixie sudo[553080]: john : PWD=/home/john ; USER=root ; COMMAND=/usr/bin/id
Nov 18 16:15:51 trixie sudo[553114]: john : PWD=/home/john ; USER=root ; COMMAND=/usr/bin/whoami
Nov 18 16:16:01 trixie sudo[553221]: sam : PWD=/home/sam-projects ; USER=root ; COMMAND=/usr/bin/whoami
journalctl -t sudo selects everything logged under sudo's identifier; the
grep keeps the COMMAND= decision lines, and -n 80 bounds the read
(Chapter 8 covers the journal in depth). Each line
records the real invoking user, the directory the command ran from, the
target user, and the full command as executed - the fields an administrator
needs to answer "who ran what, from where, as whom". Note sam's entry: the
drop-in's %project rule covers her too, and her PWD records the moved
home directory from Section 5.2.2. Checking what a different user may run
without becoming them is an administrator query:
# sudo -l -U sam
Matching Defaults entries for sam on trixie:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin, use_pty
User sam may run the following commands on trixie:
(root) NOPASSWD: /usr/bin/id, /usr/bin/whoami
And sam's own session shows the same boundary john hit:
As sam:
$ sudo -n cat /etc/shadow
sudo: a password is required
The delegation patterns now on the table differ in real ways:
| Pattern | Grants | Password | Typical use |
|---|---|---|---|
sudo group (stock rule) |
Every command, as any user | Invoker's | Full administrators |
| Limited list, passworded | Named commands only | Invoker's | Occasional elevated tasks by humans |
Limited list, NOPASSWD |
Named commands only | None | Automation and narrowly shared operations |
| Limited list with arguments | One specific invocation | Either | Pinning a script's usage exactly |
Whatever the pattern, the policy must parse before it is trusted. Take a
rule with a missing colon - the kind of typo an editor makes late in the
day - and check it in john's workspace before it goes anywhere near
/etc/sudoers.d:
$ printf "john ALL=(root) NOPASSWD /usr/bin/id\n" > /home/john/debian-handbook/accounts-permissions/broken-sudoers
# visudo -cf /home/john/debian-handbook/accounts-permissions/broken-sudoers
/home/john/debian-handbook/accounts-permissions/broken-sudoers:1:37: syntax error
john ALL=(root) NOPASSWD /usr/bin/id
^
The validator names the file, the line, and the column, and reproduces the offending line with a caret. Remove the practice file:
# rm /home/john/debian-handbook/accounts-permissions/broken-sudoers
The stakes are exactly what the check prevents: a live fragment with a
syntax error makes sudo refuse to evaluate the policy at all - every
elevated command, for every user, fails with a parse error until the file
is fixed or removed. The safe habit is mechanical: draft the fragment,
validate with visudo -cf, then install it with the 0440 mode and
validate the whole policy with visudo -c. Editing the main file with
plain visudo (no arguments) has the same safety built in - it locks the
file against concurrent editors and refuses to save a file that fails to
parse.
visudo option |
Description |
|---|---|
| (no arguments) | Edit /etc/sudoers with locking and a parse check on save. |
-c |
Check the main file and all drop-ins. |
-cf file |
Check one named file. |
-f file |
Edit one named file instead of the main one. |
For the audit trail, journald is the baseline shown above; two extensions
exist when the requirement grows. Sudo can log input and output sessions
(log_output, with sudoreplay for playback) for rules where the
command's effect alone is not enough evidence; and the records can be
shipped off-host by a central journal or syslog collector, so that the
machine's own administrators cannot silently rewrite history. Both are
configuration choices on the rules and the journal, not new software, and
Chapter 8 and Chapter 26 pick them
up where this chapter leaves off.
5.4.3. Diagnosing Failed Privilege Escalation
Sudo failures are a small set with distinct causes, and each message names its layer:
| Message | Meaning | Next step |
|---|---|---|
sudo: a password is required (with -n) |
No NOPASSWD rule matched the command. |
sudo -l; check the alias and paths. |
Sorry, try again ... sudo: N incorrect password attempt |
Authentication failed: wrong password. | The invoking user's own password, not the target's. |
... is not in the sudoers file. This incident will be reported. (interactively) |
No rule matches this user at all. | Check group membership and drop-ins; the report is mailed to the sudoers mailto user, root by default. |
Sorry, user ... is not allowed to execute ... (interactively) |
Rules exist, but none covers this command as invoked. | Compare the full path and arguments against sudo -l. |
sudo: parse error in /etc/sudoers.d/... |
A policy fragment is broken. | visudo -c to locate it; fix or remove the file. |
sudo: command not found |
The command is outside secure_path. |
Name the full path in the invocation and the rule. |
The first and fourth cases account for most day-to-day incidents with a
restricted policy, and both are answered by comparing what was invoked
against what sudo -l lists - including the arguments, since a rule that
names arguments only matches those arguments. The second case deserves its
warning: sudo authenticates the invoking user; a teammate who types the
target account's password at the prompt will simply fail. The third and
fifth are policy-integrity problems, not user problems: an account with no
rules, or a fragment that no longer parses, points at an administrative
mistake, and the fix is visudo -c before touching anything else.
The journal closes the loop: journalctl -t sudo shows which commands
actually ran and as whom (Section 5.4.2), and the PAM session lines around
them show the authentication steps. When the failure is at the
authentication layer rather than the policy layer - a password that should
work but does not - the account state itself is the next subject.