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 is abbreviated as “switch user”.
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.
Looking at the command terminal in XXX shell,
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.
we can see that from rreddy, now I have logged in as user1.
Checking with pwd command,
In the above output, only user account got switched but not logged into new user’s home directory.
Switch user with 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.
From rreddy, let’s switch to another user account called user1.
landing on to 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.
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.
To come back to 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.
o 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.
o If we have switched users a multiple number 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
In Shell scripting programing….
Sample command output,
Switching to root user account:
Just like switching to a normal user, we can specify “root” user name 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.
A root user can switch to any other account without even knowing the password of that user.