Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

visudo

Edit the sudo rules safely: visudo locks the file and checks the syntax before it saves.

Level
RHCSA exam
Group
Users and groups
Needs root
Yes
Package
sudo

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

-f, --file=FILE
Edit this file instead of /etc/sudoers (like /etc/sudoers.d/NAME).
-c, --check
Only check the syntax of all sudoers files; change nothing.
-q, --quiet
With -c: no "parsed OK" messages.
-s, --strict
Strict check: also errors for unknown aliases and wrong order.
-O, --owner
With -c: also check that the owner is root.
-P, --perms
With -c: also check the permissions (0440).
-I, --no-includes
Do not edit the included files.
-h, --help
Show help.
-V, --version
Show the version.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Edit the main file

    visudo

    Opens /etc/sudoers with locking and a syntax check.

  2. 2Create a rule file for a team

    visudo -f /etc/sudoers.d/webadmins

    Put the rule in a separate file: updates of the sudo package do not touch it.

  3. 3Allow a group only some commands

    cat /etc/sudoers.d/webadmins
    %webadmins ALL=(root) /usr/bin/systemctl restart httpd, /usr/bin/systemctl status httpd

    The content of the file. Use full paths.

  4. 4Allow one command without a password

    cat /etc/sudoers.d/backup1
    backup1 ALL=(root) NOPASSWD: /usr/bin/rsync

    For a backup job that runs from cron.

  5. 5Check all files after a change

    visudo -c
    /etc/sudoers: parsed OK
    /etc/sudoers.d/webadmins: parsed OK

    Run it after every change, also after a config tool wrote the file.

  6. 6Check one file before you install it

    visudo -cf /tmp/newrule

    Test a file from Ansible or a colleague before you copy it into sudoers.d.

  7. 7Strict check with owner and permissions

    visudo -c -s -O -P

    A file with wrong permissions is skipped by sudo.

  8. 8Use nano instead of vi

    EDITOR=nano visudo

    Works when env_editor is allowed (default).

  9. 9Make the password last longer

    echo 'Defaults timestamp_timeout=15' > /etc/sudoers.d/timeout; visudo -c

    sudo asks again after 15 minutes. 0 = ask every time.

  10. 10Log all sudo input and output (audit)

    echo 'Defaults log_output' > /etc/sudoers.d/iolog; visudo -c

    Sessions are saved in /var/log/sudo-io; replay them with sudoreplay.

  11. 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) WEB

    One name for a list of commands.

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.d or @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.