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 nginxrun one privileged command
$ sudo !!re-run the previous command with sudo
$ echo "x" | sudo tee /etc/foowrite to a root-owned file (redirection would NOT work)
Gotchas
sudo echo hi > /etc/foofails: the shell opens the redirection as YOU, before sudo ever runs. Use| sudo teeorsudo sh -c '...'.- Credentials are cached for 15 minutes per terminal, and every sudo restarts the 15 minutes.
- Ubuntu 25.10 and newer ship sudo-rs as sudo. Its prompt is
[sudo: authenticate] Password:(classic sudo:[sudo] password for learner:), each typed character shows as*(pwfeedback is on; TAB hides them), a wrong password sayssudo: Authentication failed, try again., and a user sudoers does not allow is refused before any password prompt:sudo: I'm sorry learner. I'm afraid I can't do that.
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.