This post was originally published on go2linux.org. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.

sudo lets a user run a command as root (or as another user) without knowing root's password. Which user may run what is decided by the rules in /etc/sudoers.

I got used to sudo on Ubuntu, where the root account has no password and sudo is the only way to administer the system. I liked it enough that on every other distribution I install, the first thing I do is give my own user sudo rights. The two big advantages over logging in as root are:

  • Limited privileges. A user can be allowed to run only specific commands.
  • A log. Every sudo command is recorded with the user who ran it.

For the difference between sudo and su, see su vs sudo.

Always edit with visudo

Do not open /etc/sudoers directly with an editor. Use:

sudo visudo

visudo locks the file, opens a temporary copy in your editor, and checks the syntax before saving. A typo in sudoers can lock everybody out of sudo, and visudo will refuse to install a broken file and ask you what to do:

>>> /etc/sudoers: syntax error near line 28 <<<
What now?

Press e to go back and fix it, or x to quit without saving.

visudo uses vi by default on some systems and nano on others. To choose:

sudo EDITOR=nano visudo

Use /etc/sudoers.d for your own rules

Most distributions end /etc/sudoers with this line:

@includedir /etc/sudoers.d

(Older versions of sudo write it as #includedir. Despite the #, it is not a comment.)

Every file in /etc/sudoers.d/ is read as part of the configuration. Put your rules there instead of editing the main file, so package updates do not conflict with your changes:

sudo visudo -f /etc/sudoers.d/backup

Two details that catch people:

  • File names that contain a dot or end in ~ are ignored. backup works, backup.conf does not.
  • The files should be owned by root with mode 0440. visudo -f takes care of that when it creates the file.

Reading a rule

The default file has a line like this:

root    ALL=(ALL:ALL) ALL

It has four parts:

who     where=(as_user:as_group) what
  • root — the user the rule applies to. A name starting with % is a group.
  • ALL= — the hosts where the rule applies. It only matters when the same file is shared by several machines; in practice it is almost always ALL.
  • (ALL:ALL) — the users and groups the commands may be run as. (ALL) alone means any user.
  • ALL — the commands allowed.

So this line reads: root, on any host, may run any command as any user and group.

Give a user full sudo rights

The easiest way is not to write a rule at all. Every distribution already has a group with full rights:

%sudo   ALL=(ALL:ALL) ALL     # Debian, Ubuntu
%wheel  ALL=(ALL:ALL) ALL     # Fedora, RHEL, Arch (uncomment it on Arch)

Add the user to that group:

sudo usermod -aG sudo alice      # Debian, Ubuntu
sudo usermod -aG wheel alice     # Fedora, RHEL, Arch

Do not forget the -a, or the user is removed from every other group. See how to add a user to a group. The change takes effect at the user's next login.

Allow only specific commands

This is where sudo is better than sharing the root password. Let the user operator shut down and reboot the machine and nothing else:

operator  ALL=(root) /usr/sbin/poweroff, /usr/sbin/reboot

Always use the full path; which poweroff gives it. You can also fix the arguments. This allows restarting nginx but no other service:

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

Be careful about which commands you allow. Many programs can start a shell, and a shell started from sudo is a root shell. Editors (vim, nano), pagers (less, more), find, awk, tar and many others have ways to run arbitrary commands. Allowing sudo vim /etc/hosts is the same as allowing everything.

To let someone edit a specific file, use sudoedit instead. It copies the file, opens it in the user's own editor as the user, and copies it back as root:

alice  ALL=(root) sudoedit /etc/nginx/sites-available/example.conf

Alice then runs sudoedit /etc/nginx/sites-available/example.conf.

Aliases

With many users and commands, aliases keep the file readable:

User_Alias  OPERATORS = joe, mike, jude
Runas_Alias OP        = root, operator
Host_Alias  OFFICE    = 10.1.2.0/255.255.255.0
Cmnd_Alias  PRINTING  = /usr/sbin/lpc, /usr/bin/lprm

OPERATORS   OFFICE = (OP) PRINTING

Joe, Mike and Jude, on hosts in the 10.1.2.0/24 network, may run the printing commands as root or as operator. Alias names must be in capital letters.

NOPASSWD

sudo asks for the user's own password (not root's). For scripts and cron jobs that need one specific command, you can skip it:

backup  ALL=(root) NOPASSWD: /usr/bin/rsync

Keep NOPASSWD to narrow rules like this one. NOPASSWD: ALL means that anyone who gets a shell as that user, through a stolen SSH key or a bug in a program running as that user, is root immediately.

How long sudo remembers your password

After you type your password, sudo does not ask again for 15 minutes (5 on some systems) in the same terminal. To change it, add a Defaults line in a file under /etc/sudoers.d/:

Defaults timestamp_timeout=5

The value is in minutes. 0 asks for the password every time. A negative value means never ask again in that session, which is not a good idea.

To forget the cached password right now, for example before walking away from the computer:

sudo -k

Check what you are allowed to do

sudo -l

It lists the rules that apply to your user. Root can check another user with sudo -l -U alice.

Check the syntax of all the files without editing them:

sudo visudo -c

The log

Every use of sudo, successful or not, is logged:

sudo journalctl _COMM=sudo

On systems that also write text logs, look in /var/log/auth.log (Debian, Ubuntu) or /var/log/secure (Fedora, RHEL).

If you break it anyway

If sudo stops working because of a bad file (for example, one copied into sudoers.d without visudo), you need root another way:

  • If root has a password: su -, then visudo -c to find the problem and fix it.
  • On desktops with polkit: pkexec visudo.
  • Otherwise, reboot into recovery mode (Ubuntu, Debian), or add init=/bin/bash to the kernel line in GRUB, remount the root file system read-write with mount -o remount,rw /, and fix the file.

A note on sudo-rs

Ubuntu 25.10 replaced sudo with sudo-rs, a rewrite in Rust. It reads the same /etc/sudoers and /etc/sudoers.d/ and supports the common syntax shown here, but not every option of the original. If an unusual setting is ignored on a recent Ubuntu, that is why. The original sudo is still available as sudo.ws.

See also

man sudoers, man visudo and man sudo.