su Command in Linux | Switch User

Being a Multiuser OS, Linux allows us to make more than one user. With this feature, every user can own a separate account where all his data is kept securely. To navigate between the users in Linux, the “su” command is used. This command is used by the administrator or the other users to make switching between different users. This blog will demonstrate the usage of the su command in Linux with the help of some examples, and also the sections served in the post are as follows:

Let’s start!

What is the su Command Utility in Linux?

The su command in Linux switches users using the command-line interface. The “su” is the abbreviation for the “Switch User”. The general syntax of using the “su” command is:

$ sudo su [options] [-] [user name]

Replace the user name with the user to whose account you want to log in. The su command will navigate to the root user account if no user name is specified, and the options supported by the “su” command are listed below:

OptionsDescription
-gIt will specify the primary group
-GIt will specify the supplemental group
-PIt will create the new sudoers terminal
-sIt will run the shell

Other options which can be used with the su command can be explored using the command:

$ su --help

Let’s practice some of the major usages of the su command in Linux.

What is the Usage of the su Command in Linux?

The su command has a wide range of usages while switching a user. This section provides a list of examples representing the usage of “su” command in various scenarios. Let’s get into these:

Example 1: Switching to a Normal User

Following the above general syntax, the “su” command can be used to switch to another user in the system. The command provided below switches from the current user to “john”:

$ sudo su - john

We have switched from the “itslinux” to the “john” user using the “su” command.

Example 2: Switching to a Root User

If we do not specify any user name with the “su” command, it will log in to the root user account:

$ sudo su -

The output shows that we are navigating to the root user.

Example 3: Run any Command in the Other User Account

We can run the command in any other user account while staying in the user account we are currently logged in. For example, we are in the “itslinux” account and staying in this account, we will update the repository of the “john” user:

$ sudo su -c ‘sudo -S apt update’ john

In the above command, “-S” is used to input the password for the “john” user.

Example 4: Logging to a Different Shell

There are different shells available in Linux, as we have already installed the zsh shell. To switch to the root user of the zsh shell, use the su command:

$ sudo su -s /usr/bin/zsh

We are in the root user account of the zsh shell. To display the shell name, use the command:

$ echo $SHELL

Hence we are in the zsh shell.

Bonus Tip: How to Display the Version of the “su” Command?

To display the installed version of the su command in Linux, use the –version option of the su command:

$ su --version

That’s all from this guide!

Conclusion

The su command in Linux is used to switch from one user to the other using the terminal. Apart from that, the “su” command enables you to switch to a root user, log in from another shell or run commands with another user. This post has briefly explained all the purposes and the usage of the su command in Linux.