[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMarch 12, 2023

How to use Whoami Command

How To Use Whoami Command

In this article, we will cover the whoami command.

As its name suggests, the whoami command prints the user name of the effective user ID. In other words, it displays the name of the currently logged-in user.

 

How to Use the whoami Command

The syntax for the whoami command is as follows:

whoami [OPTION]

To display the name of the currently logged user, invoke the command without any options:

whoami

Output similar to the following will be displayed on the screen, showing the name of the user invoking the command:

root

You can use the whoami command in shell scripts to check the user’s name running the script.

Here is an example using an if statement to compare the user’s name running the script with a given string.

if [[ "$(whoami)" != "any_name" ]]; then
  echo "Only user 'any_name' can run this script."
  exit 1
fi

If the user name does not match the given string, the script will echo a message and exit.

The whoami command is also handy for verifying the user’s name after switching to another user with the su command.

whoami does not accept arguments. If you pass an argument, the command prints an error message:

whoami: extra operand ‘anything’
Try 'whoami --help' for more information.

The whoami command accepts only two options:

  • -h, –help – Display a help message and exit.

  • -V, –version – Shows the version information and exit.

 

Alternative Commands

Running the id command with the -un options produces the same output as running whoami:

whoami [OPTION]

Use the id command to obtain more information about a given user.

The $USER environment variable contains the name of the logged-in user:

echo $USER

Conclusion

The whoami command is a compound of the words “Who am I?” and prints the name of the user associated with the current effective user ID.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM