su — Switch User in a Running Session
Practical guide to su: switch identity in a running shell, open a login shell with su -, run commands as another user, and how it differs from sudo.
su (substitute user) switches your identity in a running session without logging out and back in. With no argument you become root; with su - <user> you drop into another account's full environment. Unlike sudo, su asks for the target user's password rather than your own. This guide covers the login shell, running single commands, and why sudo is usually the better, more auditable choice on modern systems.
Basic Usage
su — Switch to root. Prompts for root's password. Does not change the working directory or environment.
susu - — Switch to root with a full login shell. Loads root's environment (.profile, .bashrc). Recommended over plain 'su'.
su -su <user> — Switch to a specific user. Prompts for that user's password.
su deploysu - <user> — Switch to a specific user with a full login shell. Changes directory to that user's home and loads their environment.
su - www-datasu -l <user> — Same as 'su -
su -l postgresRun Commands Without Switching Shell
su -c '<command>' <user> — Run a single command as another user without opening an interactive shell.
su -c 'whoami' deploysu - -c '<command>' — Run a single command as root with a login environment.
su - -c 'systemctl restart nginx'su -s /bin/bash <user> — Switch to a user using a specific shell, even if their login shell is different (e.g. /sbin/nologin).
su -s /bin/bash www-datasu -s /bin/bash -c '<command>' <user> — Run a command as a system user that has no interactive login shell.
su -s /bin/bash -c 'php artisan queue:work' www-dataEnvironment Control
su (without -) — Keeps the current user's environment variables (PATH, HOME, etc.). Only the user identity changes.
su rootsu - (with -) — Creates a fresh login environment for the target user. HOME, PATH, SHELL, and USER are all set to the target user's values.
su - rootsu -m <user> — Same as -p. Preserve the current environment when switching user.
su -m deploysu -p <user> — Preserve the current environment (PATH, HOME, etc.) when switching user.
su -p deploysu vs sudo
su - root — Requires root's password. Opens a full root login shell.
su - rootsudo -i — Requires the current user's password. Opens a full root login shell. Preferred on modern systems.
sudo -isudo su - — Use sudo to become root without knowing root's password. Common pattern when root login is disabled.
sudo su -sudo -u <user> -s — Open a shell as another user using sudo (no need to know the target user's password).
sudo -u www-data -sOptions & Flags
su --help — Show help and available options.
su --helpsu -w VAR <user> — Whitelist specific environment variables to keep when switching user (util-linux su).
su -w TERM,COLORTERM deployexit — Exit the switched user session and return to the previous user.
exitCtrl+D — Send EOF to exit the switched user shell. Equivalent to 'exit'.
Ctrl+D Conclusion
The most important difference hides in a single dash: su - starts a real login shell and loads the target's full environment (HOME, PATH, SHELL), whereas su <user> only swaps the identity and keeps your current environment – which otherwise leads to surprising path and configuration issues. For one-off tasks su -c '<cmd>' is enough, and su -s /bin/bash lets you enter system accounts like www-data whose login shell is set to /sbin/nologin. The security distinction is key: su asks for the target user's password (root's password when switching to root), while sudo asks for your own. That is why sudo is usually the better choice on modern systems – authorizable per command, fully auditable, and the direct root login is commonly disabled with a locked password (!) in favour of sudo. Treat both with care: every switch is a privilege escalation.
Further Reading
- Wikipedia: su (Unix) – background on the command and its history
- util-linux su manual – the authoritative reference for su's options and behaviour