Whoami Command in Linux
Introduction
The whoami
command in Linux is a simple utility that displays the username of the current logged-in user. It retrieves the information from the system and prints the effective user ID (EUID).
When you run the whoami
command in the terminal, it returns the username associated with your current user session. This command is useful when you need to quickly verify the user you are logged in as, especially in scripts or command line operations that require user-specific actions or permissions.
In this tutorial, we will talk about whoami
command. We will also address a few FAQs on whoami command in Linux.
How to Use the whoami
Command
The whoami
command has the following syntax:
whoami [OPTION]
Invoke the command without any parameters to see the name of the currently logged-in user:
whoami
The following output will be displayed on the screen, along with the name of the user who invoked the command:
vegastack
In shell scripts, the whoami
command can be used to determine the user who is running the script.
Here's an example of a script that uses an if statement to compare the user's name with a string.
if [[ "$(whoami)" != "any_name" ]]; then
echo "Only user 'any_name' can run this script."
exit 1
fi
The script will echo a message and exit if the username does not match the specified string.
The whoami
command is also useful for validating the user's name after using the su
command to switch to another user.
Arguments are not accepted by whoami
. The command prints an error message if you give an argument:
Output
whoami: extra operand ‘anything’
Try 'whoami --help' for more information.
Only two choices are available when using the whoami command:
-h
,--help
- Show a help message before exiting.-V
,--version
- Displays and exits the version information.
Alternative Commands
When you execute the id
command with the -un
parameters, you get the same results as when you run whoami
:
whoami [OPTION]
To learn more about a specific user, use the id
command.
The logged-in user's name is stored in the $USER
environment variable:
echo $USER
FAQs on whoami command in Linux
Can the output of the whoami
command be different for different users?
Yes, the output of the whoami
command depends on the user who runs it. Each user will see their own username displayed when executing the whoami
command.
Is there any option or flag available for the whoami
command?
No, the whoami
command does not have any additional options or flags. It is a straightforward command that solely displays the current username.
What should I do if the whoami
command shows a different username than expected?
If the whoami
command displays a different username than you expect, it could indicate that you are logged in with a different user account than intended. Verify that you entered the correct credentials during login or contact the system administrator for further assistance.
Can the whoami
command display the user's full name instead of the username?
No, the whoami
command specifically displays the username associated with the current user session. It does not provide the user's full name or any additional information.
Is there a way to display the ID numbers (UID, GID) along with the username using whoami
?
No, the whoami
command solely displays the username and does not provide any additional information, such as user ID (UID) or group ID (GID). To view the UID and GID, you can use other commands such as id
or ls -l
on a file or directory owned by the user.
Can the whoami
command be used by any user?
Yes, any user can use the whoami
command to determine their own username. It does not require special permissions or privileges.
Is there an alternative command to whoami
for obtaining the current username?
No, there is no direct alternative to the whoami
command to obtain the current username. However, you can retrieve the username by using other commands or environment variables, such as id -un
or echo $USER
.
Conclusion
The whoami
command, which is a compound of the terms "Who am I?", publishes the name of the user linked with the current effective user ID.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.