Always Use visudo
sudo visudo
visudo opens /etc/sudoers in an editor, usually nano on Debian and Ubuntu and vi on AlmaLinux and Rocky Linux. It locks the file against a second edit at the same time and checks the syntax before it saves. If it finds an error, it tells you and lets you go back and fix it instead of writing a broken file. A plain editor does none of this, so never open the file with nano or vim directly.
The Safe Order
A file that visudo accepts can still contain a rule that does not do what you meant. Work in this order:
- Open a second SSH session and run
sudo -iin it. Keep that root shell open until the end. - Make the change with visudo in the first session.
- Check every sudoers file with
sudo visudo -c. - Log in again in a new session and run
sudo -vandsudo -lto confirm that sudo still works for you. - Only then close the root shell.
If sudo is broken, run visudo from the open root shell and repair it. If no root shell is left, use your provider's console or rescue mode: boot the rescue system, mount the server's disk, and correct or remove the faulty file. On Ubuntu the root account has no password by default, so a console login as root is often not possible and rescue mode is the way in.
How a Rule Is Built
root ALL=(ALL:ALL) ALL
%sudo ALL=(ALL:ALL) ALL
| Part | Meaning |
|---|---|
root, %sudo | A user, or a group with % |
First ALL | Hosts the rule applies to |
(ALL:ALL) | Users and groups to run as |
Last ALL | Commands allowed |
On Debian and Ubuntu the admin group is sudo; on AlmaLinux and Rocky Linux the file has the line %wheel ALL=(ALL) ALL. For most servers, adding a user to that group is all you need, and it requires no edit at all: see how to create a sudo user and how to add a user to a group. When several rules match the same command, the last one in the file wins, so put specific rules after general ones.
Put Your Rules in sudoers.d
The main file ends with a line that includes the directory /etc/sudoers.d; in current files it starts with @includedir, in older ones with #includedir, and in both cases it is not a comment. Keep your own rules there, one file per purpose, and edit each file with visudo as well:
sudo visudo -f /etc/sudoers.d/deploy
- File name: use a plain name without a dot. Files whose names contain a dot or end in a tilde are skipped, so
deploy.confwould be ignored. - Permissions: the file should be owned by root with mode
0440; check withls -l /etc/sudoers.d. - Updates: a package update may bring a new version of the main file, while your drop-in files stay untouched.
One Command Without a Password
A deployment script may need to restart one service without a prompt. Allow exactly that command:
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp.service
- Use the full path: find it with
command -v systemctl. - Name the arguments: with arguments in the rule, only those exact arguments are allowed. Without them, any arguments are.
- Never NOPASSWD for ALL: anyone who gets into that account has root without knowing a password.
- Avoid commands that open other commands: editors, pagers, shells, and scripts the user can edit all lead to a full root shell.
sudo-rs on Ubuntu 25.10 and 26.04
On Ubuntu 25.10 and 26.04, sudo is sudo-rs, a rewrite in Rust. It reads the same /etc/sudoers and /etc/sudoers.d files, so the rules above work unchanged. It supports the common rule syntax but not every rarely used option, and its messages are worded differently. After an upgrade, run sudo visudo -c and test your rules again; see how to upgrade Ubuntu.
Common Mistakes
- Group without %:
sudo ALL=(ALL:ALL) ALLnames a user called sudo, not the group. - Testing in the same session: sudo may still remember your password from a minute ago. Test in a new login.