Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 sudo isn’t set up — on minimal installs or live systems, root login via su may be your only path.
  • You’ll meet it in containers — docker exec -u root and shell scripts frequently use su.
  • It pairs with sudo -i — understanding both makes you fluent in how Unix systems handle privilege.

su grants a full shell as the target user, which makes it more powerful — and more dangerous — than sudo cmd. Prefer sudo for everyday admin work, and know su for 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.
  • exit or Ctrl+D returns 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 su to 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

Featuresu -sudo cmd
Password askedThe target user’sYour own
ScopeWhole new shellOne command
LoggingNot logged by defaultEvery use logged to /var/log/auth
Reversalexit back to previous userCommand ends; you were never changed
PolicyAll-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: sudo keeps you identifiable — every privileged command is logged with your name attached.
  • That’s why modern distributions favor sudo and often disable the root password entirely (making plain su fail 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 -c flag 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. su without - keeps your current directory, PATH, and environment; su - gives a clean login shell. Admin sessions almost always want -.
  • Expecting your own password to work. su asks for the target’s password. If you don’t know it, you can’t switch — that’s what sudo is for.
  • Assuming root login is available. Many systems (Ubuntu, for one) have no root password at all; su will just fail. Use sudo -i instead.
  • Staying root too long. An open root shell is an open door — exit when you’re done.
  • Using su to “fix” permissions on random files. If you find yourself switching to root to chmod files you don’t understand, stop and read about ownership first — see chmod and chown.
  • Confusing su - with sudo -i. Both give a root shell, but su - needs root’s password and sudo -i needs yours. On systems without a root password, only the latter works.

PageWhy it’s related
sudoThe modern, scoped alternative for admin tasks
idVerify your identity before and after switching
whoamiConfirm which user your shell is now running as
/etc/passwdWhere every switchable account is defined
passwdSetting the password for an account you want to su to
useraddCreating the accounts that su can switch between

su Cheatsheet

CommandMeaning
suSwitch to root, keeping current environment
su -Switch to root with a full login environment
su - usernameSwitch to any user with a login environment
su usernameSwitch without changing environment
su - postgres -c "cmd"Run one command as a user, non-interactively
whoamiConfirm who you are right now
exit / Ctrl+DReturn to the original user