# How do I let a user run one command with sudo?

Let one account run one command as root without a password. Write a sudoers drop-in, check it with visudo -cf, and avoid wildcards and editors.

Source: https://valakes.com/learn/sudo-for-one-command/

Last updated: 2026-11-29

Write a rule that names the command with its full path and exact arguments, put it in its own file under /etc/sudoers.d/, check it with visudo -cf, and install it with mode 440. Do not use wildcards in the arguments, and do not allow editors or any program that can start a shell.

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), 25 November 2026.

Give the account exactly one rule: `deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp.service`. Put it in a file of its own under `/etc/sudoers.d/`, check the file with `visudo -cf` before it goes live, and test it as that user. Do not use `*` in the command, and do not allow editors. Both give the user the whole machine.

This is security advice, so the rules below follow the sudoers manual, and each command ran on the system named in the check line below.

```console
$ sudo --version | head -1
Sudo version 1.9.15p5
```

## Start with the case that has no rule

The account `deploy` is meant to restart a service called `myapp` and do nothing else. Today it has no right to use sudo at all:

```console
$ sudo -l -U deploy
User deploy is not allowed to run sudo on dev.
```

`sudo -l -U` lists what another user may run. It is the command to use at every step below.

## Write the rule in a temporary file

A rule has five parts: who, on which host, as which user, the tags, and the command.

```console
$ echo 'deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp.service' > /tmp/deploy-myapp
```

`deploy` is the user. `ALL` is the host and does not matter on a single machine. `(root)` is the user the command runs as. `NOPASSWD:` is the tag that skips the password, and what follows is the command. Write the full path, as `which systemctl` shows it. The arguments in a rule must match what the user types. This rule allows `restart myapp.service` and nothing else.

Write it to `/tmp` first. A mistake in a file that is already in `/etc/sudoers.d/` can leave sudo unable to read its configuration, and the fix then needs root.

## Check it with visudo

`visudo -c` checks the syntax, and `-f` points it at the file you name:

```console
$ sudo visudo -cf /tmp/deploy-myapp
/tmp/deploy-myapp: parsed OK
```

Here is how a typo looks. This version has no colon after `NOPASSWD`:

```console
$ echo 'deploy ALL=(root) NOPASSWD /usr/bin/systemctl restart myapp.service' > /tmp/bad-rule
$ sudo visudo -cf /tmp/bad-rule
/tmp/bad-rule:1:68: syntax error
deploy ALL=(root) NOPASSWD /usr/bin/systemctl restart myapp.service
                                                                   ^
```

The message gives the line, the column and a caret. The exit status is 1.

## Install it

Copy the checked file in with the right owner and mode. The default for sudoers files is the owner root and mode 0440, and `visudo -c` checks both. Use a name without a dot, because `sudoers(5)` says that `@includedir` skips file names that end in `~` or contain a `.`:

```console
$ sudo install -m 440 -o root -g root /tmp/deploy-myapp /etc/sudoers.d/deploy-myapp
$ sudo visudo -c
/etc/sudoers: parsed OK
/etc/sudoers.d/README: parsed OK
/etc/sudoers.d/deploy-myapp: parsed OK
/etc/sudoers.d/dev: parsed OK
```

With no file named, `visudo -c` checks the main file and everything it includes, and it also checks owners and modes. Now see what `deploy` may do:

```console
$ sudo -l -U deploy
Matching Defaults entries for deploy on dev:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin,
    use_pty

User deploy may run the following commands on dev:
    (root) NOPASSWD: /usr/bin/systemctl restart myapp.service
```

## Test it as the user

`sudo -u deploy sudo -n ...` runs sudo as `deploy`, from your admin account. `-n` makes the inner sudo fail instead of asking for a password, so nothing hangs. The real test is a login as `deploy`, but this shows the same rule:

```console
$ sudo -u deploy sudo -n systemctl restart myapp.service
$ systemctl is-active myapp.service
active
$ sudo -u deploy sudo -n systemctl restart ssh
sudo: a password is required
```

The first restart ran without a password. The second command is not in the rule, so sudo falls back to asking for a password, and `-n` turns that into an error. That is what you want.

## Why a wildcard is dangerous

A shell wildcard in the arguments looks like a convenient way to allow many files. The sudoers manual says what it matches: in arguments, `*` matches any character, including white space, and a forward slash too, because an argument can be any string, not only a path. Say you allow `cat` for logs:

```console
$ echo 'deploy ALL=(root) NOPASSWD: /usr/bin/cat /var/log/*' > /tmp/deploy-logs
$ sudo install -m 440 -o root -g root /tmp/deploy-logs /etc/sudoers.d/deploy-logs
$ sudo -u deploy sudo -n cat /var/log/../../etc/shadow | cut -d: -f1 | head -3
root
daemon
bin
$ sudo rm /etc/sudoers.d/deploy-logs
```

`/var/log/../../etc/shadow` matches `/var/log/*`. The user just read the password file of the machine, and I cut the output to the account names. The last command removes this rule again. Do not leave a rule like it on a real server, and try the demo on a test machine. If a rule must take an argument, write one rule per exact value, or use a regular expression that starts with `^` and ends with `$`.

