Running commands
sudo <command>
Run a command as root
sudo -u <user> <command>
Run as another user
sudo -g <group> <command>
Run with another primary group
sudo -u <user> -g <group> <command>
Set user and group
sudo -u '#<uid>' <command>
Target a user by numeric UID
sudo -n <command>
Fail instead of asking for a password
sudo -b <command>
Run in the background
sudo -T <time> <command>
Kill the command after a timeout
Needs a sudoers rule that allows user-set timeouts.
sudo -R <dir> <command>
Run with another root directory
sudo -- <command> -x
Stop sudo parsing options
sudo !!
Repeat the last command with sudo
Bash and zsh history expansion, not sudo itself.
Privileges and password cache
sudo -l
List what you may run here
sudo -ll
List in the long format
sudo -l <command>
Check if a command is allowed
sudo -l -U <user>
List another user's privileges
sudo -v
Refresh the cache without running anything
sudo -k
Invalidate the cache for this session
sudo -K
Remove all cached credentials
sudo -k <command>
Run a command and force a password prompt
sudo -V
Show sudo and plugin versions
sudo -S
Read the password from stdin
sudo -A
Ask for the password via a helper program
sudo -p '<prompt>'
Custom password prompt
Redirection and pipes
echo '<text>' | sudo tee <file>
Write a root-owned file
echo '<text>' | sudo tee -a
<file>
Append to a root-owned file
echo '<text>' | sudo tee <file>
>/dev/null
Write without echoing to the screen
sudo sh -c '<command> >
<file>'
Redirect inside a root shell
sudo bash -c '<command1> &&
<command2>'
Run a compound command as root
<command> | sudo -u <user>
<command>
Pipe into a command run as another user
sudo cat <file> | <command>
Read a root-only file into a pipe
sudoers rules
sudo visudo
Edit /etc/sudoers with a syntax check
sudo visudo -f /etc/sudoers.d/<name>
Edit a drop-in file
sudo visudo -c
Check the syntax only
sudo visudo -cf <file>
Check one file
Rule: who where = (as whom) what
| Part |
Example |
Meaning |
user or %group |
alice, %admin |
who may run it |
host |
ALL |
on which machine |
(runas) |
(root), (ALL) |
as which user |
tags: commands |
NOPASSWD: /usr/bin/apt |
what, and options |
alice ALL=(ALL:ALL) ALL
Full rights for a user
%wheel ALL=(ALL) ALL
Full rights for a group
alice ALL=(root) NOPASSWD: /usr/bin/systemctl restart
<svc>
One command without a password
alice ALL=(root) /usr/bin/apt update, !/usr/bin/apt remove
*
Allow some, deny others
Cmnd_Alias <NAME> = <cmd1>, <cmd2>
Name a group of commands
Defaults timestamp_timeout=<minutes>
Change the password cache time
Defaults:<user> !authenticate
Never ask this user for a password
Defaults env_keep += "<VAR>"
Always keep a variable
@includedir /etc/sudoers.d
Include drop-in rule files