Category: Access and AuthenticationUpdated 2026-10-18

sudo

sudo is a command that lets a permitted user run a program as the superuser (root) or another user, as a security policy allows. The default policy, sudoers, checks the invoking user's own credentials, applies per-command rules and logs what was run. It is how most Linux servers grant administrative access without sharing the root password.

What sudo is and how it works

sudo (pronounced "su-do") runs a command as the superuser or another user when a security policy allows it. According to the sudo manual, the policy is checked against the invoking user's real user ID, and the default policy, sudoers, is configured in /etc/sudoers. When authentication is required, sudoers validates the invoking user's own credentials, not the target user's, so administrators never need to share the root password.

That is the main difference from su. The su manual says it defaults to "an interactive shell as root" and leaves access rules such as wheel-group membership to PAM. sudo works per command instead: the policy can limit which commands a person may run, on which host and as which user, and the sudo project describes the result as an audit trail of commands and their arguments. The rules themselves are covered on the sudoers page, and the surrounding idea is least privilege.

Everyday commands and a safe way to test

These forms cover most daily use:

sudo -l                  # list what you may run on this host
sudo -u www-data id      # run one command as another user
sudo -i                  # login shell of the target user (root by default)
sudo -s                  # shell named in $SHELL
sudo -v                  # refresh cached credentials
sudo -k                  # forget cached credentials for this session
sudo -n true             # fail instead of asking for a password
sudoedit /etc/hosts      # edit a temporary copy, copy back if changed

The manual says -i runs the target user's shell as a login shell, reads its login files and changes to its home directory, while -s runs the shell from SHELL or the passwd entry. Both give you an interactive root shell, so prefer one-off commands. To check a rule without risk, run sudo -k then sudo -n true: it exits with an error if a password would be needed or the command is not permitted. Administrators can inspect another account with sudo -l -U deploy.

Caching, logging and common mistakes

sudo is itself privileged software, so keep it updated, as the patch management guide explains.

Where sudo fits in server hardening

On a VPS, sudo is the tool that turns least privilege into practice: each person logs in as a named account with a key, direct root login stays off, and administrative work is elevated one command at a time. Put your own rules in /etc/sudoers.d, keep each one narrow, and audit them on a schedule. The users, sudo and least privilege guide walks through that audit and the sudo CVE worth patching, and the SSH hardening guide covers turning off root over SSH.

Review who can elevate and what happened recently:

getent group sudo wheel
sudo journalctl _COMM=sudo --since today

On systemd hosts the journal collects sudo's syslog messages, and _COMM= matches the process name; elsewhere read your distribution's auth log. Because full sudo access turns one stolen key into root, add a second factor for SSH for administrators, and feed these logs into the log monitoring guide.

Related terms: sudoers, Least privilege, Root login, PAM, UID 0.

Frequently asked questions

What does sudo do?

sudo runs a command as root or another user when the security policy, by default the sudoers file, permits it. It asks for the invoking user's own password when authentication is required and logs the attempt.

What is the difference between sudo and su?

su starts an interactive shell as another user, root by default, and leaves access rules to PAM. sudo applies a policy per command, host and target user, and records an audit trail, so people can be given narrow rights without the root password.

What is the difference between sudo -i and sudo -s?

sudo -i runs the target user's shell as a login shell, reads its login files and changes to its home directory. sudo -s runs the shell named in the SHELL variable, or the one in the user's passwd entry.

How long does sudo remember my password?

By default for 5 minutes, per terminal, controlled by timestamp_timeout in sudoers. Run sudo -k to discard the cached credentials for the current session, or sudo -K to remove them for all terminals.

Where does sudo log what I run?

By default sudo logs through syslog, with allowed commands at notice priority and denied commands or failed authentication at alert. On systemd hosts you can read them with journalctl, and a log file can be set with the logfile option.

Want to see where your own server stands?

Run the free, read-only server check, or open the Security Lab and watch the detect, contain, recover, verify loop in action.

Get your free server checkOpen the Security Lab