How to Use the whoami Command in Linux

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.

  1. Open your Linux terminal.
  2. Type the following command:
    whoami
  3. 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.

  1. In the terminal, type sudo -i and press Enter. (You will need to provide your password).
  2. Notice that your command prompt likely changed from a $ (standard user) to a # (root user).
  3. Type whoami and press Enter.
  4. 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: Typing id and 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 the sudo or docker groups).
  • logname: Typing logname will display the name of the user who originally logged into the system. Unlike whoami, the logname output will not change even if you use su to switch to the root user. It always remembers the original human who sat down at the keyboard.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.