## Why not an editor

`sudoers(5)` explains the problem: once sudo has started a program, the program can do anything, including starting other programs, and many allow a shell escape. The manual names shells, editors, paginators, mail and terminal programs. A rule such as `NOPASSWD: /usr/bin/vim /etc/myapp/app.conf` therefore gives the user a root shell: open `vim`, type `:!sh`.

The manual's answer for editing is `sudoedit`. sudo copies the file to a temporary file owned by the user, runs the editor as that user, and copies the result back if it changed. The editor never runs as root:

```console
$ echo 'deploy ALL=(root) NOPASSWD: sudoedit /etc/myapp/app.conf' > /tmp/deploy-edit
$ sudo install -m 440 -o root -g root /tmp/deploy-edit /etc/sudoers.d/deploy-edit
$ sudo -u deploy env EDITOR='sed -i s/debug/info/' sudoedit /etc/myapp/app.conf
$ cat /etc/myapp/app.conf
log_level = info
```

Here `sed` stands in for the editor so that the check runs without a person. The user changed a file that only root can write, and without a root shell. In real life, `deploy` types `sudoedit /etc/myapp/app.conf` and edits the file in the editor of their choice. Keep this rule: it is the one of the two that is meant to stay.

The folder of the file must not be writable by the user, or the user can replace the file with a link to another file. `sudoedit` has a check for this, but do not count on it.

## Mistakes people make

You write `deploy ALL=(ALL) NOPASSWD: ALL` to save time. That turns the account into root, and anyone who gets its key gets root. It is the same when the account belongs to an AI agent, and an agent runs far more commands than you do; [the article on AI agents on servers](https://valakes.com/learn/ai-agents-on-servers/) covers that case.

You allow a script that the user can write to. The user changes the script, then runs it as root. A script you allow must be owned by root and not writable by the user.

You edit `/etc/sudoers` in an ordinary editor. Use `visudo`, which checks the file before it saves.

You skip the check when the change looks small. A single missing colon was enough above.

You use the rule to hide a password problem. Think first about whether the account needs root at all, and see [safe rm on servers](https://valakes.com/learn/safe-rm-on-servers/) for what a root command can do when it goes wrong. How the account logs in is a different question; [the SSH keys article](https://valakes.com/learn/ssh-keys/) and [turning off password login](https://valakes.com/learn/disable-ssh-password-login/) cover it. When sudo fails over SSH with "a terminal is required", see [sudo over SSH](https://valakes.com/learn/sudo-over-ssh/).

## In Valakes

If a server keeps a sudo password instead, you can bind a saved credential to sudo for that host, and Valakes answers the sudo prompt from its vault without showing the value. sudo is one of the ten tools it knows by default. [Feature: Password auto-fill](https://valakes.com/features/password-auto-fill/)

[Read the docs: Credentials and auto-fill](https://docs.valakes.com/credentials/)

## Questions

### Why a file in /etc/sudoers.d/ and not /etc/sudoers?

A separate file is easy to add and to remove, and a package or a script can drop one in without touching the main file. sudo reads every file of that folder in alphabetical order, and it skips names that end in ~ or contain a dot.

### What does NOPASSWD do?

It means sudo does not ask for the password of the person who runs it, for the commands of that rule only. Other commands still need the password, or are refused.

### Can I allow a command with any arguments?

Yes, by writing the command with no arguments at all, but then the user chooses the arguments. For a tool such as systemctl or cat that is almost the same as giving root. List the exact argument sets you want instead.

### Are there safer patterns than wildcards?

Since sudo 1.9.10 the arguments can be a regular expression that starts with ^ and ends with $. The sudoers manual calls these usually safer, because they give you control over what matches. Ubuntu 24.04 ships sudo 1.9.15, so they work there.

## References

- [sudoers(5) at sudo.ws, command specs, wildcards, @includedir and preventing shell escapes](https://www.sudo.ws/docs/man/sudoers.man/)
- [visudo(8) at sudo.ws, the -c and -f options](https://www.sudo.ws/docs/man/visudo.man/)
- [sudo(8) at sudo.ws, the -e option (sudoedit)](https://www.sudo.ws/docs/man/sudo.man/)
- [sudoers(5) on Ubuntu 24.04](https://manpages.ubuntu.com/manpages/noble/man5/sudoers.5.html)

## Related articles

- [How do I generate an SSH key and put it on a server?](https://valakes.com/learn/ssh-keys/): Run ssh-keygen -t ed25519, then ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host. The private key stays on your computer, the public key goes into ~/.ssh/authorized_keys on the server, and you log in without a password.
- [How do I disable SSH password login safely?](https://valakes.com/learn/disable-ssh-password-login/): Put PasswordAuthentication no in a file in /etc/ssh/sshd_config.d/ whose name sorts first, run sudo sshd -t, then reload ssh. Keep your current session open and test a new login in a second window before you close it.
- [How do I use rm safely on a server?](https://valakes.com/learn/safe-rm-on-servers/): List what a command will remove before you run it, with ls, find or echo. Use rm -I so that rm asks once before a recursive delete, and write variables as "${dir:?}" so that an empty one stops the command. The built-in --preserve-root protects only the path /.
