What it does
visudo opens /etc/sudoers (or another file with -f) in an editor (vi by default, or the one in $EDITOR / $SUDO_EDITOR if allowed). When you save, it checks the syntax. With an error it asks what to do: e = edit again, x = quit without saving, Q = save anyway (do not do this).
A broken sudoers file can stop all sudo use. On a server without a root password that means you are locked out — so always use visudo.
Rule format: WHO WHERE=(AS_WHOM) COMMANDS, for example %wheel ALL=(ALL) ALL.
How to write it
visudo [OPTION]... [[-f] FILE]Main parts
%group- A group name in a rule (users without %).
ALL- Any host / any user / any command (by position).
(root)- The user the command runs as.
NOPASSWD:- Do not ask for a password for the commands after it.
Defaults- A setting line, like
Defaults timestamp_timeout=10. User_Alias,Cmnd_Alias- Names for lists of users or commands.
Options 9
Most used
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1Edit the main file
visudoOpens /etc/sudoers with locking and a syntax check.
2Create a rule file for a team
visudo -f /etc/sudoers.d/webadminsPut the rule in a separate file: updates of the sudo package do not touch it.
3Allow a group only some commands
cat /etc/sudoers.d/webadmins %webadmins ALL=(root) /usr/bin/systemctl restart httpd, /usr/bin/systemctl status httpdThe content of the file. Use full paths.
4Allow one command without a password
cat /etc/sudoers.d/backup1 backup1 ALL=(root) NOPASSWD: /usr/bin/rsyncFor a backup job that runs from cron.
5Check all files after a change
visudo -c /etc/sudoers: parsed OK /etc/sudoers.d/webadmins: parsed OKRun it after every change, also after a config tool wrote the file.
6Check one file before you install it
visudo -cf /tmp/newruleTest a file from Ansible or a colleague before you copy it into sudoers.d.
7Strict check with owner and permissions
visudo -c -s -O -PA file with wrong permissions is skipped by sudo.
8Use nano instead of vi
EDITOR=nano visudoWorks when
env_editoris allowed (default).9Make the password last longer
echo 'Defaults timestamp_timeout=15' > /etc/sudoers.d/timeout; visudo -csudo asks again after 15 minutes.
0= ask every time.10Log all sudo input and output (audit)
echo 'Defaults log_output' > /etc/sudoers.d/iolog; visudo -cSessions are saved in /var/log/sudo-io; replay them with
sudoreplay.11Use command aliases for clear rules
cat /etc/sudoers.d/web Cmnd_Alias WEB = /usr/bin/systemctl restart httpd, /usr/bin/systemctl reload httpd %webadmins ALL=(root) WEBOne name for a list of commands.
Nothing matches this filter.
Watch out
- Never edit /etc/sudoers with a normal editor.
- Files in /etc/sudoers.d/ with a dot or
~in the name are ignored. - The last matching rule wins: put special rules after general ones.
Files
/etc/sudoers- The main file (has
#includedir /etc/sudoers.dor@includedir). /etc/sudoers.d/- Extra rule files.
Exit codes
0 :: Saved, or the check found no error. 1 :: Error in the file or the check failed.