🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

su Command

Beginner User Management man(1)

Switch user or become superuser

👁 9 views 📅 Updated: Mar 15, 2026
SYNTAX
su [OPTION]... [USER]

What Does su Do?

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.

Options & Flags

OptionDescriptionExample
- Full login shell (load user environment) su - root
-c Execute a single command su -c 'systemctl restart nginx' root
-s Use specific shell su -s /bin/bash www-data
-l Same as - (login shell) su -l postgres
-p Preserve current environment su -p root

Practical Examples

#1 Switch to root

Switches to root with full login shell. Requires root password.
$ su -
Output: Password:

#2 Switch to user

Switches to the www-data user with their environment loaded.
$ su - www-data

#3 Run single command

Runs one command as root without opening an interactive shell.
$ su -c 'cat /etc/shadow' root

#4 Switch with specific shell

Opens bash as nginx user (useful when user's default shell is /sbin/nologin).
$ su -s /bin/bash nginx

#5 Return to original user

Returns to the previous user after su.
$ exit

Tips & Best Practices

Always use su - (with dash): su without dash keeps your current environment, which can cause confusion. su - loads the target user environment properly.
sudo vs su: sudo uses YOUR password and logs commands. su requires the TARGET user password. sudo is preferred for admin tasks.
Service user shells: Service accounts often have /sbin/nologin as shell. Use su -s /bin/bash username to get a shell.

Frequently Asked Questions

What is the difference between su and sudo?
su switches to another user (needs their password). sudo runs a command as another user (needs your password and sudoers permission). sudo is preferred.
Why use su - instead of su?
su - loads the target user's full environment (PATH, HOME, shell config). su without dash keeps your environment, which can cause issues.
How do I switch to a service account?
Use su -s /bin/bash service_user. Service accounts typically have /sbin/nologin, so -s specifies a usable shell.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →