DEV Community

Alex
Alex

Posted on

Difference between the whoami and who am i commands

In the world of Linux, where command-line proficiency is a valuable skill, understanding the nuances of various commands can significantly enhance user experience and system administration. Two such commands that often cause confusion due to their similar syntax and functionality are whoami and who am i. Despite their apparent similarities, these commands serve different purposes and provide distinct outputs.

The whoami Command:
The whoami command is straightforward; it displays the username of the current user—the effective user ID (EUID) running the shell or script. It's a simple command without options, and its output is the username that you're operating under after any initial logins or user switches.

Example:

$ whoami
john_doe
Enter fullscreen mode Exit fullscreen mode

The who am i Command:
On the other hand, who am i is a usage of the who command with the am i option. This command provides information about the original user who logged into the system, even if you've switched users using the su command. It's particularly useful for checking the real user ID (RUID) when you're operating under a different user's environment.

Example:

$ su - jane_doe
$ who am i
john_doe   pts/0        2024-04-08 23:20 (:0)
Enter fullscreen mode Exit fullscreen mode

In this example, even though the current effective user is jane_doe, who am i returns john_doe, which is the user that originally logged into the system.

Comparative Analysis:
The key difference lies in the context of the user identity they report. whoami gives you the identity under which the current process is running, which could be the result of a series of user switches. In contrast, who am i provides the identity of the user who initiated the session, regardless of any subsequent su commands.

Practical Use Cases:

  • whoami is commonly used in scripts to check the current user before executing commands that require specific user privileges.
  • who am i is helpful for system administrators to track the original login, especially when multiple su commands are used to switch between users.

Top comments (1)

Collapse
 
dreamhollow4219 profile image
Ian

Interesting.

There's a lot I still don't know about the "server side" implementation of Linux that this clearly caters towards. Still a lot I don't fully understand about this OS.

But seeing this reminds me of a video I saw several months ago talking about Linux and multi-seating in regards to older computer usage.