Grant one command in a separate file
A separate file is easy to see and easy to remove, and the main /etc/sudoers stays
untouched. File requirements: a name with no dot and no trailing ~, owner root:root,
mode 0440.
The visudo command edits /etc/sudoers and files in /etc/sudoers.d and refuses to save a rule with a grammar error.
sudo visudo
the main /etc/sudoers
sudo visudo -f /etc/sudoers.d/deploy
a separate rule file
sudo EDITOR=vim visudo -f /etc/sudoers.d/deploy
another editor
sudo chmod 0440 /etc/sudoers.d/deploy
the mode sudo expects
sudo visudo -c
check all files and their modes
sudo visudo -cf /tmp/candidate
check a finished file
sudo -l -U deploy
what deploy may run in the end
sudo lets users in by the rules in /etc/sudoers and the files of the
/etc/sudoers.d directory. A mistake in these files can leave the machine without
sudo, so they are edited with visudo. It opens a copy of the file in an editor (on
Ubuntu nano by default; another one via EDITOR) and checks the grammar after you
save. It won't write a file with an error.
visudo -c checks the main file and every included one, along with their owner and
mode. parsed OK only means the line follows the grammar. What it allows, visudo
doesn't judge.
A rule reads left to right:
Part of deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart app |
Meaning |
|---|---|
deploy |
who; %group means every member of the group |
ALL= |
on which machine (almost always ALL) |
(root) |
as whom to run |
NOPASSWD: |
without typing your password |
/usr/bin/systemctl restart app |
which command — full path and arguments |
Full list — man visudo; the rule grammar — man sudoers.
| Option | What it does | Example |
|---|---|---|
-c --check |
Check only — grammar, owner and mode of every sudoers file. | sudo visudo -c |
-f --file file |
Edit or check the given file instead of /etc/sudoers. | sudo visudo -f /etc/sudoers.d/deploy |
-s --strict |
Strict checking — undefined aliases too. | sudo visudo -cs |
-q --quiet |
Omit error details — with -c in scripts that rely on the exit code. | sudo visudo -cq |
Nothing found. Try a shorter query.
A separate file is easy to see and easy to remove, and the main /etc/sudoers stays
untouched. File requirements: a name with no dot and no trailing ~, owner root:root,
mode 0440.
Check the grammar first, then the meaning: the command in a rule is a full path (find
it with command -v systemctl), and the arguments are exactly the ones needed. ALL
in the command position grants any command as root.
-n forbids the password prompt: where there's no rule, sudo prints a password is
required right away.
visudo was run without root. Use sudo visudo.
The saved file failed the check: visudo prints the file, line number and the spot of
the error and asks what to do. e — back to the editor to fix it, x — exit without
saving. Q saves the broken file — don't.
The file in /etc/sudoers.d was created with ordinary permissions. Run sudo chmod
0440 file and repeat sudo visudo -c.
The file name contains a dot (deploy.conf) or ends with ~ — sudo silently skips such
files, and visudo -c doesn't list them. Rename it without the dot.
Of all matching rules, the last one wins. The line %sudo ALL=(ALL:ALL) ALL or another
file later in order can override yours. See the result with sudo -l -U name. Files in
/etc/sudoers.d are read in name order — hence prefixes like 90-.
sudo nano /etc/sudoers will save a broken file, and sudo may stop working. Edit
only through visudo, and keep a second terminal with sudo -i open until you've
checked the result.
NOPASSWD: ALL, a wildcard in the arguments, or a program that can start a shell
(vim, less, find -exec) give the user full root. Allow a specific command with
specific arguments.
sudo group and its membership.sudo group.0440 included.visudo opens on Ubuntu.EDITOR=vim.Found an inaccuracy? Report an error in the article