1. Introduction

Due to the often non-graphical nature of Linux interfaces, we sometimes need to find out information about the current user profile and session when already logged in. Moreover, a list of current and past session data can also be beneficial. These two scenarios are covered by two commands with similar names but different functions:

In this tutorial, we explore the difference and similarities between who and whoami. First, we talk about whoami and another command that can return current user information. After that, we thoroughly explain who and most of its options. Finally, we perform a short comparison between the two.

We tested the code in this tutorial on Debian 12 (Bookworm) with GNU Bash 5.2.15. It should work in most POSIX-compliant environments unless otherwise specified.

2. Get Current User Information

Sometimes, we need data about the current session or effective user ID (UID). The effective user ID is the one that new processes execute with.

Both commands below are part of the GNU coreutils package.

2.1. whoami

To begin with, the whoami command is a quick way to find out the effective username of the current terminal session:

$ whoami
baeldung

In this case, we’re running the shell as baeldung. Following the UNIX principle of do one thing, but do it well or one problem, one program, this is the only function of whoami.

It’s based on the POSIX-standard geteuid() C library function. Let’s look at another tool that also utilizes the same function but much more extensively.

2.2. id

In fact, the output of whoami coincides with the output of another command:

$ id -un
baeldung

Unlike the minimalistic whoami, the id command can show other related information as well:

  • security –context (-Z) of the process
  • effective –group (-g) ID
  • effective –user (-u) ID
  • –real (-r) ID

We can also resolve ID numbers to [–name]s (-n), print the ID of all –groups (-G), and format the output with –zero (-z) terminators. By default, id alone shows all user and group data about the current process.

After getting to know who and one of its relatives, let’s move on to another command.

3. Get Session and System Information

Unlike the whoami command, who doesn’t limit itself to the current session. In fact, we see data about all users currently logged into the system:

$ who
root     tty1         2023-11-11 10:10
baeldung pts/0        2023-10-28 06:56 (192.168.6.66)

In this case, baeldung is a local user session at tty 1, while the root login came over the network from IP 192.168.6.66 and is currently at pts 0. Further, we can see the initial date and time of each.

Importantly, the default (–short, -s) output of who doesn’t hint at the context it was called from.

3.1. Include Details

We can also add column [–heading]s and a bit more detail via the –users flag:

$ who --heading --users --mesg
NAME      LINE         TIME             IDLE          PID COMMENT
noot    - tty1         2023-11-11 10:10 15:56       50005
root    - pts/0        2023-10-28 06:56   .         20666 (192.168.6.66)

In addition, the -T, -w, and –mesg flags all have the same function, namely to append a character after the user name that indicates whether they allow writing messages to their terminal:

  • + allowed
  • disallowed
  • ? cannot find terminal device

In addition to the user data, we can also get further information with who.

3.2. Last Times

Among other things, who can tell us the last times of some important events:

  • system clock –time change
  • system –boot (-b) time
  • system –runlevel change

Let’s see the latter two in action:

$ who --boot --runlevel
         system boot  2023-10-10 10:10
         run-level 5  2023-10-10 10:10

So, our system last started on 10 Oct (10) 2023 at 10:10 and entered run-level 5.

3.3. IP and Name Translation

With who, we can ensure the command outputs –ips instead of hostnames, but there is also the –lookup flag that canonicalizes hostnames via DNS.

3.4. Special Processes

In addition to other information, we can get data about a number of special processes in the system:

These options make who an all-around command to query system information around users and sessions.

4. who vs. whoami

In summary, the who and whoami commands differ quite a bit.

In fact, the one and only purpose of whoami is to show the current user. Meanwhile, the who command can display information about all sessions on the system along with data such as boot times and login process PID numbers.

In short, we use whoami as a quick way to check the current execution context and who – to understand the global system situation with regard to login processes, times, terminals, and users.

5. Summary

In this article, we talked about the similarly-named who and whoami commands.

In conclusion, the who command is much more versatile than whoami to the point that their main similarities end with the name and current user output.

Comments are closed on this article!