su is the Linux command that switches the current user identity by starting a new shell, or running a single command, with the privileges of another account. Without arguments it targets the root account, and it normally asks for the password of the target user, not of the person typing the command. Within Linux permissions, the su command is one of the two classic ways to obtain elevated rights, the other being sudo.
The name is usually read as substitute user or switch user. On most Linux distributions, su is provided by the util-linux package and authenticates through PAM (Pluggable Authentication Modules), which lets administrators restrict who may use it and log every attempt.
How the su command changes identity
su is installed as a setuid-root binary, so it starts with root privileges regardless of who runs it. It asks PAM to authenticate the caller, typically by checking the target account’s password against the shadow file. When authentication succeeds, su calls setgid, initgroups and setuid to adopt the target user’s UID, GID and supplementary groups, then executes that user’s login shell from the password database.
Root is a special case: when root runs su, no password is requested, because root already holds every privilege the target could have. The new shell is a child process, so typing exit returns the operator to the original session and identity.
The table below shows the most common forms of the command and what each one does.
| Command | Target user | Environment |
|---|---|---|
su | root | Keeps most of the caller’s environment |
su - or su -l | root | Full login environment of root |
su - alice | alice | Login environment of alice |
su -c 'cmd' alice | alice | Runs one command, then returns |
su -s /bin/sh svc | svc | Uses a different shell for the session |
Login shell versus plain su
The difference between su and su - is the most important detail of the command. A plain su keeps much of the caller’s environment, including the current directory and many variables, while su - starts a login shell that reads the target user’s profile files and builds a clean environment.
Keeping the old environment can be dangerous. If root inherits an ordinary user’s PATH variable, a malicious program placed in a writable directory early in that PATH could run with root privileges. Many administrators therefore treat su - as the only acceptable form for becoming root.
| Aspect | Plain su | su – (login) |
|---|---|---|
| Working directory | Unchanged | Target user’s home directory |
| PATH | Partly kept or reset by policy | Set from the target’s profile |
| HOME, SHELL, USER | Set for the target user | Set for the target user |
| Profile scripts | Not read | Read as in a normal login |
| Security risk | Higher, inherited settings | Lower, clean environment |
su compared with sudo
su and sudo both grant elevated rights, but they follow different trust models. su requires knowledge of the target account’s password and grants a full shell, whereas sudo asks for the caller’s own password and grants only what the sudoers policy allows.
| Property | su | sudo |
|---|---|---|
| Password asked | Target user’s password | Caller’s own password |
| Scope of rights | Full shell as the target | Per-command rules in sudoers |
| Audit trail | Session start in auth log | Each command logged |
| Shared secret | Root password known to several people | No shared root password needed |
| Typical default | Traditional Unix systems | Ubuntu, macOS, many cloud images |
Because a shared root password cannot be traced to one person, many organizations prefer sudo and lock the root password entirely. In that setup su still works for administrators through sudo su - or the equivalent sudo -i, but the accountability comes from sudo.
Restricting and auditing su
Access to su is controlled through PAM configuration in /etc/pam.d/su. Enabling the pam_wheel.so module limits su to members of the wheel group (or another named group), so a stolen ordinary account cannot be used to guess the root password interactively.
- Group restriction:
auth required pam_wheel.so use_uidallows only wheel members to switch to root. - Logging: successful and failed attempts are recorded in the authentication log or the systemd journal.
- Service accounts: accounts with a
nologinshell refuse interactive su sessions unless-snames another shell. - Session handling:
su -Pon recent util-linux versions allocates a pseudo-terminal, which reduces terminal injection risks.
The following table maps frequent problems to their causes and remedies.
| Symptom | Likely cause | Remedy |
|---|---|---|
| Authentication failure for root | Root password locked or unset | Use sudo -i or set a root password |
| Permission denied despite correct password | pam_wheel restriction active | Add the user to the wheel group |
| Commands not found after su | Plain su kept a limited PATH | Use su - for a login shell |
| Account refuses login | Shell set to nologin | Run su -s /bin/bash user if appropriate |
| GUI programs fail to start | Display authorization not passed | Use the desktop’s own elevation tools |
When su is the right tool
su remains useful when an administrator needs a long interactive session as another account, such as testing a service user’s file permissions or reproducing an environment problem. Running su - postgres to work with a database cluster is a standard example, because many tools expect to run as that service account.
For routine administrative tasks, sudo is generally the better default: it limits rights to specific commands and records each action. A sound rule is to avoid staying in a root shell longer than necessary, because every typing error then has system-wide consequences.
A non-technical view
On a Linux computer, every person and program works under an account. Each account has its own permissions. The su command is like borrowing another person’s key card. If you know their password, you can work as them for a while. You get exactly the access they have.
Most often the key card belongs to root. That account can do anything on the system. This is powerful but risky. Careful administrators use it briefly. Then they type exit to return to their own, safer account.
A concrete example
A database administrator at a mid-sized hosting company needs to check why nightly backups of a PostgreSQL cluster fail on one server. The backup script runs as the postgres service account, so testing it under a personal account would give misleading results. The administrator logs in with a personal account, runs sudo su - postgres, and gets a clean login shell as postgres with the correct home directory and environment.
Running the backup script by hand reveals a permission error on the target directory, which belongs to another group after a recent storage migration. After fixing the group ownership with chgrp and setting the mode with chmod, the administrator reruns the script in the same su session and confirms that it completes in about twelve minutes. Typing exit returns the administrator to the personal account, and the authentication log shows who switched to postgres and when.