Su Command in Linux (Switch User)

By 

Updated on

5 min read

Linux su Command

The su (short for substitute or switch user) utility allows you to run commands with another user’s privileges, by default the root user. If you’re looking for running single commands with elevated privileges, see our sudo command guide .

Using su is the simplest way to switch to the administrative account in the current login session. This is especially handy when the root user is not allowed to log in to the system through SSH or using the GUI display manager.

In this guide, we will show you how to use the su command with practical examples.

Syntax

The general syntax for the su command is as follows:

Terminal
su [OPTIONS] [USER [ARGUMENT...]]

When invoked without any option, the default behavior of su is to run an interactive shell as root:

Terminal
su

You will be prompted to enter the root password, and if authenticated, the user running the command temporarily becomes root.

Info
Unlike sudo, which asks for your own password, su requires the password of the target user you are switching to.

To confirm that the user is changed, use the whoami command:

Terminal
whoami
output
root

To switch to another user account, pass the user name as an argument to su. For example, to switch to the user tyrion you would type:

Terminal
su tyrion

To start a login shell as another user:

Terminal
su - tyrion

Login Shell vs Non-Login Shell

When you run su without the - option, the SHELL and HOME environment variables are set from the target user’s /etc/passwd entry, but the current directory and the rest of the environment are not changed. This means the PATH variable still contains the original user’s directories.

The most commonly used option when invoking su is - (-l, --login). This starts a login shell with an environment identical to a real login, including changing the current directory to the target user’s home:

Terminal
su -

In most cases, you want to use su - rather than plain su to get a clean environment.

Options

The su command accepts the following options:

Run a Specific Shell (-s)

To run a shell other than the one defined in the passwd file, use the -s, --shell option. For example, to switch to root and run the zsh shell:

Terminal
su -s /usr/bin/zsh

Preserve the Environment (-p)

To preserve the entire environment (HOME, SHELL, USER, and LOGNAME) of the calling user, use the -p, --preserve-environment option:

Terminal
su -p

When the - option is used, -p is ignored.

Run a Single Command (-c)

To run a command as the target user without starting an interactive shell, use the -c, --command option. For example, to invoke the ps command as root:

Terminal
su -c 'ps aux'

To run a command as a specific user:

Terminal
su -c 'whoami' tyrion

The command string should be quoted if it contains spaces or special characters.

Sudo vs Su

On some Linux distributions like Ubuntu, the root user account is disabled by default for security reasons. This means that no password is set for root, and you cannot use su to switch to root.

One option to change to root would be to prepend the su command with sudo and enter the currently logged-in user password:

Terminal
sudo su -

The sudo command allows you to run programs as another user, by default the root user.

If the user is granted sudo access, the su command is invoked as root. Running sudo su - and then typing the user password has the same effect as running su - and typing the root password.

When used with the -i option, sudo runs an interactive login shell with the root user’s environment:

Terminal
sudo -i

sudo -i is functionally the same as running su -.

The key differences between sudo and su:

  • Password: su requires the target user’s password. sudo requires your own password.
  • Access control: sudo allows fine-grained control over which commands a user can run (configured in /etc/sudoers). su gives full access to the target user’s account.
  • Auditing: sudo logs every command that is run. su only logs that a user switched accounts.
  • Root password: sudo eliminates the need to share the root password among multiple administrators.

Quick Reference

TaskCommand
Switch to rootsu
Switch to root (login shell)su -
Switch to another usersu username
Run a command as rootsu -c 'command'
Use a specific shellsu -s /bin/zsh
Preserve environmentsu -p
Switch to root via sudosudo su -
Sudo login shellsudo -i

FAQ

What is the difference between su and su -?
su switches to the target user but keeps most of the current environment, including PATH and the working directory. su - starts a full login shell with the target user’s complete environment.

What is the difference between su and sudo?
su switches your entire session to another user and requires that user’s password. sudo runs a single command (or opens a shell) with elevated privileges using your own password.

Why does su ask for a password even though I am root?
If you are already root, su does not ask for a password. If it does, you may be in a non-root shell that looks like root (e.g., changed prompt). Use whoami to verify.

How do I restrict which users can use su?
On most distributions, you can restrict su access to members of the wheel group by configuring PAM. Edit /etc/pam.d/su and uncomment the line containing pam_wheel.so.

Conclusion

The su command allows you to switch to another user account and run commands with that user’s privileges. In most cases, sudo is the preferred method for running commands as root because it provides better access control and auditing.

If you have any questions, feel free to leave a comment below.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page