whoamicommand allows Linux users to see the currently logged-in user. The output displays the username of the effective user in the current shell. Additionally,
whoamiis useful in bash scripting to show who is running the script.In this tutorial, you will learn how to use the
whoamicommand with examples.
- A system running Linux.
- Access to the command line/terminal.
whoami Command SyntaxThe syntax for the
whoami Command OptionsThe
whoamicommand comes with the two following options:
|--help||Shows a help message and exits.|
|--version||Shows the version information and exits.|
whoami Command ExamplesThere are several different ways to use
whoami. The examples below explain common use cases of the
Basic whoami UsageWhen running
whoamiwithout any options, the output shows the name of the currently logged-in user.To test the command, open the terminal and enter:
The output prints the name of the effective user.
Verify the Effective UserWhen you have more than one account, use
whoamito verify the username after switching to a different account.For example, switch to another user with the su command:
Then, run the
su [account name]
whoamicommand to verify the effective user: Since you executed the command as a different user, the terminal shows another username.
Check an Account for Sudo PermissionsUse the
whoamicommand to check if a user has
sudoprivileges. To do this, execute the command with sudo and provide the password:
In case you don’t have sudo privileges, the output looks like this:
Note: Learn how can you create sudo user.
Confirm Which User is Running a ScriptThe
whoamicommand in bash scripts shows which user is running the script. For example, use
whoamito test if root is executing the script, and if so, print a warning message using the echo command.Use a simple bash if statement to test this:
if [[ "$(whoami)" != 'root' ]] then echo "You are running this script as root user!" fi
List All whoami OptionsExecute the following command to show the help message for
whoamiand see all available options:
The output prints the usage of the
whoamicommand and provides the alternative
id -uncommand. Furthermore, the terminal also shows links to the online help page and the full documentation website.
Check whoami Version and ExitTo check the version of the
whoami vs. Other CommandsSome commands print the same output as
whoami. The examples below provide the
whoami vs. wWhile
whoamionly prints the username of the effective user, the w command provides more details. That is, the
wcommand shows where users are logged in from and what they are currently doing.
whoami vs. lognameBoth
lognameshow the name of the current user. The difference is that while the
whoamicommand shows the effective user, the
lognamecommand only returns the username.Test this by running the commands with
sudo: When using
logname, the command returns the name of the current user. However, if you execute
whoami, the command returns root.
whoami vs. whoThe who command returns info about all logged-in users (not only the effective users). The command provides not only usernames but also shows their terminal line and the time they logged in.
whoami vs. idWhen executing the
idcommand without any options, it returns the currently logged-in user details, like the user id, the group id, and the list of groups the user is in. However, if you execute the
idcommand with the
-unoption, the output is the same as with
ConclusionAfter following this tutorial, you should know how to use the
whoamicommand to find the name of the effective user on a Linux system.Check out and download this helpful Linux commands cheat sheet or refer to our in-depth list of basic Linux commands.