On Unix-like operating systems, the su command changes the current user ID to that of the superuser, or another specified user.

This page covers the GNU/Linux version of su.

Description

The su command, which is short for substitute user or switch user, enables the current user to act as another user during the current login session.

  • Description
  • Syntax
  • Configuration
  • Exit values
  • Examples
  • Related commands
  • Linux commands help

Syntax

su [options] [username]

If no username is specified, su defaults to becoming the superuser (root).

Additional arguments may be provided after the username, in which case they are supplied to the user’s login shell. In particular, an argument of -c causes the next argument to be treated as a command by most command interpreters. The command will be executed by the shell specified in /etc/passwd for the target user.

The optional argument “-” (a dash) can provide an environment similar to what the user would expect had the user logged in directly.

You can use the “–” argument to separate su options from the arguments supplied to the shell.

The user will be prompted for a password, if appropriate. Invalid passwords produce an error message. All attempts, both valid and invalid, are logged to detect abuse of the system.

The current environment is passed to the new shell. The value of $PATH is reset to “/bin:/usr/bin” for normal users, or “/sbin:/bin:/usr/sbin:/usr/bin” for the superuser. This may be changed with the ENV_PATH and ENV_SUPATH definitions in /etc/login.defs.

A subsystem login is indicated by the presence of a “*” as the first character of the login shell. The given home directory will be used as the root of a new file system which the user is actually logged into.

Options

Configuration

The following configuration variables, located in the file /etc/login.defs, change the behavior of su:

  • The shell specified with –shell.If –preserve-environment is used, the shell specified by the $SHELL environment variable.The shell indicated in the /etc/passwd entry for the target user./bin/sh if a shell could not be found by any above method.

If the target user has a restricted shell, this option has no effect (unless su is called by root).

Note that the default behavior for the environment is the following:

  • The $HOME, $SHELL, $USER, $LOGNAME, $PATH, and $IFS environment variables are reset.
  • If –login is not used, the environment is copied, except for the variables above.
  • If –login is used, the $TERM, $COLORTERM, $DISPLAY, and $XAUTHORITY environment variables are copied if they were set.
  • Other environments might be set by PAM modules.

Exit values

On success, su returns the exit value of the command it executed.

If this command was terminated by a signal, su returns the number of this signal plus 128.

If su has to kill the command (because it was asked to terminate, and the command did not terminate in time), su returns 255.

Some exit values from su are independent from the executed command:

Examples

su - hope

Switch the current user ID to that of user hope, and set the environment to hope’s login environment.

  • How to become root in Linux.

csh — The C shell command interpreter.env — Report the value of environment variables.ksh — The Korn shell command interpreter.login — Begin a session on a system.sh — The Bourne shell command interpreter.sudo — Execute a command as the superuser.