Linux Command: su
Switch user or become superuser
su (substitute user) switches the current user identity to another user, requiring that user's password. Without a username, it switches to root. su creates a new shell running as the specified user. The - flag (su -) is important โ it simulates a full login, loading the target user's environment, PATH, and shell initialization files. While su is still available, sudo is generally preferred for administrative tasks because it provides better auditing, does not require sharing the root password, and allows more granular access control.
Syntax
su [OPTION]... [USER]Key Options
-โ Full login shell (load user environment)-cโ Execute a single command-sโ Use specific shell-lโ Same as - (login shell)-pโ Preserve current environment
Examples
Switch to root
su -Output: Password:
Switch to user
su - www-dataRun single command
su -c 'cat /etc/shadow' rootPro Tips
- su without dash keeps your current environment, which can cause confusion. su - loads the target user environment properly.
- sudo uses YOUR password and logs commands. su requires the TARGET user password. sudo is preferred for admin tasks.
- Service accounts often have /sbin/nologin as shell. Use su -s /bin/bash username to get a shell.
Learn more: Full su reference โ