sudo Run a command as another user
Runs one command with the rights of another user, most often the super user (root). You use it for tasks that normal accounts are not allowed to do, such as editing system files or changing file owners. It asks for your own password, not root's.
Synopsis
sudo [-u user] command [arg ...]
sudo -i | -s
sudo -l | -v | -k
Common options
| Option or form | What it does |
|---|---|
-u user | Run the command as this user instead of root |
-i | Start a login shell as the target user, with that user's environment and home folder |
-s | Start a shell as the target user, keeping more of your own environment than -i |
-l | List what you are allowed to run with sudo. Give a command to check only that one |
-v | Refresh the password timestamp without running a command |
-k | Forget the cached password, so the next sudo asks again |
-E | Keep your environment variables when running the command |
-e | Edit a file as the target user using a temporary copy, rather than running an editor as root |
Examples
sudo ls /var/root
sudo -l
sudo -u alice whoami
sudo chown alice:staff report.pdf
sudo -e /etc/hosts
sudo -k
Notes
- sudo gives the command full power over the machine. Read the command before you press Enter, and never paste a command that starts with sudo from a source you do not trust.
- Prefer sudo with a single command over sudo -i or sudo -s. A root shell keeps full rights until you leave it, and mistakes made in it can be hard to undo.
- Redirection happens in your shell, before sudo runs. So sudo echo hi > /etc/file fails with permission denied, because the > is yours. Use sudo tee /etc/file instead, or sudo sh -c '... > file'.
- sudo only works for users the system administrator has allowed. On macOS that means admin accounts. After a successful password entry, sudo remembers it for a few minutes.
- Be most careful with sudo rm -r and sudo chmod -R. A mistake made as root has no safety net, so check the path before you run it.
See also
Last reviewed 2026-10-07.