su Command Cheatsheet
The su (substitute user) command allows you to run commands or start a new shell session as another user. It is most commonly used to switch to the root user, but can be used for any user account.
Common Usage and Options
| Command | Description |
|---|---|
su | Switches to the root user by default. You will be prompted for the **root user's password**. |
su - | Switches to the root user, but also loads the root user's environment variables and home directory. This is the recommended way to get a clean root shell. |
su [USER] | Switches to a specified user. You will be prompted for that user's password. |
su - [USER] | Switches to a specified user and loads their environment. |
su -c "command" | Executes a single command as the specified user without starting a new interactive shell. For example: su -c "ls -l /root". |
exit | Exits the `su` session and returns to the previous user's shell. |
su vs sudo
While both commands are used for privilege escalation, their mechanisms and use cases differ:
su: Requires the **password of the target user** (e.g., root). It is used to get a new shell session as that user.sudo: Requires the **password of the current user**. It is used to run a single command with elevated privileges and is generally considered more secure as it does not require sharing the root password.