su · Linux command

Also known as: su command

On this page 7

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.

CommandTarget userEnvironment
surootKeeps most of the caller’s environment
su - or su -lrootFull login environment of root
su - alicealiceLogin environment of alice
su -c 'cmd' alicealiceRuns one command, then returns
su -s /bin/sh svcsvcUses a different shell for the session
Common su invocations

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.

AspectPlain susu – (login)
Working directoryUnchangedTarget user’s home directory
PATHPartly kept or reset by policySet from the target’s profile
HOME, SHELL, USERSet for the target userSet for the target user
Profile scriptsNot readRead as in a normal login
Security riskHigher, inherited settingsLower, clean environment
Effect of the login option on the new shell

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.

Propertysusudo
Password askedTarget user’s passwordCaller’s own password
Scope of rightsFull shell as the targetPer-command rules in sudoers
Audit trailSession start in auth logEach command logged
Shared secretRoot password known to several peopleNo shared root password needed
Typical defaultTraditional Unix systemsUbuntu, macOS, many cloud images
Key differences between su and sudo

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_uid allows 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 nologin shell refuse interactive su sessions unless -s names another shell.
  • Session handling: su -P on recent util-linux versions allocates a pseudo-terminal, which reduces terminal injection risks.

The following table maps frequent problems to their causes and remedies.

SymptomLikely causeRemedy
Authentication failure for rootRoot password locked or unsetUse sudo -i or set a root password
Permission denied despite correct passwordpam_wheel restriction activeAdd the user to the wheel group
Commands not found after suPlain su kept a limited PATHUse su - for a login shell
Account refuses loginShell set to nologinRun su -s /bin/bash user if appropriate
GUI programs fail to startDisplay authorization not passedUse the desktop’s own elevation tools
Common su problems

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.