December 5, 2026

The sudoers File and visudo: Writing Safe Rules, and the Wildcard That Handed Out /etc/shadow

A ring of master keys beside a small cabinet holding a single key

Giving someone permission to restart one service, or read one folder of logs, without handing over the whole machine is exactly what sudo is for. The rules live in /etc/sudoers and the files beside it, and most of them are short. They are also easy to get subtly wrong: one test rule below, meant to let a user read application logs, gave that user the system’s password hashes on the first try. Every rule here was written, checked with visudo and then exercised as an ordinary user on Ubuntu 24.04 with sudo 1.9.15.

Video: Veronica Explains on YouTube

Where rules live

The main file and drop-ins

The stock /etc/sudoers on the test system held a handful of Defaults lines, root’s rule, a rule for the sudo and admin groups, and at the end one line that matters most for day-to-day changes:

@includedir /etc/sudoers.d

Rather than editing the main file, the safer habit is to add a small file to /etc/sudoers.d, one per purpose, so it can be reviewed, removed or managed by configuration tools without touching anything else.

What gets read and what does not

The sudoers manual for Ubuntu 24.04 says files in an included directory are read in sorted lexical order, and that names ending in ~ or containing a . are skipped to avoid editor and package-manager backup files. The test confirmed the second rule the hard way. A rule for the user bob was saved as /etc/sudoers.d/bob.conf, and bob was refused. Renaming the same file to bob made the rule work immediately. A .conf extension, natural for almost every other config directory, silently disables a sudoers file.

The same manual warns that sorting is lexical, not numeric, so 1_whoops loads after 10_second. Two-digit prefixes such as 10-, 20-, 90- keep the order predictable.

Office paperwork

Writing a rule

The shape of a line

alice ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginx

From left to right: who (alice), on which hosts (ALL), as which user (root), with which options (NOPASSWD:), and which exact command. Commands need full paths. Arguments written after the command are part of the rule, so this line allows systemctl restart nginx and nothing else.

Checking before installing

$ visudo -cf /var/tmp/10-alice
/var/tmp/10-alice: parsed OK
$ install -m 0440 /var/tmp/10-alice /etc/sudoers.d/10-alice

visudo -c checks syntax without installing anything, and -f points it at any file. Writing the rule in a temporary location, checking it, and installing it with mode 0440 avoids ever leaving a half-edited file in place.

What the user sees

