keepcalculator

06 Numbers and Powers

Sudo vs su: Key Differences Explained

Card No. 06·78841 · Updated · Standard formula, shown below

The main difference between sudo and su is that sudo runs a single command with elevated privileges using your own password, while su switches you to another user (usually root) and requires that user’s password. This article explains how each works, their security trade-offs, and when to use one over the other.

Background: The Unix Privilege Model

Both sudo and su are tools for privilege escalation on Unix-like systems (Linux, macOS, BSD). They exist because everyday tasks should run with limited permissions to reduce the risk of accidental or malicious damage. The root user (or superuser) has unrestricted access, so it’s dangerous to always work as root. Instead, you log in as a normal user and elevate privileges only when needed.

The su command (short for “switch user”) has been around since the early days of Unix. It allows you to become another user, most commonly root, by providing that user’s password. Once you switch, you remain in that user’s context until you type exit. The sudo command (“superuser do”) was introduced later (at the University at Buffalo in the 1980s) to allow authorized users to run specific commands as root without sharing the root password. It uses a configuration file (/etc/sudoers) to define who can run what.

How su Works

When you type su (or su - to also load the target user’s environment), you are prompted for the target user’s password. If you know the root password, you can become root. Once you are root, you can run any command without further authentication until you log out. This is convenient for long administrative sessions, but it has drawbacks:

  • You must share the root password with anyone who needs admin access.
  • There is no audit trail of who did what because everyone uses the same root account.
  • If you forget to log out, you remain as root, increasing the risk of mistakes.

In practice, su is often used to switch to a non-root user (e.g., su - john) to test permissions or run a shell as another user.

How sudo Works

With sudo, you run a single command with elevated privileges. For example, sudo apt update runs the package manager as root. You are prompted for your own password, not root’s. The system checks /etc/sudoers to see if your user is allowed to run that command. If yes, the command executes as root (or another specified user).

Key features of sudo:

  • Password caching: By default, sudo remembers your password for 15 minutes (configurable), so you don’t have to re-enter it for every command in a session.
  • Auditing: Every sudo command is logged (typically in /var/log/auth.log), so you can see who ran what.
  • Fine-grained control: The sudoers file can allow specific users to run only certain commands, without giving full root access.
  • No root password needed: Users don’t need to know the root password, which is more secure and easier to manage.

Security and Safety Considerations

Security experts generally recommend sudo over su for administrative tasks. The main reasons are:

  • Least privilege: With sudo, you only elevate for the command you need, not for an entire session. This limits the window of exposure if you make a mistake.
  • Accountability: Each sudo command is tied to your username, so there’s a record of actions. With su, everyone appears as root, making it hard to trace who did what.
  • Password sharing: With su, you must share the root password, which is a security risk. If an employee leaves, you have to change the root password. With sudo, you can simply revoke that user’s sudo privileges.

However, sudo is not inherently safer if misconfigured. A user with full sudo access (ALL=(ALL) ALL) can effectively do anything, including changing the sudoers file itself. Also, sudo’s password caching can be a risk if you step away from an unlocked terminal; someone could run sudo commands without a password. The timestamp_timeout setting can be set to 0 to require a password every time.

Common Problems and Misconceptions

A common mistake is thinking that sudo and su are interchangeable. They are not. sudo runs a single command; su starts a new shell. If you type sudo su, you are using sudo to run the su command, which then switches you to root (usually without a password because you’re already root). This is a common way to get a root shell without knowing the root password, but it’s often discouraged because it bypasses sudo’s logging.

Another misconception: “sudo is only for root.” Actually, sudo can run commands as any user, e.g., sudo -u www-data ls /var/www. The default is root, but that’s configurable.

People also confuse the password prompts. With su, you need the target user’s password (often root). With sudo, you need your own password. If you’re the only user and you know the root password, both might seem the same, but the behavior differs.

How to Choose Between sudo and su

Use sudo when:

  • You need to run a single administrative command (e.g., sudo systemctl restart nginx).
  • You want an audit trail of who ran what.
  • You don’t want to share the root password.
  • You’re on a system where root login is disabled (common on Ubuntu).

Use su when:

  • You need an interactive root shell for a long session (e.g., editing multiple config files).
  • You want to switch to a non-root user to test permissions.
  • You’re on a system where sudo isn’t installed or configured.

In practice, many distributions (like Ubuntu) configure sudo by default and don’t even set a root password. In that case, su to root won’t work unless you set a root password first. On other systems (like older CentOS), root has a password and su is the default.

Alternatives and Related Tools

Besides sudo and su, there are other privilege escalation tools:

  • doas (from OpenBSD): A minimal alternative to sudo, designed to be simpler and more secure. It’s available on many Linux distributions.
  • pkexec (PolicyKit): Used for graphical applications that need elevated privileges. It prompts for authentication via a GUI dialog.
  • run0 (from systemd): A newer tool that runs commands as root without setuid binaries, using systemd’s service manager.

These tools aim to address some of sudo’s complexity or security issues, but sudo remains the most widely used.

Frequently Asked Questions

Is sudo safer than su?

Generally, yes. Sudo gives you granular control over who can run what, logs all commands, and doesn’t require sharing the root password. Su requires the root password and gives full root access for the entire session, which is riskier. However, both are safe if used correctly and configured properly.

Can I use sudo to become root?

Yes, you can run sudo -i or sudo su - to get a root shell. The first is the preferred way because it logs the action and sets the root environment. The second works but is often discouraged because it bypasses some of sudo’s logging.

Why does sudo ask for my password but su asks for root’s password?

Because they work differently. Sudo checks your user against the sudoers list and asks for your password to confirm it’s really you. Su asks for the target user’s password because you’re trying to authenticate as that user. This is a fundamental difference in their design.

What does ‘sudo’ stand for?

‘sudo’ stands for “superuser do.” It’s a play on the Unix ‘su’ (switch user) and ‘do’ (execute a command). The original implementation was called ‘sudo’ and the name stuck.

Can I use sudo without a password?

Yes, you can configure the sudoers file to allow passwordless sudo for specific commands or users. For example, adding username ALL=(ALL) NOPASSWD: ALL allows passwordless sudo for that user. This is convenient but reduces security, so it should be used sparingly.

Does sudo work on macOS?

Yes, macOS includes sudo by default. It works similarly to Linux, but the sudoers file is managed differently. On macOS, you can also use su to switch to root, but root is disabled by default (you need to enable it via Directory Utility).

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →