Open In App

Switch Users on Linux with the su Command

Last Updated : 08 Nov, 2025
Comments
Improve
Suggest changes
1 Likes
Like
Report

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.

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 .bashrc and .profile, sets their PATH, and changes the directory to their home directory.
  • Why use it? You are root (or user) with root's (or user's) environment and root's PATH. 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 - user

This 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 su to root from your home directory (/home/user), you will be the root user but still be in /home/user and still be using your own user's PATH. This can cause you to run the wrong commands or create files as root in 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

CommandWhat It Does
su -Switches to root with a full login shell. (Recommended)
suSwitches 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 --loginThe full flag for the - dash. Creates a login shell.
-s /bin/shSpecifies 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

2024-01-12_13-47

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 username

Replace '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

2024-01-12_13-48

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 username

Replace '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

2024-01-12_13-50

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 -

2024-01-12_13-53

How to Simulate a Login Shell

To simulate a full login shell for another user, use the '-l' or '--login' option:

su -l username

This ensures that the target user's environment is fully loaded, just like during a regular login.

2024-01-12_13-53_1

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


Explore