$ sudo -l
User alice may run the following commands on vm:
    (root) NOPASSWD: /usr/bin/systemctl restart nginx, /usr/bin/cat /var/log/app/*

sudo -l lists every rule that applies, which is the fastest way to confirm what a user really has.

Groups and aliases

Rules for teams are easier to maintain with a group and a command alias. A test file granted a webops group three nginx commands under one name:

Cmnd_Alias WEB_RESTART = /usr/bin/systemctl restart nginx, /usr/bin/systemctl reload nginx, /usr/bin/systemctl status nginx
%webops ALL=(root) NOPASSWD: WEB_RESTART

A user added to webops saw all three commands expanded in sudo -l. Adding or removing people now means changing group membership, not editing sudoers, and the alias name documents what the rule is for. Alias names must be uppercase, and the % marks a group rather than a user.

The wildcard that leaked /etc/shadow

The rule

The second command in alice’s rule was meant to let her read application logs:

/usr/bin/cat /var/log/app/*

What it allowed

$ sudo /usr/bin/cat /var/log/app/app.log
app log line
$ sudo /usr/bin/cat /var/log/app/../../../etc/shadow
root:*:...
daemon:*:...
$ sudo /usr/bin/cat /var/log/app/app.log /etc/shadow
app log line
root:*:...

Both tricks worked. The * happily matched ../../../etc/shadow, a path that starts with /var/log/app/ and climbs straight out of it. It also matched app.log /etc/shadow, two arguments, because the wildcard matched the space between them. The sudoers manual is explicit on both points: in command arguments, a wildcard does match a slash, and wildcards can match any character including white space. It advises using wildcards in arguments with care, and in most cases a regular expression instead.

The fix that held

Recent sudo versions accept regular expressions in rules when they start with ^ and end with $:

alice ALL=(root) NOPASSWD: /usr/bin/cat ^/var/log/app/[a-z]+\.log$

With that rule, cat /var/log/app/app.log still worked. Both escape attempts were refused, because neither ../ nor a space matches [a-z]+\.log.

Better still: avoid the problem

Commands that open files, start pagers or launch editors can often be turned into a shell or made to read other files through their own features. Granting membership in a group that can read the logs (often adm on Ubuntu) or using sudoedit for editing hands out the access needed without running a general-purpose program as root.

Door

Order: the last match wins

The test

A second file, 20-alice, added one more line for the same command, this time without NOPASSWD:

alice ALL=(root) /usr/bin/systemctl restart nginx

sudo -l now listed both rules, and running the restart asked for a password. The earlier, more generous rule from 10-alice no longer applied. The sudoers manual states the principle directly: when several entries match, the last one is used, which is not necessarily the most specific. A broad %sudo ALL=(ALL:ALL) ALL line placed after a careful drop-in will override it, and vice versa. Since @includedir sits at the bottom of the stock file, drop-ins normally get the last word, which is what most administrators want.

When a file is broken

Syntax errors

A deliberately broken rule, with a missing parenthesis, was caught by visudo before installation:

/var/tmp/broken:1:15: syntax error
bob ALL=(root NOPASSWD: /usr/bin/id
              ^~~~~~~~~

Installed anyway, the file produced the same error message every time anyone ran sudo. The old fear is that a single syntax error locks every administrator out until someone boots into recovery. With sudo 1.9.15, that did not happen: alice’s valid rule from another file kept working, and only bob’s broken line failed. The manual explains why: since version 1.9.3, sudoers tries to recover from a syntax error by discarding the rest of the offending line, and this behavior is on by default. Older systems still in service do not recover, which is why visudo -c before every change remains the right habit.

Unsafe permissions

A copy of a valid rule installed with mode 0666 produced:

sudo: /etc/sudoers.d/30-loose is world writable

sudo refused to trust a file anyone could edit and ignored it. Mode 0440, owned by root, is the standard.

Stamp

What sudo does to the environment

A clean slate

A rule that allows a command still runs it in a different environment from the user’s own shell. A test user set a custom PATH with a personal bin directory first, plus two variables, MYTOKEN and EDITOR, then ran /usr/bin/env through sudo. The output contained:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
USER=root
HOME=/root
SUDO_USER=carol

The personal bin directory was gone, replaced by the secure_path from the stock Defaults line. MYTOKEN and EDITOR were gone entirely. The sudoers manual describes env_reset, on by default, as running the command in a minimal environment with only a short list of variables such as TERM, PATH, HOME, SHELL and USER, plus the SUDO_* variables that record who ran it.

Why that matters

Without the reset, a user could place a fake systemctl early in their own PATH, then run the real-looking rule and get their own program executed as root. The secure path closes that door. It also explains a common complaint: a variable a script relies on is “missing under sudo” because sudo removed it on purpose.

Asking to keep the environment

The same test user tried sudo -E to preserve everything:

sudo: sorry, you are not allowed to preserve the environment

Keeping the environment has to be granted explicitly, for example with the SETENV: tag on a specific rule or an env_keep entry for one named variable. Granting it broadly undoes the protection.

The password grace period

After a successful password prompt, sudo does not ask again for a while. The manual sets that window at 15 minutes unless timestamp_timeout overrides it. sudo -k ends it immediately, a reasonable habit before stepping away from an unlocked terminal.

A few safe patterns

Ready to adapt

# Restart one service, no password
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp

# Let a group run a maintenance script (owned by root, not writable by the group)
%ops ALL=(root) /usr/local/sbin/rotate-logs

# Read one log file pattern safely
alice ALL=(root) NOPASSWD: /usr/bin/cat ^/var/log/app/[a-z]+\.log$

# Run a command as a service account instead of root
alice ALL=(postgres) /usr/bin/psql

Rules of thumb from the tests

  • Use a drop-in in /etc/sudoers.d with no dot in its name, mode 0440.
  • Run visudo -cf on the file before installing it, and sudo -l -U username afterward to see the result.
  • Name exact commands and arguments. Treat every * in arguments as a possible hole, and prefer a regular expression or a group permission.
  • Never allow a script the user can edit, an editor, a pager or a shell.
  • Remember the last match wins.

A sudoers rule is a promise that a user can do exactly one thing as root. The test rule above promised log files and delivered the password database, with nothing but a star in between.

Sources

  • sudoers(5), Ubuntu 24.04 manual page: https://manpages.ubuntu.com/manpages/noble/man5/sudoers.5.html

719 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *