sudo command (Linux / bash / sh / zsh)
What it does
Run an authorized command with another user's privileges. When people search for the sudo command linux admin, they usually want sudo's everyday flags, safe examples, and how it differs from related tools. Below you'll find syntax, options, and copyable examples for Bash/sh/zsh.
Run an authorized command with another user's privileges.
Syntax
sudo [OPTION]... COMMAND
Common options
| Flag / option | Meaning |
|---|---|
| -u USER | Run as a selected user instead of root. |
| -i | Start the target user's login shell. |
| -k | Invalidate cached credentials. |
| -l | List allowed commands. |
Practical examples
Administrative command
sudo systemctl restart nginx
Restarts nginx with elevated privileges.
Run as service user
sudo -u www-data whoami
Executes as www-data.
Review access
sudo -l
Lists the current user's sudo policy.
Common mistakes
- Running an entire shell with sudo when only one command needs elevation.
- Using sudo with redirected output and forgetting the shell performs redirection before sudo.
Related commands
FAQ
What does sudo do in Linux?
Run an authorized command with another user's privileges.
How do I use sudo from the terminal?
Start with `sudo [OPTION]... COMMAND`, then choose only the options needed for the task.
What is a practical sudo example?
Try `sudo systemctl restart nginx` — Restarts nginx with elevated privileges.
Can I use sudo in a shell script?
Yes. sudo works in Bash, sh, and Zsh, but scripts should handle quoting, errors, and command output explicitly.
Browse the full Linux and Windows command reference to search more bash, CMD, PowerShell, Conda, and GitHub CLI commands.