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]...
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)
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:
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.For the user jim you can see the output in the below image.
Similar Readings
- The “usermod” Command in Linux [14+ Practical Examples]
- The “users” Command in Linux [4 Practical Examples]
- The “who” Command in Linux [6 Practical Examples]
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.Otherwise, the output will look like this.
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
➌ 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.For root user the output will be like this:
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:
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.
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.
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