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
| Option | What it does |
|---|---|
-, -l, --login | login 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 shell | run the given shell instead of the one recorded in /etc/passwd |
-m, -p | leave the environment alone: HOME, SHELL, USER stay the caller's |
-w VAR1,VAR2 | with -, keep the listed variables |
-g group | primary group (root only) |
-G group | supplementary group (root only) |
-P | allocate 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
| Command | Whose password | Environment | Directory |
|---|---|---|---|
su alice | alice | HOME, SHELL, USER, LOGNAME — alice's, other variables stay yours | current |
su - alice | alice | alice's clean environment, ~/.profile is read | /home/alice |
su - | root | root's clean environment | /root |
sudo -i | yours | like su -, but by sudoers rules | /root |
sudo -u alice -i | yours | like su - alice | /home/alice |
sudo -s | yours | root shell, environment mostly yours | current |
/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
suwithout-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 — usesu -.-ctakes a single string.su alice -c ls -lruns onlyls, and-lgoes tosu's own options. Quote the whole command.- Double quotes are expanded by your shell. In
su - alice -c "echo $HOME",/rootis substituted beforesueven starts. Use single quotes. - Root has no password on Ubuntu (
/etc/shadowhas*), sosu -from a regular user always ends with "Authentication failure". Usesudo -i. - Without a terminal
suwon't ask for a password in a script: input fromecho password | su …works only by accident and is insecure. In scripts root callssuwithout a password; everyone else needssudo -nwith aNOPASSWDrule. - An expired password or account blocks even
sufrom root: "Your account has expired" or a demand to change the password. Checkchage -l user. su -sfor a nologin user is a deliberate bypass: it's allowed, but don't give such users a regular shell in/etc/passwd.