Switch users using the SU command in Linux

Home > Search > Linux commands

The su (switch user) command can be used to swich between different user account. The su command without any options will prompt you to enter a password. Entering root's password will switch you to root's account. If you are signed in as root, the su command will not examples will have no effect, as you are already signed in as root.

[john.doe@server1 ~]# su


One thing that may be a little strange is that when you are switched, you may see both root and john.doe. The whoami command verifies that you are in root account, and not in john.doe account.

[root@server1 john.doe] whoami


In this example, the reason that john.doe is displayed is because /home/john.doe is the present working directory.

[root@server1 john.doe] pwd


The su command followed by a username can be used to switch to a certain users account. In this example, root switches to john.doe. There will be no prompt to enter john.doe password, since root is the super user.

[root@server1 ~] su john.doe
[john.doe@server1 ~]


Reset environment variables

The - (single dash), -l, or --login options can be used to reset the environment variables. As an example, let's say john.doe sets the TZ (time zone) environment variable to US/Central.

[john.doe@server1 ~] export TZ=US/Central


When john.doe echoes $TZ, US/Central will be displayed.

[john.doe@server1 ~] echo $TZ


If john.doe switches to root without using the - (single dash), the TZ variable will remain set to US/Central.

[john.doe@server1 ~] su root
[root@server1 ~] echo $TZ


On the other hand, if the - (single dash) is used, the environment variables will be reset. In this example, the TZ variable no longer contains US/Central.

[john.doe@server1 ~] su - root
[root@server1 ~] echo $TZ


Select a different shell

The -s or --shell option can be used to switch to a different shell. For example, let's say the default shell is /bin/bash. In this example, /bin/sh is used instead of /bin/bash.

[john.doe@server1 ~] su root -s /bin/sh


echo $SHELL can be used to verify the shell has been switched.

-sh-4.2$ echo $SHELL


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter in the box below so that we can be sure you are a human.