Switch Users on Linux with the su Command
The su command (which stands for "Substitute User") is a fundamental Linux tool that allows you to temporarily switch your current login session to a different user account.
This is a core task for system administration. You will use su to:
- Become the root user: Gain administrative privileges to perform system-wide tasks.
- Test permissions: Switch to a regular user account (like a service account, e.g.,
www-data) to see what it can or cannot access. - Run commands as another user: Execute a single command with the privileges of a different user
Syntax of su Command
The basic syntax of the 'su' command is as follows:
su [OPTION] [USER]Here,
- [OPTION] represents various command-line options.
- [USER] is the username to which you want to switch.
The Most Important Concept: su - vs. su
The single most important part of using su is understanding the difference between a "login" and a "non-login" shell.
1. su - [username] (The "Login" Shell - Recommended)
This is the correct and recommended way to switch users. The dash (-) is a shortcut for --login.
- What it does: It simulates a full login. It loads the target user's environment, including their
.bashrcand.profile, sets theirPATH, and changes the directory to their home directory. - Why use it? You are
root(oruser) withroot's (oruser's) environment androot'sPATH. This is safe and predictable.
Example (Becoming Root):
su -(This will ask for the root password and place you in /root)
Example (Becoming "user"):
su - userThis will ask for user's password and place you in /home/user)
2. su [username] (The "Non-Login" Shell)
This is the "quick switch" method.
- What it does: It only switches your user ID. You keep your original environment and
PATH. You even stay in your current directory. - Why it's dangerous: If you
sutorootfrom your home directory (/home/user), you will be therootuser but still be in /home/user and still be using your own user'sPATH. This can cause you to run the wrong commands or create files asrootin your user's home, which is messy and a security risk.
In short: Always use su - unless you have a specific, advanced reason not to.
Options Available in su Command
| Command | What It Does |
su - | Switches to root with a full login shell. (Recommended) |
su | Switches to root but keeps your environment. (Not recommended) |
su - [user] | Switches to [user] with their full login shell. (Recommended) |
su [user] | Switches to [user] but keeps your environment. |
su -c "cmd" | Runs a single cmd as root (with root's environment) and exits. |
su -p [user] | Preserves your current environment and switches to [user]. |
-l or --login | The full flag for the - dash. Creates a login shell. |
-s /bin/sh | Specifies a different login shell to use. |
How to Switch to the Root User by Using su Command
To switch to the root user, simply type:
sudo su
You will be prompted to enter the root user's password. Once authenticated, you will have superuser privileges until you exit the session.
How to Switch to the Specific User by Using su Command
To switch to a different user, specify the username as an argument:
su usernameReplace 'username' with the actual username you want to switch to. You'll be prompted to enter the password for that user.
For example: If we want to switch to a user name "shivansh260" we will use the following command
su shivansh260
How to Execute a Command as Another User by Using su Command
You can use 'su' to execute a single command as another user without switching to their environment:
sudo su -c "command" -s /bin/bash usernameReplace 'command' with the desired command and 'username' with the target user. The '-s /bin/bash' option specifies the shell to be used.
For example: If we want to print "hello" on the screen , and this command has to be run by a username "shivansh" we will use the following command
sudo su -c "echo hello" -s /bin/bash shivansh
How to Preserve Environment Variables while Switching User
When switching users, you might want to preserve the environment variables from the original user. Use the '-' option to achieve this:
sudo su -
How to Simulate a Login Shell
To simulate a full login shell for another user, use the '-l' or '--login' option:
su -l usernameThis ensures that the target user's environment is fully loaded, just like during a regular login.

This command switches to the root user while preserving the environment.