With the help of the whoami command in Linux, you can instruct your system to display the system user’s name who is currently executing the scripts. This command-line utility will help you learn about users in minutes.
When we talk about Linux utilities, some offer only a few command-line options and hence primarily aim to perform a single task; whoami is one of those.
The whoami command can be listed as one of the most accessible Linux commands. Just running the script will let you see what the current username is. Following this tutorial, you’ll understand these commands and how to use them in the most promising manner.
whoami command in Linux: Syntax
Before we start learning how to use the whoami command in Linux, let us first have a look at the basic structure that the command-line holds. The whoami command looks something like this:
whoami [OPTION]
Using whoami Command in Linux
The whoami command in Linux can be simply used to display the name of the currently logged-in user. For that, you’ll need to run the command without any option.
$ whoami
Executing the command in the format mentioned above will display the output as follows. It will simply show the name of the user who is currently invoking the command.
The whoami command can also be used in the shell. Such practices will help you check the name of the user who is running the script.
In the situation when the given string doesn’t match with the username executing, the whoami command
will simply echo a message before eventually exiting.
whoami Command in Linux for Verifying Switched Users
When we talk about the whoami command in Linux, it plays a vital role in verifying any user’s name after using the su command and switching to others.
whoami Commands Options
-h or --help flag
: Running this option alongside the whoami command will display a help message.
-V or --version flag
: This option can be used to instruct the system to show the version information.
whoami Command with Arguments
Unlike many other Linux commands, the whoami command doesn’t accept arguments. In case you decide to pass one, the only thing you’ll receive as output is an error message. For instance,
Input:
$ whoami something
Output:
whoami: extra operand ‘something.’ Try ‘whoami --help’ for more information.
Other Commands Related to whoami Command in Linux
w command: Running the command will help you get information about the logged-in users alongside their activities.
who command
: It reports users who are logged in to the concerned system.
How is whoami, and who am i Commands Different?
Although both whoami and who am i commands come in handy while getting the name of the users who are logged in, they work a bit differently in a certain scenario.
Like when anyone logs in as a root on the network, running any of the mentioned commands will return the root as output. However, when the user logs in as another root and switches to the primary one, who am i prints the originally logged-in user while whoami outputs the root.
Alternative to the whoami Command
While working with the Unix system, if you find the whoami command isn’t working for any reason, employing the id -un
command will also help print the current user name. Apart from that, it can be used for displaying effective group ID, group name, and more.
$ id and -g flag
: It prints the group ID.
$ id command with -gn flag
: The command can be used to show the effective group name.
$ id and -G flag
: Running this command will display the ID of every group to which a user belongs.
$ id -Gn
: For displaying the names of the groups, you can use this command.
What does whoami command in Linux do?
The whoami command in Linux displays the username, group ID, group name, and other information for the one who is currently logged in. The command is pretty much similar to how $id -un
works.
Why is whoami important?
There may be instances when you might have a number of usernames working in several UNIX systems. Under such a situation, invoking the whoami command before running any other script can help verify if the user account you’re logged in with is the appropriate one.
Are there any alternatives to the whoami command in Linux?
The id command can help you obtain a piece of more detailed information about a user. For that reason, the same can also be listed as a comprehensive alternative to the whoami command.
The whoami command in Linux is a pretty straightforward utility. When you’re working on any network or system and aren’t sure about the user with which you’re logged in, a quick execution will help you get the necessary information.
The command-line utility also helps in bash scripting, a situation when you need to run a script and test which user is executing it. In this article, we’ve discussed every possible way in which you can use whoami and the associated commands in Linux.
If this guide helped you, please share it.