su command

su command practical examples

As we work through Linux administration activities, we most of the times have to switch between one user credentials to another user credentials.

In this section, we will look at those requirement and command operations.


su command:

su is abbreviated as “switch user”.


su user-account-name


Throughout this section, we will understand about this command implementation with different example scenarios.


sudo & su both commands are different in nature.


To understand more about sudo command, How can use sudo command for file executions?


Before we get started with examples, let us understand specific lookout areas of user identification.

Once we login to terminals, let’s check with what credentials I have logged into and what is my default directory.

[rreddy@abclearn rreddy]$pwd


Looking at the command terminal in XXX shell,

  • We can see userid specified before the server name.

Also “$” dollar symbol indicates that we have logged in as a non-root user.



Switch from one user to another user:

Using su command, we can switch from one user to another user account.

From rreddy, let’s switch to another user account called user1. Type the password.

[rreddy@abclearn~]$su user1


[user1@abclearn rreddy]$ whoami


we can see that from rreddy, now I have logged in as user1.

Checking with pwd command,

[user1@abclearn rreddy]$pwd



In the above output, only user account got switched but not logged into new user’s home directory.



Switch user to its login user’s home directory:

With the same su command, if we specify “-“ then we will be in new user home directory by default.


su – user-account-name


From rreddy, let’s switch to another user account called user1.

[rreddy@abclearn~]$su - user1
Last login: Fri July 15 20:38:37 EST 2016 on pts/1


[user1@abclearn user1]$whoami


[user1@abclearn rreddy]$pwd



landing onto user’s home directory may have advantages like we can directly start accessing the files and directories under his home directory. This will avoid another “cd” command typing.


Also, assigns the default shell for this user.

[user1@abclearn ~]$ echo $SHELL


How to check landing user’s home directory and default shell information?

From /etc/passwd configuration file, we can understand each user default home directory and default shell.

[rreddy@abclearn~]$ grep user1 /etc/passwd


To come back to the previous user account:

We can type in exit command to come out of user session. It will take you to the previous logged in user session,

Or else, we can again use su command to switch to the corresponding new user session.

[user1@abclearn rreddy]$ exit



For a root user, the password is not required while switching to another user. By default, he/she can switch to any user they want.

If we have switched users multiple numbers of times, then for each exit type control goes back to the previous user account.



Changing the default user login shell:


su --shell shell-path user-account-details


Practical usage:

In Shell scripting programming….

Another example….

Sample command output,

[rreddy@abclearn~]$su --shell /bin/sh user1
sh-4.2$ echo $SHELL



Switching to root user account:

Just like switching to a normal user, we can specify “root” username for switching to it.

But, there is a special case also,
even if we don’t specify any user account details after su command, then by default, it will expect root account credentials. 


Su – root
su –


[rreddy@abclearn~]$su -
Last login: Fri Jan 15 02:31:24 EST 2016 on pts/3



A root user can switch to any other account without even knowing the password of that user.


[root@abclearn~]#su user1
[user1@abclearn ~]$whoami



Topics Summary