Oct 13, 2023 4 min read

How to use Su Command in Linux (Switch User)

In this tutorial, we'll go through how to utilize the su command which is the simplest way to switch to the administrative account.

Use Su Command in Linux (Switch User)
Table of Contents

Introduction

su (short for substitute or switch user) is a command-line application that allows you to run commands with the capabilities of another user, by default the root user.

In the current login session, su is the simplest way to switch to the administrative account. This is especially useful when the root user isn't allowed to utilize ssh or the GUI display manager to log in to the system.

In this tutorial, we'll go through how to utilize the su command. We will also address a few FAQs on how to use su Command in Linux (Switch User).

How to Use the su Command

The su command has the following general syntax:

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

su's default behavior when executed without any options is to perform an interactive shell as root:

su

You'll be requested for the root password, and if you input it, the user running the command will be elevated to root for the time being.

The substitute user's /etc/passwd entry is used to set the session shell (SHELL) and home (HOME) environment variables, but the current directory is not modified.

Use the whoami command to verify that the user has been changed:

whoami

The command will print the name of the user running the current shell session:

Output

root

When invoking su, the most commonly used option is - (-l, --login). This changes the current directory and turns the shell into a login shell with a very similar environment to a real login:

su -

Use the -s, --shell option to run a different shell than the one defined in the passwd file. To switch to root and launch the zsh shell, for example, type:

su -s /usr/bin/zsh

Invoke the command with the -p, --retain-environment option to preserve the caller user's entire environment (HOME, SHELL, USER, and LOGNAME).

su -p

The -p option is ignored when the - option is used.

Use the -c, --command option to run a command as the substitute user without having to launch an interactive shell. To use the ps command as root, for example, type:

su -c ps

Pass the username as an argument to su to switch to another user account. To switch to the user tyrion, for instance, type:

su tyrion

Sudo vs. Su

The root user account is blocked by default on some Linux distributions, such as Ubuntu, for security reasons. This indicates that root has no password and that you cannot switch to root using su.

To change to root, prepend the su command with sudo and input the password for the currently logged in user:

sudo su -

You can use the sudo command to run applications as another user, by default the root user.

The su command is run as root if the user has sudo access permission. Running sudo su - followed by the user password has the same effect as running su - followed by the root password.

sudo runs an interactive login shell with the root user's environment when used with the -i option:

sudo -i

Running su - is essentially the same as sudo -i.

The benefit of sudo over su is that the root password does not have to be shared between different administrative user accounts.

You can also use sudo to allow users to run only particular apps as root.

FAQs to su Command in Linux (Switch User)

How do I use the su command to switch to another user?

To switch to another user, simply type su followed by the username of the user you want to switch to. For example, su john will switch to the user account named "john".

What is the difference between su and su -?

The su - command, also known as "login mode", simulates a full login session for the target user, including executing the login scripts and setting up the user's environment. In contrast, plain su switches to the user without executing the login scripts or setting the environment.

Can I switch to the root user using the su command?

Yes, by default, without specifying a specific username, su will switch to the root user. However, this requires the root password to be entered unless you are already the root user or have sudo privileges.

How do I run a command as another user using the su command?

To execute a command as another user, use the -c option followed by the command you want to run. For instance, su john -c "ls -l" will run the ls -l command as the user "john".

Can I become the superuser (root) temporarily without knowing or entering the root password?

Yes, if your user account is permitted to run commands with root privileges via sudo, you can run sudo su, enter your own password, and become the root user temporarily. However, using sudo is generally recommended instead of switching to the root user directly.

What if I forget to specify the target user with the su command?

If you don't specify a user when using the su command, it will assume you intend to switch to the root user by default. However, this is subject to system configuration, and some systems may require specifying a valid username explicitly.

How can I return to my original user after switching using the su command?

To return to your original user account, you can use the exit command or simply close the terminal session you opened after using su. This will end the session of the user you switched to and bring you back to your original user.

Conclusion

su is a command-line utility that allows you to temporarily assume the identity of another user and run commands as that user.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.