su Command in Linux: How to Switch Users

By 

•

Updated on

•

9 min read

Switching users with the su command in Linux

When you are logged in as a regular user and a task needs root or a different account, you have two practical options on Linux: switch into that user with su, or run a single command with elevated privileges through sudo. The su utility (short for substitute or switch user) is the older of the two and the one many sysadmins still reach for when they want a full shell as another user.

Using su is the simplest way to switch to the administrative account in the current login session. It is especially handy when the root user is not allowed to log in to the system through SSH or using the GUI display manager. If you only want to run a single command with elevated privileges, see the sudo command guide instead.

This guide explains how to use the su command with practical examples and how it compares to sudo.

Syntax

The general syntax for the su command is as follows:

txt
su [OPTIONS] [USER [ARGUMENT...]]

To open an interactive shell as root, run su with no arguments.

Terminal
su

From a regular account, su typically prompts for root’s password. Ubuntu locks the root password by default. If you have sudo access on Ubuntu, you can open a root login shell with sudo .

Info
When su prompts for a password, enter the target user’s password. sudo typically asks for your own password when authentication is required.

Once the new shell opens, run whoami .

Terminal
whoami
output
root

The output confirms that the shell is running as 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

Plain su keeps your current directory and sets the HOME and SHELL environment variables from the target user’s account record. For local accounts, that record is in /etc/passwd . su also sets USER and LOGNAME when switching to a non-root account. PAM, the system’s authentication framework, can then modify the environment.

On Debian 13 with the default configuration, plain su typically inherits the calling user’s PATH. On Ubuntu 26.04, the default PAM configuration can replace it with the value in /etc/environment, which typically includes /usr/sbin.

Use - (-l, --login) to start a login shell . This initializes login variables such as HOME and PATH and changes the current directory to the target user’s home. On systemd-based systems, the shell runs within the existing login session. The following command opens a root login shell.

Terminal
su -

Options

The su command accepts the following options:

Run a Specific Shell

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

If the target user’s account has a restricted shell, meaning one that is not listed in /etc/shells, then su ignores both the -s option and the SHELL variable unless you are calling su as root.

Preserve the Environment

Use -p or --preserve-environment to have su leave the calling user’s HOME, SHELL, USER, and LOGNAME in place.

Terminal
su -p

PAM can still modify the environment. The --login option takes precedence over -p.

Run a Single Command

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.

Using sudo su to Become Root

Ubuntu locks root’s password by default, so a regular user cannot switch to root by entering a password at the su prompt.

If your sudo policy permits it, use sudo su - to open a root login shell.

Terminal
sudo su -

Under the default policy, sudo asks for your own password if authentication is required. It then invokes su as root, and the default PAM configuration lets root skip the second prompt.

Dropping the - gives you a root shell that leaves you in the current working directory:

Terminal
sudo su

You can also switch to a regular account this way, which is useful when you do not know that user’s password:

Terminal
sudo su - tyrion

The sudo command can open a root shell on its own, without involving su at all. The -i option runs an interactive login shell with the root user’s environment:

Terminal
sudo -i

The -s option starts a root shell without running a login shell:

Terminal
sudo -s

All four commands leave you at a root prompt, but they differ in shell selection, startup files, and working directory:

CommandShellReads root’s login filesWorking directory
sudo suRoot’s configured shell, non-loginNoUnchanged
sudo -sCalling user’s shell, non-loginNoUnchanged
sudo su -Root’s configured shell, loginYesRoot’s home, usually /root
sudo -iRoot’s configured shell, loginYesRoot’s home, usually /root

The sudo -s command uses the SHELL variable or the calling user’s configured shell, while the other forms use root’s configured shell. The login forms read root’s login files and move to root’s home directory, which is usually /root.

When you need a root login shell, prefer sudo -i because it invokes root’s login shell directly instead of starting su as a second program. For routine administrative work, prefix individual commands with sudo so that sudo policy and command logging apply to each invocation.

sudo vs su

