When working in the Linux terminal, you usually know exactly who you are logged in as. However, if you are constantly switching between user accounts using the su command, testing scripts, or acting as the root user via sudo -i, you might lose track of which security context your terminal is currently operating under.
Executing a powerful command like rm -rf / as a standard user will result in a harmless “Permission denied” error. Executing it as the root user will destroy your entire operating system. Therefore, verifying your current identity is critical.
The easiest way to check this is by asking Linux a simple question: “Who am I?”
How to Use the whoami Command
The whoami command is one of the simplest commands in Linux. It has no complex syntax and requires no arguments or flags.
- Open your Linux terminal.
- Type the following command:
whoami - Press Enter.
The terminal will instantly output the username associated with your current effective user ID.
For example, if you logged into your Ubuntu laptop as “david”, the output will simply be:
david
Testing the Output with Root
To see why this command is useful, you can test it by switching to the root user account.
- In the terminal, type
sudo -iand press Enter. (You will need to provide your password). - Notice that your command prompt likely changed from a
$(standard user) to a#(root user). - Type
whoamiand press Enter. - The output will now say:
root
This confirms that any command you type in this specific terminal window will be executed with absolute administrative privileges.
Type exit and press Enter to leave the root session and return to your standard user account.
Alternative Commands: id and logname
While whoami is great for a quick glance, Linux offers a few other commands if you need more detailed information about your identity.
id: Typingidand pressing Enter provides much more detail. It will show your username, your numeric User ID (UID), the primary group you belong to (GID), and a list of all secondary groups you are a member of (such as thesudoordockergroups).logname: Typinglognamewill display the name of the user who originally logged into the system. Unlikewhoami, thelognameoutput will not change even if you usesuto switch to the root user. It always remembers the original human who sat down at the keyboard.