su
Overview
su (“substitute user”) starts a shell or runs a command as another user. On modern Ubuntu, prefer sudo for policy-controlled elevation; su still matters for switching to service accounts, recovering systems without sudo, and older procedures. runuser is a non-setuid alternative used in scripts from root.
Syntax
su [options] [-] [user [argument...]]
su [options] -c command [user]Common Options
| Option | Description |
|---|---|
- / -l / --login |
Login shell (loads target profile, resets env) |
-c cmd |
Run command then exit |
-s shell |
Use alternate shell |
-w / --whitelist-environment |
Preserve listed vars (util-linux) |
-p / -m |
Preserve environment (careful) |
Safety
su -vssudiffers wildly in environment (HOME,PATH). Prefer login form for interactive admin.
- From an unprivileged user,
suasks for the target user’s password (root password on many systems) — not your sudo password.
- Avoid
su -cwith unquoted complex scripts; quoting bugs become root bugs.
Examples with Explanations
Become root (login)
su -
# or
su - rootRun one command as root
su -c 'systemctl status ssh' rootSwitch to a service user
sudo su - postgres
sudo -u postgres -i # usually better when sudo is availablePreserve selected environment
su -w LANG,TERM - aliceNotes & Pitfalls
- Many Ubuntu systems (including Server) have no root password by default —
suto root fails; usesudo -i.
- PAM modules may restrict
sueven if the password is known.
sudo suworks but obscures audit trails compared tosudo -i/ directsudo cmd.
Additional Resources
man su
man runuser