
The su (switch user or substitute 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
Password:
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
root
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
/home/john.doe
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 (- or -l or --login)
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
US/Central
If john.doe switches to root without using the - (single dash), -l, or --login, the TZ variable will remain set to US/Central.
[john.doe@server1 ~] su root
Password:
[root@server1 ~] echo $TZ
US/Central
On the other hand, if the - (single dash), -l, or --login is used, the environment variables will be reset. In this example, the TZ variable no longer contains US/Central.
[john.doe@server1 ~] su - root
Password:
[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
Password:
-sh-4.2$
echo $SHELL can be used to verify the shell has been switched.
-sh-4.2$ echo $SHELL
/bin/sh
Did you find this article helpful?
If so, consider buying me a coffee over at