Linux and servers · Intermediate · 17 min
Write least-privilege sudo rules with visudo
By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · sudo-rs 0.2.13
You will replace blanket sudo access with checked rule files in /etc/sudoers.d that let two operators restart one service and edit one file, give an automation account a password-free command list, and audit what every account can run.
On this page
Membership of the sudo group means "may do anything as root". That suits the one or two people who run the server. Everyone else, and every script, should get a list of exact commands. This tutorial picks up where the one-line rule in Create a sudo user stops.
You will write rule files for a small app. Two operators, ada and kofi, may restart app.service and edit its settings file, and nothing more. An automation account, deploybot, may run a short list of commands without a password. Along the way you will see two rules that look narrow but hand out root, and you will finish by auditing every account.
Ubuntu 26.04 runs sudo-rs, a Rust rewrite of sudo, as sudo, visudo and sudoedit. It reads the same /etc/sudoers format. A few options of the original sudo are missing or always on, and this tutorial points them out where they matter.
Step 1 · Check your sudo and keep a way back in
A broken rule file can stop sudo from working for everyone, so keep a root shell open before you change anything. In your second terminal, start one and leave it alone until the end:
sudo -iIf you manage the server over a mobile data connection that drops now and then, start that root shell inside tmux so it survives a disconnect.
In your main terminal, see which implementation you have and how the main file pulls in the rule directory:
sudo --version
sudo grep -n 'includedir' /etc/sudoers
ls -l /etc/sudoers.dCreate the two operator accounts. adduser asks for a password for each. They are not added to the sudo group.
sudo adduser ada
sudo adduser kofiConfirm ada is an ordinary account:
id adaCheck it worked
The first line of sudo --version names sudo-rs and a version such as 0.2.13. The grep prints a line ending in @includedir /etc/sudoers.d, which means every file in that directory is read as part of the configuration. id ada shows ada in its own group only, with no sudo group in the list.
If you need the original sudo for an option sudo-rs lacks, Ubuntu still ships it as sudo.ws, selected with update-alternatives. Everything below works with both.
Step 2 · Draft rules outside /etc and check them first
Never write straight into /etc/sudoers.d. Write a draft in your home directory, let visudo parse it, and only then install it with the owner and mode sudo insists on. Make a folder for the drafts:
mkdir -p ~/sudoers-demoFirst, see what a mistake looks like. This draft is missing a closing bracket:
cat > ~/sudoers-demo/broken <<'EOF'
ada ALL=(root /usr/bin/systemctl restart app.service
EOF
sudo visudo -cf ~/sudoers-demo/broken || echo "rejected"-c means check only, and -f names the file. With -f, visudo checks the syntax but not the file's owner or mode, so a draft you own is fine here.
Check it worked
The output shows a syntax error with the position it gave up at, then rejected. Nothing was installed, so sudo still works. Delete the broken draft:
rm ~/sudoers-demo/brokenStep 3 · Give two operators one command with aliases
Aliases name groups of users and commands so the rule itself reads like a sentence. User_Alias lists accounts, Cmnd_Alias lists commands with their full paths and arguments. Write the draft:
cat > ~/sudoers-demo/60-app-ops <<'EOF'
# Operators of app.service: restart it, nothing else.
User_Alias APP_OPS = ada, kofi
Cmnd_Alias APP_RESTART = /usr/bin/systemctl restart app.service
APP_OPS ALL=(root) APP_RESTART
EOF
sudo visudo -cf ~/sudoers-demo/60-app-opsRead the rule as: users in APP_OPS, on any host, may run the commands in APP_RESTART as root. Because the arguments restart app.service are part of the entry, those exact arguments are the only ones allowed. A command written with no arguments at all, such as plain /usr/bin/systemctl, would allow any arguments, which is the whole of systemd. To allow a command only with no arguments, write "" after it.
Install the checked file. install copies it with the owner and mode in one step; sudo ignores rule files that anyone but root can write.
sudo install -o root -g root -m 0440 ~/sudoers-demo/60-app-ops /etc/sudoers.d/60-app-ops
sudo visudo -cCheck it worked
sudo visudo -c checks the owner and mode of /etc/sudoers, then parses it and every file it includes. Any error is reported with the file and position; otherwise it prints one line, /etc/sudoers: parsed OK. Now ask sudo what ada may run:
sudo -l -U ada
sudo -l -U ada /usr/bin/systemctl restart app.service
sudo -l -U ada /usr/bin/systemctl stop app.service || echo "not allowed"The first command lists a single (root) entry for the restart command. Given a command, -l answers yes or no for that exact command line: the restart prints its full path back, and the stop line fails and prints not allowed.
Step 4 · See why a wildcard argument hands out more than you meant
A rule such as systemctl restart * looks like "restart things". Try it on a locked account that nobody can log in to, so the demonstration grants nothing to a real person.
The next commands create a rule that lets an account restart any service on the server. Use the locked account shown, and remove the rule at the end of the next step.
sudo useradd --system --no-create-home --shell /usr/sbin/nologin sudodemo
cat > ~/sudoers-demo/90-unsafe-demo <<'EOF'
sudodemo ALL=(root) NOPASSWD: /usr/bin/systemctl restart *
EOF
sudo visudo -cf ~/sudoers-demo/90-unsafe-demo
sudo install -o root -g root -m 0440 ~/sudoers-demo/90-unsafe-demo /etc/sudoers.d/90-unsafe-demoIn sudo-rs, a * is accepted only as the last argument, and it means "any further arguments, or none". The original sudo goes further and matches * inside arguments like a shell pattern, where it can also match spaces and ../. A rule like /usr/bin/cat /var/log/app/* then allows cat /var/log/app/x /etc/shadow. Either way, a wildcard widens the rule to arguments you never listed.
Check it worked
sudo -l -U sudodemo /usr/bin/systemctl restart ssh.service
sudo -l -U sudodemo /usr/bin/systemctl restart app.service ssh.service cron.serviceBoth commands are allowed. The account can restart the SSH server or several services at once, not only the app. Write out each permitted argument list instead, as APP_RESTART does.
Step 5 · Avoid commands that open a shell, and use sudoedit
Some programs can start other programs. Allowing one of them as root allows everything. Common cases are pagers and editors (less, more, vim, nano), which can run shell commands from inside, and tools with an exec option (find -exec, tar --to-command, awk's system()). Add find to the demo account's rule and see what it gives away:
cat > ~/sudoers-demo/90-unsafe-demo <<'EOF'
sudodemo ALL=(root) NOPASSWD: /usr/bin/systemctl restart *, /usr/bin/find
EOF
sudo visudo -cf ~/sudoers-demo/90-unsafe-demo
sudo install -o root -g root -m 0440 ~/sudoers-demo/90-unsafe-demo /etc/sudoers.d/90-unsafe-demo
sudo -u sudodemo sudo -n /usr/bin/find /etc/hostname -exec /usr/bin/id -u \;The last command prints 0: find ran id as root, and it would run a shell just as readily. The NOEXEC: tag asks sudo-rs to stop a command starting others, but its own manual says this is not complete protection. Leave such programs out of rules.
Remove the demo rule and account now:
sudo rm /etc/sudoers.d/90-unsafe-demo ~/sudoers-demo/90-unsafe-demo
sudo userdel sudodemoWhen an operator needs to change a root-owned file, grant sudoedit instead of an editor. sudoedit copies the file, opens the copy in the user's own editor as that user, and writes it back as root when the editor exits. The editor never runs as root. Create the app's settings file, private from the start:
sudo install -d -o root -g root -m 0755 /etc/app
sudo install -o root -g root -m 0640 /dev/null /etc/app/app.envRewrite the operators' draft with a second alias. In the rule, sudoedit is written without a path.
cat > ~/sudoers-demo/60-app-ops <<'EOF'
# Operators of app.service: restart it and edit its settings.
User_Alias APP_OPS = ada, kofi
Cmnd_Alias APP_RESTART = /usr/bin/systemctl restart app.service
Cmnd_Alias APP_EDIT = sudoedit /etc/app/app.env
APP_OPS ALL=(root) APP_RESTART, APP_EDIT
EOF
sudo visudo -cf ~/sudoers-demo/60-app-ops
sudo install -o root -g root -m 0440 ~/sudoers-demo/60-app-ops /etc/sudoers.d/60-app-opsada edits the file with:
sudoedit /etc/app/app.envsudo-rs refuses if the file sits in a directory the user can write to, which stops a user swapping the file for a link to something else. That is why /etc/app is owned by root.
Check it worked
ls /etc/sudoers.d
sudo -l -U ada90-unsafe-demo is gone from the listing. The entry for ada now shows the restart command and sudoedit /etc/app/app.env.
Step 6 · Allow NOPASSWD only for an automation account
A deploy script cannot type a password, so its account needs NOPASSWD. A person can, so their rules should keep the password prompt: it confirms that whoever is at the keyboard is really them. Create a system account for the script. It logs in with an SSH key, not a password.
sudo adduser --system --group --home /var/lib/deploybot --shell /bin/bash deploybotGive it its own file and its own alias, with the exact commands a deploy runs:
cat > ~/sudoers-demo/70-deploybot <<'EOF'
# CI deploys: restart the app and read its state, without a password.
Cmnd_Alias DEPLOY_CMDS = /usr/bin/systemctl restart app.service, /usr/bin/systemctl is-active app.service
deploybot ALL=(root) NOPASSWD: DEPLOY_CMDS
EOF
sudo visudo -cf ~/sudoers-demo/70-deploybot
sudo install -o root -g root -m 0440 ~/sudoers-demo/70-deploybot /etc/sudoers.d/70-deploybotCheck it worked
sudo -l -U deploybot
sudo -u deploybot sudo -n whoami || echo "refused"The listing shows both commands tagged NOPASSWD:. The second line runs whoami as deploybot with -n, which never prompts; it is refused straight away and prints refused.
Step 7 · Set logging and terminal defaults
sudo-rs logs every allowed and refused command to syslog, which on Ubuntu ends up in the systemd journal. Two settings are worth writing down in a file of their own, so they stay in force if someone switches back to sudo.ws:
use_ptyruns each command in its own pseudo-terminal, so a program left running in the background cannot type into your session after sudo exits. It is on by default in sudo-rs.timestamp_timeoutsets how many minutes a password is remembered. Five is shorter than the usual fifteen.
cat > ~/sudoers-demo/10-defaults <<'EOF'
Defaults use_pty
Defaults timestamp_timeout=5
EOF
sudo visudo -cf ~/sudoers-demo/10-defaults
sudo install -o root -g root -m 0440 ~/sudoers-demo/10-defaults /etc/sudoers.d/10-defaults
sudo visudo -csudo-rs has no logfile option and no input or output recording (sudoreplay), so do not copy those lines from older guides. visudo reports options it does not know.
Check it worked
sudo visudo -c prints one line, /etc/sudoers: parsed OK, which covers /etc/sudoers and the three files it includes. After ada has run a few commands, read the log:
sudo journalctl --since today | grep -i sudoEach entry names the user, the terminal, the working directory and the full command, and refused attempts are logged as well.
Step 8 · Audit what every account can run
Rules pile up. Review them whenever someone joins or leaves, and after any change. This loop asks sudo for the rights of every normal login account plus the automation account:
for u in $(getent passwd | awk -F: '$3 >= 1000 && $3 < 65534 {print $1}') deploybot; do
echo "== $u"
sudo -l -U "$u" || true
done
sudo grep -rnE 'NOPASSWD|ALL$' /etc/sudoers /etc/sudoers.dCheck it worked
Each == heading is followed by that account's rules, or by a line saying it may not run sudo. Only the people who run the server should show (ALL : ALL) ALL. The grep lists every NOPASSWD entry and every rule ending in ALL. Here that is four lines, something like root ALL=(ALL:ALL) ALL, %admin ALL=(ALL) ALL and %sudo ALL=(ALL:ALL) ALL from /etc/sudoers, plus the NOPASSWD line in the deploybot file. The root and %admin lines are Ubuntu defaults; root already has full rights, and no admin group exists unless someone creates one. Anything else in that list needs a reason.
Step 9 · Remove the demo rules and accounts
The next commands delete the demo accounts with their home directories, the two rule files and the app settings file. Skip any line for an account or file you want to keep. 10-defaults stays.
sudo rm /etc/sudoers.d/60-app-ops /etc/sudoers.d/70-deploybot
sudo userdel -r ada
sudo userdel -r kofi
sudo userdel -r deploybot
sudo rm -r /etc/app ~/sudoers-demo
sudo visudo -cCheck it worked
sudo visudo -c still prints parsed OK, and ls /etc/sudoers.d shows 10-defaults and whatever was there before you started (on Ubuntu, usually a README). You can now close the root shell in your second terminal with exit.
Next steps
- Turn off root and password logins over SSH: /tutorials/harden-ssh-on-ubuntu-26-04
- Find the sudo log entries quickly with journalctl filters: /tutorials/search-the-systemd-journal-with-journalctl
- Read
man sudoerson your server for the full rule syntax supported by sudo-rs.
Related tutorials
- Write safer Bash scripts with strict mode and trapsYou will build a small Bash release script that stops on the first error, says which line failed, survives file names with spaces and always removes its temporary files.
- Upgrade a server from Ubuntu 24.04 LTS to 26.04 LTSYou will take a backed-up Ubuntu 24.04 server through do-release-upgrade to 26.04 LTS over SSH with a fallback login, then confirm services, repositories and language runtimes work on the new release.
- Find your way around the Linux filesystem: /etc, /var, /usr and the restYou will know what each top-level folder on an Ubuntu 26.04 server is for, find where any file came from with dpkg, which, file and findmnt, and have a map of where Nginx keeps its binary, config, data, logs and unit file.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials