OnCallReadyStart learning

sudo: execute a command as another user

sudo [-u user] [-i] [-s] [-k] [-l] [-n] [-p prompt] command [args]

Options you will use

-u user
Run the command as user instead of root.
-i
Run a login shell as the target user (fresh environment, target user HOME).
-s
Run a shell as the target user, keeping most of the current environment.
-k
Alone: invalidate the cached credentials, the next sudo asks again. With a command: ask now, and leave the cache as it was.
-K
Remove the cached credentials of every terminal.
-l
List the commands the invoking user is allowed to run.
-n
Non-interactive: fail ("sudo: interactive authentication is required") instead of asking for a password. sudo -n true tests passwordless sudo.
-p prompt
Replace "authenticate" in the prompt: [sudo: PROMPT] Password: (%u = you, %h = host). -p '' prints no prompt.
-V
Print the version (sudo-rs 0.2.13-0ubuntu1.2 on Ubuntu 26.04).
-v
Refresh the cached credentials (asks for the password if needed) without running a command.

Examples

$ sudo systemctl restart nginx

run one privileged command

$ sudo !!

re-run the previous command with sudo

$ echo "x" | sudo tee /etc/foo

write to a root-owned file (redirection would NOT work)

Gotchas

Taught in

Try sudo in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.