The key differences between sudo and su:

  • For password authentication, su typically asks for the target user’s password. sudo typically asks for yours.
  • 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 commands invoked through sudo. su only logs that a user switched accounts, and commands run inside the new shell are not logged individually by su.
  • Root password: sudo removes the need to share the root password among multiple administrators.

Troubleshooting

su: Authentication failure
Check the target user’s password and whether local PAM policy allows the switch. If root’s password is locked, as it is by default on Ubuntu, use sudo -i or sudo su - if your sudo policy allows it. If you need password-based access to root, sudo passwd root sets a root password.

command not found for root commands after running su
If PATH lacks /usr/sbin, the shell may report command not found for commands installed there, such as useradd, fdisk, and shutdown. Use su - to load root’s login environment, or call the command by its full path, such as /usr/sbin/useradd. Setting ALWAYS_SET_PATH yes in /etc/login.defs or /etc/default/su makes plain su initialize PATH before PAM applies its settings.

su: user <name> does not exist or the user entry does not contain all the required fields
su could not find a usable account record. Double-check the spelling and run getent passwd <name> to look up the user in the system’s configured account sources, which can include local files and LDAP. If the record exists, check that required fields such as the home directory are filled in.

This account is currently not available.
The target is a system account such as www-data, and its login shell is /usr/sbin/nologin. To get a shell anyway, run su as root and pass a real shell with -s, for example sudo su -s /bin/bash www-data. To run a single command, sudo -u www-data command is simpler.

su -c runs the wrong command or splits arguments unexpectedly
The command string is being parsed by your current shell before su sees it. Wrap the whole command in single quotes, for example su -c 'systemctl restart nginx', so the target user’s shell receives it as a single argument.

Cannot exit back to the original user
The exit command (or pressing Ctrl+D) leaves the current shell. If su was nested several times, you may need to run exit more than once before you are back in your original session.

Quick Reference

For a printable quick reference, see the su cheatsheet .

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
Keep the caller’s environment before PAMsu -p
Switch to root via sudosudo su -
Switch to another user via sudosudo su - username
Sudo login shellsudo -i

FAQ

What is the difference between su and su -?
Plain su starts a shell as the target user in your current directory and can inherit your PATH, depending on the system configuration. su - starts a login shell in the target user’s home directory.

What is the difference between su and sudo?
su usually opens a shell as the target user. sudo runs a command or opens a shell as another user if the sudo policy permits it. When prompted, you typically enter the target user’s password for su and your own for sudo.

What password does su ask for?
If su prompts you for a password, enter the target account’s password. The target defaults to root. A caller who is already root can skip the password prompt when the PAM configuration grants access through pam_rootok.

How do I exit an su session?
Type exit or press Ctrl+D to leave the current shell and return to the previous user. If you ran su more than once in a row, repeat the step until you are back in your original session.

How do I restrict which users can use su?
Configure su restrictions through PAM, usually in /etc/pam.d/su. To allow only members of a specific group, use pam_wheel.so with an explicit group, for example auth required pam_wheel.so group=wheel. Confirm that the group exists and that at least one administrator belongs to it before enabling the rule, because an incorrect PAM configuration can block su access. Distribution defaults differ, so review the comments in the local PAM file rather than uncommenting a generic line.

What is the difference between sudo su and su -?
sudo su opens a non-login root shell in your current directory if your sudo policy allows it. su - opens a root login shell in root’s home directory, usually /root. From a regular account, su - typically asks for root’s password, which is locked by default on Ubuntu.

What is the long version of the su option that runs a command as a different user?
It is --command, the long form of -c. For example, su --command 'whoami' tyrion runs a single command as tyrion instead of opening an interactive shell.

Conclusion

The su command lets you switch to another user account and run commands with that user’s privileges, which is the right tool when you need a full interactive shell as another user. For everyday administrative work, sudo is usually the better choice thanks to its access control and auditing, so consider pairing this guide with the sudo command in Linux post.

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 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page