Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Permissions and Users

Linux sudo command

Reference for the Linux sudo command: run one command as root or another user, list your rights with -l, edit files safely with sudoedit, and avoid common mistakes.

sudo runs a single command with the privileges of another user, most often root. It checks the rules in /etc/sudoers (and the files in /etc/sudoers.d/), asks for your own password, and then starts the command. Because every use is logged, sudo gives administrators fine-grained control and an audit trail that sharing the root password never could.

Rules can be narrow ("user deploy may run only systemctl restart app") or broad ("members of group sudo may run anything"). You never edit the file with an ordinary editor; you use visudo, which refuses to save a syntax error that would lock everyone out.

Terminal
sudo [OPTION]... COMMAND [ARG]...

Try it

sudo is not installed in the sandbox, so the first example checks for it and shows how a script should react. The second prints example sudoers rules and parses them (read-only content, ordinary text tools).

Bash Check for sudo in a script

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Reading sudoers rules (sample file)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
sudo [-u USER] [-g GROUP] [-E] [-H] [-n] COMMAND [ARG]...
sudo -i | -s [COMMAND]
sudo -l | -v | -k | -K
sudoedit FILE...
Option Description
-u USER Run as USER instead of root (a name, or #UID).
-g GROUP Run with GROUP as the primary group.
-i Simulate a full login for the target user (like su -).
-s Start the target user's shell without a login environment.
-E Preserve your environment, if the rules allow it.
-H Set HOME to the target user's home directory.
-n Non-interactive: fail instead of asking for a password. Good for scripts.
-l List what you may run (add a command to check one command).
-v Refresh the cached credentials without running anything.
-k, -K Forget cached credentials for this terminal / for all.
-e FILE or sudoedit Edit a file with your own editor, writing the result as the target user.

Returns: the exit status of the command. If the rules deny it or authentication fails, sudo exits with 1 and writes a message to the log.

Usage notes

  • Redirections are not elevated. In sudo echo x > /etc/file the shell opens the file as you, so it fails. Use echo x | sudo tee /etc/file >/dev/null or sudo sh -c 'echo x > /etc/file'.
  • Password caching. After a successful login, sudo remembers it for about 5 minutes per terminal (timestamp_timeout).
  • Environment is cleaned. sudo resets PATH to secure_path and drops most variables. Use sudo VAR=value cmd for one variable, or env_keep in the rules.
  • Check with sudo -l to see exactly what is allowed, including NOPASSWD: entries. Rules are matched top to bottom and the last match wins, a frequent source of confusion.
  • Wildcards in sudoers are dangerous. A rule such as /bin/cp * /srv/ can be abused with extra arguments. Prefer a small wrapper script that checks its input.
  • Always edit with visudo (visudo -f /etc/sudoers.d/deploy for a drop-in file; keep it mode 0440). Files whose names contain a dot or end in ~ are ignored.
  • Scripts: do not hard-code sudo in a script that may run as root or in a container without it; use the SUDO= pattern from the example.
  • Logging. Each use is recorded in the system log (or /var/log/auth.log / /var/log/secure), including the command line.