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”.

Syntax:

su user-account-name

 

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

Note: 

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
/home/rreddy

 

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.

 

Example-1:

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
Password:

 

[user1@abclearn rreddy]$ whoami
user1

 

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

Checking with pwd command,

[user1@abclearn rreddy]$pwd
/home/rreddy

 

Note: 

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

 

Example-2:

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.

Syntax:

su – user-account-name

 

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

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

 

[user1@abclearn user1]$whoami
user1

 

[user1@abclearn rreddy]$pwd
/home/user1

 

Note: 

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
/bin/bash

 

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
user1:x:1001:1001::/home/user1:/bin/bash

 

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
exit

 

Note: 

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.

 

Example-3:

Changing the default user login shell:

Syntax:

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
Password:
sh-4.2$ echo $SHELL
/bin/sh

 

Example-4:

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. 


Syntax:

Su – root
su –

 

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

 

Note: 

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

 

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

 

 

Topics Summary