How-to

sudo vs su: Which Way to Become Root

Both commands give you another account's powers. They differ in whose password they want, what they record and how much they hand over at once.

What is the core difference?

su means "substitute user": it starts a shell as another account, root by default, and asks for that account's password. sudo runs one command as another account after checking a policy file and, usually, your own password. With su, anyone who knows the root password has all of root. With sudo, each admin keeps a personal password, gets the rights the policy grants and leaves a log entry for every command.

Where do su and sudo part ways?

Aspectsusudo
Password askedTarget account'sYour own (or none, by rule)
ScopeWhole shell sessionOne command, or a shell on request
Fine-grained rulesNoYes, per user, group and command
LoggingSession start onlyEvery command with arguments
Shared secret neededYes, the root passwordNo
Works when root is lockedNoYes

What changes when I add a dash to su?

The dash, also written -l or --login, starts a full login shell for the target account: its home folder, its PATH, its shell startup files. Plain su switches identity but keeps much of your current environment, including your working folder and many variables. That can lead to odd results, such as root running a program from your personal PATH. When you use su at all, prefer su -.

su -                  # root login shell, asks for the root password
su - postgres -c 'psql -c "select version()"'   # one command as another account

What are the sudo equivalents?

sudo -i gives a root login shell, the sudo counterpart of su -. sudo -s gives a root shell that keeps more of your environment, like plain su. sudo -u postgres psql runs one command as another user. The popular sudo su - works, but it is a roundabout way of writing sudo -i and the log only records that you ran su.

How do I act as an account that has no login shell?

Service accounts such as www-data, nginx or postgres often have /usr/sbin/nologin as their shell, so su - www-data fails with "This account is currently not available". Name a shell explicitly:

sudo -u www-data -s /bin/bash
sudo -u www-data php /srv/shop/bin/console cache:clear
su -s /bin/bash -c 'whoami' www-data        # as root, without sudo

Running maintenance commands as the service account keeps file ownership correct; running them as root creates root-owned cache files the service cannot later change.

Which one do the images expect?

Ubuntu locks the root password by default on its own installs and relies entirely on sudo. Debian, Rocky Linux, AlmaLinux and CentOS Stream support both. Our machines arrive with a root password shown in the panel, so both su and direct root login work at first. The usual next step is a personal admin account with sudo, explained in Create a sudo user on Debian and Sudo user on CentOS Stream, Rocky, Alma, after which the root password can be locked with passwd -l root.

Can su be restricted?

Yes. On Enterprise Linux and Debian, the PAM module pam_wheel can limit su to members of one group. Uncomment the line auth required pam_wheel.so use_uid in /etc/pam.d/su; on Debian, add group=sudo to point it at the sudo group. Then only admins may even try su, which removes one more door for anyone who learned the root password.

Which should I use day to day?

sudo, for nearly everything. It keeps a trail, avoids sharing a root password among people, and lets you hand out narrow rights such as "may restart the web server". Keep su for rare cases: switching to a service account on a system without sudo, or recovery when sudo itself is broken by a bad sudoers file. In that last case, su - with the root password, or pkexec visudo on desktop systems, gets you back in to fix it. On servers without a root password and with broken sudo, the remaining fix is a reinstall from the panel, so check every sudoers change with visudo -c. Details on sudo rules are in The sudo command.

Keep Reading

The sudo command

How sudo decides who may run what, the flags you use daily, safe sudoers rules, logging and the habits that keep root powers contained.

Open

SSH root login settings

What each PermitRootLogin value does, how to move daily work to a sudo user and when key-only root login is reasonable.

Open

Linux user management

The full life of an account on a server: create, group, change, expire, lock, audit and delete, with the files behind each step.

Open

Create a sudo user on Debian

Add an admin account on Debian 13 or 12, give it sudo rights, install your SSH key and retire daily root logins safely.

Open

FAQ

Quick Answers

Your own account's password, unless a rule says NOPASSWD.

The password of the account you switch to, root by default.

It starts a full login shell with the target account's environment, home folder and PATH.

sudo -i, which opens a root login shell and logs the session start under your name.

Use sudo -u www-data followed by the command, or add -s /bin/bash for a shell since the account has no login shell.