← Full reference

su

Switch user in the shell — su, su -, su -c, su -s; how it differs from sudo -i

su (substitute user) starts a shell or a single command as another user — root by default. It asks for the target user's password; root can switch to anyone without a password. For a one-off command with root privileges sudo is usually handier; su is more often needed when root runs something as a service account.

Syntax

su [options] [-] [user [arguments…]]
su - alice                       # log in as alice with her environment
su alice -c 'command'            # run one command
su -s /bin/bash svc -c 'command' # bypass nologin in /etc/passwd

Options

OptionWhat it does
-, -l, --loginlogin shell: clean environment, change to the home directory, read ~/.profile
-c 'command'run one command via shell -c and exit; the exit code is the command's
-s shellrun the given shell instead of the one recorded in /etc/passwd
-m, -pleave the environment alone: HOME, SHELL, USER stay the caller's
-w VAR1,VAR2with -, keep the listed variables
-g groupprimary group (root only)
-G groupsupplementary group (root only)
-Pallocate a new pseudo-terminal

Arguments after the user name are passed to the shell: in su alice -c 'echo $0 $1' x y, $0 will be x and $1 — y.

su, su - and sudo

CommandWhose passwordEnvironmentDirectory
su alicealiceHOME, SHELL, USER, LOGNAME — alice's, other variables stay yourscurrent
su - alicealicealice's clean environment, ~/.profile is read/home/alice
su -rootroot's clean environment/root
sudo -iyourslike su -, but by sudoers rules/root
sudo -u alice -iyourslike su - alice/home/alice
sudo -syoursroot shell, environment mostly yourscurrent

/sbin/nologin and service accounts

Service users (www-data, nobody, those created with useradd -r -s /usr/sbin/nologin) have the shell /usr/sbin/nologin (same as /sbin/nologin) in /etc/passwd. It prints "This account is currently not available." and exits with code 1 — so su svc and su svc -c '…' don't work. To run a command as such a user, root specifies the shell explicitly: su -s /bin/bash svc -c '…', or uses sudo -u svc command.

Examples

# become root with a full environment
su -
sudo -i            # the same if root has no password (as on Ubuntu)

# one command as alice; the quotes are required
su - alice -c 'whoami; pwd'

# variables inside -c are expanded by alice's shell — use single quotes
su - alice -c 'echo $HOME'

# a service user with nologin
useradd -r -s /usr/sbin/nologin svc
su svc -c id                       # This account is currently not available.
su -s /bin/bash svc -c id          # works
sudo -u svc id                     # works, sudo doesn't start a shell

# pass one variable into a clean environment
APP_ENV=prod su -w APP_ENV - alice -c 'echo $APP_ENV'

# run as alice with primary group devs (root only)
su -g devs alice -c id

Pitfalls

  • su without - keeps your environment. PATH, application variables and the current directory stay the same, so a command may behave differently than on a real login of that user. Need a "clean" session — use su -.
  • -c takes a single string. su alice -c ls -l runs only ls, and -l goes to su's own options. Quote the whole command.
  • Double quotes are expanded by your shell. In su - alice -c "echo $HOME", /root is substituted before su even starts. Use single quotes.
  • Root has no password on Ubuntu (/etc/shadow has *), so su - from a regular user always ends with "Authentication failure". Use sudo -i.
  • Without a terminal su won't ask for a password in a script: input from echo password | su … works only by accident and is insecure. In scripts root calls su without a password; everyone else needs sudo -n with a NOPASSWD rule.
  • An expired password or account blocks even su from root: "Your account has expired" or a demand to change the password. Check chage -l user.
  • su -s for a nologin user is a deliberate bypass: it's allowed, but don't give such users a regular shell in /etc/passwd.

See also

sudo · id · useradd · chage · env