su Command Guide for Switching User Identity
su (switch user) changes your current shell to another user account — most often root — after asking for that user’s password.
What is su, and Why Use It?
su is the older, simpler way to change identity on Linux. Where sudo runs one command as another user, su starts a whole new shell as that user. You type su, give the target user’s password, and from that point on everything you type runs as them — until you type exit.
By itself, su with no arguments means “switch to root”.
Why Learn su?
- It’s still everywhere — servers, containers, recovery mode, and older tutorials all use
su. - It’s the fallback when
sudoisn’t set up — on minimal installs or live systems, root login viasumay be your only path. - You’ll meet it in containers —
docker exec -u rootand shell scripts frequently usesu. - It pairs with
sudo -i— understanding both makes you fluent in how Unix systems handle privilege.
sugrants a full shell as the target user, which makes it more powerful — and more dangerous — thansudo cmd. Prefersudofor everyday admin work, and knowsufor the times it’s the only or best tool.
1. Switch to Root
su
Example:
$ su
Password:
root@server:/home/alice# whoami
root
root@server:/home/alice# exit
$ whoami
alice
- The password asked for is root’s password, not yours.
- Your working directory and environment stay the same — you’re root, but still sitting in alice’s home.
exitorCtrl+Dreturns you to your normal user.
2. Switch With a Full Login Environment
su - (or su --login) simulates a full login as the target user:
su -
Example:
$ su -
Password:
root@server:~# pwd
/root
root@server:~# whoami
root
-changes to the target user’s home directory, loads their environment variables, and sets their shell.- This is the form to use for admin sessions — the environment matches what a real login would give.
- Compare with
sudo -i, which gives the same root shell but authenticates with your password instead.
3. Switch to Any User
su <username> targets any account on the system:
su - postgres
Example:
# Switch to a service account
$ su - www-data
www-data@server:~$
# Switch to another real user
$ su - bob
Password:
$ whoami
bob
- You need the target user’s password — unless you’re already root (root can
suto anyone without a password). - Common on servers: databases, web servers, and other services each have their own account.
- If the account’s shell is
/usr/sbin/nologin, the switch will fail by design — service accounts usually aren’t meant for interactive login.
4. su vs sudo
| Feature | su - | sudo cmd |
|---|---|---|
| Password asked | The target user’s | Your own |
| Scope | Whole new shell | One command |
| Logging | Not logged by default | Every use logged to /var/log/auth |
| Reversal | exit back to previous user | Command ends; you were never changed |
| Policy | All-or-nothing (root’s pass) | Fine-grained via /etc/sudoers |
Example:
su - bob # I'm now bob until I exit — needs bob's password
sudo apt update # I'm still alice, but this one command ran as root
- The key safety difference:
sudokeeps you identifiable — every privileged command is logged with your name attached. - That’s why modern distributions favor
sudoand often disable the root password entirely (making plainsufail unless you’re already root).
5. su Inside Scripts and Containers
su appears constantly in Dockerfiles and automation:
# Run a command as another user inside a container
docker exec -it mycontainer su - appuser
# In a Dockerfile, switching users for a layer
USER appuser
Example:
# A non-interactive one-liner (script-friendly)
su - postgres -c "psql -c 'SELECT version();'"
- The
-cflag runs a single command string as the target user, then returns — no interactive shell. - Quoting matters: wrap the whole command in double quotes so it’s passed as one argument.
Common Pitfalls
- Forgetting
-and wondering why the environment is weird.suwithout-keeps your current directory,PATH, and environment;su -gives a clean login shell. Admin sessions almost always want-. - Expecting your own password to work.
suasks for the target’s password. If you don’t know it, you can’t switch — that’s whatsudois for. - Assuming root login is available. Many systems (Ubuntu, for one) have no root password at all;
suwill just fail. Usesudo -iinstead. - Staying root too long. An open root shell is an open door —
exitwhen you’re done. - Using
suto “fix” permissions on random files. If you find yourself switching to root tochmodfiles you don’t understand, stop and read about ownership first — see chmod and chown. - Confusing
su -withsudo -i. Both give a root shell, butsu -needs root’s password andsudo -ineeds yours. On systems without a root password, only the latter works.
Related Pages
| Page | Why it’s related |
|---|---|
| sudo | The modern, scoped alternative for admin tasks |
| id | Verify your identity before and after switching |
| whoami | Confirm which user your shell is now running as |
| /etc/passwd | Where every switchable account is defined |
| passwd | Setting the password for an account you want to su to |
| useradd | Creating the accounts that su can switch between |
su Cheatsheet
| Command | Meaning |
|---|---|
su | Switch to root, keeping current environment |
su - | Switch to root with a full login environment |
su - username | Switch to any user with a login environment |
su username | Switch without changing environment |
su - postgres -c "cmd" | Run one command as a user, non-interactively |
whoami | Confirm who you are right now |
exit / Ctrl+D | Return to the original user |