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.
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,
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.
From rreddy, let’s switch to another user account called user1.
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 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.
Changing the default user login shell:
su --shell shell-path user-account-details
In Shell scripting programming….
Sample command output,
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.