su Linux Command
Switches to another user account or the root user.
What does su do?
Switches to another user account or the root user.
Syntax
su [options] [username]Practical example
su - rootQuestions about su
What is the difference between su and sudo?
`su` switches to another user entirely and requires that user's password. `sudo` runs a single command as root using your own password.
How do I switch to root with su?
Use `su -` or `su - root` to switch to the root user. You will be prompted for the root password. The `-` sets up root's environment.
What does the dash (-) mean in su -?
The dash `-` starts a login shell, loading the target user's environment variables and profile. Without it, you keep your current environment.
How do I exit from su?
Use `exit` or press Ctrl+D to leave the su session and return to your original user account.
How do I switch to another user?
Use `su - username` and enter that user's password. This gives you a shell with that user's permissions and environment.
How do I run a single command as another user?
Use `su -c 'command' username` to run a single command as another user without switching to an interactive shell.
Why is su better than logging in as root?
Using `su` or `sudo` is safer than direct root login because it provides an audit trail and reduces the risk of accidental system damage.
Related User Management
idPrints user and group IDs (UID, GID) for the specified or current user.
sudoExecutes a command with superuser (root) privileges.
wDisplays who is logged in and what they are currently doing.
passwdChanges the password for a user account.
whoShows all users currently logged into the system with login details.
useraddCreates a new user account on the system.