What Does su do in Linux, and How to Use it?

The “su” command refers to the “Switch user or Substitute user”, which allows the user to execute the command with another user’s privileges. By default, this command temporarily acts as a root user. Moreover, using this command act, the user can easily switch the administrator/root user account in the current logged-in environment.

Considering its importance, this post enlists the main objective, working, and usage of the “su” command in Linux. The guidelines of this post are noted here:

How Does “su” Command Work in Linux?

Simply the “su” command invokes another system user environment/shell in the current working directory of the logged-in user.

Suppose the currently logged-in user is “itslinuxfoss”, as shown on the screenshot:

Execute the “su” command followed by another user of the current working system:

$ su johnson

The user “itslinuxfoss” has been switched to the “johnson” but the environment(HOME, SHELL) of user “itslinuxfoss” remain same.

Apart from that, when the “su” command executes with the “-(hyphen)” symbol, then it enables the specified user shell as the logged-in user:

$ su - johnson

The output shows that the switched user “johnson” is now in its own “home” directory.

How to Use the “su” Command In Linux?

The usage of the “su” command depends on its basic syntax, which is written below:

$ su Options User [Argument..]

The syntax contains the following parameters:

  • su: Main Keyword represents the “su” command.
  • Options: Represent the supported option of the “su” command.
  • User: Denotes the targeted user.
  • Argument: shows the different arguments or flags that the user can use with the targeted users to perform tasks.

For supported options of the “su” command, read out its help page that can be easily accessed through the following “help” command:

$ su --help

Now move on to the various examples to understand the practical implementation/usage of the su command via its supported options.

Example 1: Access the Login User

The “-l or –login” flag of the su command invokes the “login” user environment. It displays the same output as the “su –” command. Still, the difference is that it will also inherit the environment variables, current working directory, and other settings of the target user.

Suppose the current user is “johnson” as shown below:

To switch the login user executes the “su” command followed bythe “-l” option and enters the “itslinuxfoss” password for authentication:

$ su -l itslinuxfoss

The user “johnson” has been switched to the logged-in user “itslinuxfoss” shell.

Example 2: Invoke the Target User in the Same Environment

The “-c(command)” flag of the “su” command allows the specific user to operate the task in the current user environment.

In this example, “itslinuxfoss” is the current user of the device “Ubuntu”:

Now execute the “pwd” command as the “anna” user in the environment shell of “itslinuxfoss”:

Example 3: Access the Specified User Environment

The “-p or –preserve” flagkeeps the current user environment i.e., “HOME”, “USER”, “SHELL”, and “LOGNAME” while working in the specified calling user.

In this example, we preserved theuser “itslinuxfoss” environment for the switched user “anna”  as shown in the below image:

$ su -p anna

The output of the “echo” command shows that the root user “anna” is working in the “itslinuxfoss” user “home” directory.

Conclusion

In Linux, the “su(Switch User)” built-in command is utilized to switch the currently logged-in user to the specified user of the system. This command line tool provides a convenient way to get root user privileges. In addition, it provides some essential options to change the output based on the specified option. This post has illustrated the objective, working, and usage of the “su” command in Linux.