The “whoami” Command in Linux [5 Practical Examples] 

The whoami command in Linux can be handy while working on a machine with multiple users.  It displays the username of the effective user in the current shell. Moreover, it is useful in bash scripting to show who is running the script.

A. Description

Unix and Unix-like operating systems are multi-user operating systems where different users have different privileges. Sometimes it becomes necessary to know who the current user is. A quick input of the whoami command on the terminal can do the trick. The command whoami in Linux is basically the concatenation of the words “Who am I?” Furthermore, the current user can check whether he has root privileges or not.

B. Syntax

The syntax of whoami is very simple. It doesn’t take any argument. So no need to stress about that. The syntax is a whoami command followed by one or multiple options.

whoami [OPTION]...
Note: In the above syntax OPTION enclosed by a square bracket and followed by 3 dots represents that multiple options can be utilized at the same time.

C. Options

There are only two options available for whoami command. You can check the man page by yourself.

man whoami

Useful Options

  • –help (display the man page and exit)
  • –version (output version information and exit)
Note: The options in Linux CLI(Command Line Interface) are all case-sensitive, So be cautious while using them.

Practical Examples of the “whoami” Command in Linux

Here I have given some practical examples of whoami command in Linux.

Example 1: Displaying the Username of the Current Effective User

It is the primary function of the command whoami. My Ubuntu has two users, walid and jim. Now I will switch between them and show you the output. If you have 2 users you can also do so by following the steps below:Showing the name of two users

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

whoami

➌ Now, press the ENTER button.

Output >

For the user walid you can see the output in the below picture.printing the current effective username using whoami command in linuxFor the user jim you can see the output in the below image.Printing effective username for another user


Similar Readings


Example 2: Using the “whoami” Command Checking for Root Privileges

Having root privileges allows a user to do pretty much anything on the system. You can follow the steps below to check whether you have root privileges or not.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo whoami

➌ Now, press the ENTER button.

❹ Enter your Password if necessary.

❺ Hit ENTER again.

Output >

For the user with root privileges, the output will look like this.Showing the output for root userOtherwise, the output will look like this.output for normal users

Example 3: Confirming Whether the Root User Running the Script or Not

You can use the whoami command in the bash script and check if the root is executing the script. You can also print a warning message. Now I will write a script to demonstrate that. Follow the steps below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

nano myscript
Note: Here “myscript” is just a file name. You can give it anything you want.

➌ Type the following lines.

#!/bin/bash

if [[ "$(whoami)" = 'root' ]]
then
echo "You are running this script as a root user. Proceed with caution."
else
echo "You aren't running this script as a root user"
fi

❹ To save press CTRL + O and ENTER, to exit press CTRL + X.

❺ Now type the following command to execute the script.

bash myscript

➏ Finally, press the ENTER button again.

Output >

It will create a file named myscript and open it in nano text editor.Bash scripting in Nano text editor using whoami commandFor root user the output will be like this:using bash script as root user

To switch to root user use the following command.

sudo su

To exit from the root user type.

exit

For normal user the output will look like this:Showing the output for normal user

Example 4: Checking “whoami” version Using the “whoami” Command in Linux

To check whoami version, I will use the option “–help”. Now I will show you how you can check your whoami by yourself. Follow the steps below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

whoami --help

➌ Press the ENTER button.

Output >

I can see my whoami version on the display.Printing the version of whoami command in linux

Example 5: Using “id” Command Instead of “whoami” Command in Linux

The id command can serve a similar purpose to whoami command in Linux but you need to provide two options. Here you will see that id command can give us the same result. Follow the steps below:

Useful Options

  • -u (Displays only the effective user ID)
  • -n ( Shows a name instead of a number)

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

id -un

➌ Press the ENTER button.

Output >

The output is similar to “whoami” command.Printing current username name using "id" command

Conclusion

If you have made it to the end of the article, you can be confident that you have learned enough. Practicing the examples will help you to become a power user.


Similar Readings

Rate this post
Walid Al Asad

Hello Everyone! I am Walid Al Asad. Currently, I am working at a tech company named Softeko as a Linux Content Developer Executive. I live in Dhaka, Bangladesh. I have completed my BSc. in Mechanical Engineering from Bangladesh University of Engineering and Technology (BUET). You can find me on LinkedIn, and ResearchGate. Read Full Bio

Leave a Comment