In Linux, the
su command (switch user) is used to run a command as a different user.
In this guide, you will learn how to use the
su command, with practical examples.
- A system running Linux
- Access to a terminal window/command-line (Ctrl-Alt-T, Ctrl-Alt-F2)
How the su Command Works
su command is used to run a function as a different user. It is the easiest way to switch or change to the administrative account in the current logged in session.
Some versions of Linux, like Ubuntu, disable the root user account by default making the system more secure. But, this also restricts the user from running specific commands.
su to temporarily act as a root user allows you to bypass this restriction and perform different tasks with different users.
Note: A root account is a master administrator account with full access and permissions in the system. Because of the severity of changes this account can make, and because of the risk of it being compromised, most Linux versions use limited user accounts for normal use.
su Command Syntax
To use the
<strong>su</strong> command, enter it into a command-line as follows:
su [options] [username [arguments]]
If a username is specified,
su defaults to the superuser (root). Simply find the user you need and add it to the
<strong>su</strong> command syntax.
su Command Options
To display a list of commands, enter the following:
Here are some common options to use with the
Username– Replace username with the actual username you want to log in with. This can be any user, not just root.
–command [command]– Runs a specific command as the specified user.
–login [username]– Runs a login script to change to a specific username. You’ll need to enter a password for that user.
–shell [shell]– Allows you to specify a different shell environment to run in.
–help– Show the help file for the
––preserve–environment– Preserve the shell environment (HOME, SHELL, USER, LOGNAME).
su Command Examples
Switch to a Different User
To switch the logged-in user in this terminal window, enter the following:
su –l [other_user]
You’ll be asked for a password. Enter it, and the login will change to that user.
If you omit a username, it will default to the root account. Now, the logged-in user can run all system commands. This will also change the home directory and path to executable files.
whoami command to verify you switched to a different user.
Note: If you are having issues with authentication, you can change the root or sudo password in a couple of simple steps.
Run Specific Command as a Different User
To run a specific command as a different user, use the
su –c [command] [other_user]
The system will respond by asking you for the user password.
When you enter this example, the system will use the specified account to run the
ls (list directory contents) command.
Use a Different Shell
To use a different shell, or operating environment, enter the following:
su –s /usr/bin/zsh
This command opens a root user account in Z shell.
Use a Different User in the Same Environment
You can keep the environment of the current user account with the
su –p [other_user]
Replace [other_user] with the actual username you want to switch to.
The user account will switch, but you’ll keep the same home directory. This is useful if you need to run a command as a different user, but you need access to the current user’s data.
To verify you remained in the same home environment, use the
<strong>echo $HOME</strong> command that will display the directory you are working in.
Command Comparison: su vs sudo
sudo command grants a one-time or limited-time access to root functionality. Typically, the
sudo command is used to quickly run an administrative command, then return to the user account’s regular permissions.
To provide sudo access, the user has to be added to the sudo group.
Note: By default, some versions of Linux (such as Ubuntu) disable the root account. That means there’s no password assigned to the root user. However you can switch to root by running the following command and entering the currently logged-in user’s password:
sudo su -
su command lets you switch the current user to any other user. If you need to run a command as a different (non-root) user, use the
–l [username] option to specify the user account. Additionally,
su can also be used to change to a different shell interpreter on the fly.
su is an older but more fully-featured command. It can duplicate the functionality of
sudo by use of the
–c option to pass a single command to the shell.
You now know how to use the
su command to temporarily change users and execute commands in Linux. Use the examples provided to get started.
Next, consider checking out our Linux Command Cheat Sheet Tutorial With Examples.