Checking your Current User Logging in Linux

Posted on

Check Your current User Identifier Information

Linux as a multi-user operating system permit several users login to the operating system at the same time. Other cool thing is it also permit someone to switch from a user to another. But how do we know or do we check currently user whom we used right now ?. Basically it can be read from the bash prompt as soon as we login to the operating system via terminal or CLI (Command Line Interface). Below is the standard format of bash prompt which is displayed normally in terminal or CLI (Command Line Interface) :

For normal user, below is how the bash prompt will be displayed as the following pattern :

login_name@hostname current_working_directory

For an example :

username@hostname: ~ $
username is who are we currently login into the system
hostname is the hostname of the system we are currently login
~ is a sign for the default working directory when someone successfully login into the system. The default working directory mainly will be /home/username.
$ is a sign for identifying that we are now in a normal user mode for standard command-line prompt using Bash shell and Bourne or Korn shell among the other shell type. The other sign which is for root user is a hash mark (#).

But sometime the bash prompt can be in the following pattern :

username@hostname :/opt$

The other part which is shown by /opt is describing the position of the active current working directory.

So, besides looking at the shell prompt, how do we know we are currently logging in?, well, we can actually execute the following command :

whoami

Below is the image example of executing the command :

Switching between user using the ‘su’ command. The image shows the switch process from ‘root’ to ‘james’ in Linux CentOS 7.

As we can see, I log in as root in the above image. The hash tag mark (#) currently ends the shell prompt for root. And for the command ‘whoami’, it is explicitly display the username whom currently log in which is root.

But as you may knew, we can actually switch to other username. I run the following command :

su - james
su  is a command which stands for either switch user, super user or substitute user depends on what is the purpose of executing the command.
-  sign, a dash or a hyphen sign  is a parameter to use when we want to switch user by also using the environment variable or the user’s environment which is we are switching into.
james simply the name of the user.

When I execute ‘whoami’ again, it display ‘james’ as another user although I am logging in as root.

Below is an image of different scenario which shown I am logging in as ‘james’ instead of root and then executing the ‘whoami’ command. The image is showing that normal user can also switch to root for specific purpose :

Displaying the switching process from user ‘james’ whom logged in first to another user which is ‘root’ in this context. The image is capturing those process in Linux CentOS 7 Distribution.

Below is the manual page of ‘whoami’ which can be found by typing ‘man whoami’ :

Displaying the manual of command ‘whoami’. It is executed in Linux CentOS 7 Distribution which is displayed by typing the ‘man whoami’ command.

You can also type the command ‘man whoami’ to view the full page of the manual.

Try the command by yourself on your Linux machine to test whether the command is actually working or not.

2 thoughts on “Checking your Current User Logging in Linux

Leave a Reply