Linux commands: who

By

Published · Updated

Tagged: CLI

Learn how the Linux who command lists the users currently logged in to the system, why you appear multiple times, and what the -aH flags add to the output.

The who command displays the users logged in to the system.

Unless you’re using a server multiple people have access to, chances are you will be the only user logged in, multiple times:

Terminal showing who command output with user flavio logged in multiple times on different ttys terminals with timestamps

Why multiple times? Because each shell opened will count as an access.

Each line has:

On a server, that last column is the useful one: it tells you where each SSH session is coming from.

Show more details with -aH

The -aH flags will tell who to display more information, including the idle time and the process ID of the terminal:

Terminal showing who -aH command output with column headers USER LINE WHEN IDLE PID COMMENT and detailed session information

-H adds the column headings. -a turns on most of the other options at once.

In the idle column, a . means the session was active in the last minute, and old means it’s been idle for more than 24 hours. Otherwise you see hours and minutes, like 12:58.

Who am I?

The special who am i command will list the current terminal session details:

Terminal showing who am i command output displaying single line with current user flavio on ttys004 terminal

Terminal showing who -aH am i command with detailed header columns and current session info including PID for user flavio

who am i is the same as who -m: it only shows the session attached to your current terminal. In fact any two words work. The GNU version’s help says “‘am i’ or ‘mom likes’ are usual”, so who mom likes does the same thing.

Don’t confuse it with whoami (one word). whoami prints just the name of the user you’re running as, while who am i shows the login session of your terminal. The difference shows up with sudo: sudo whoami prints root, because the command runs as root.

Useful options

These work in both the GNU version (Linux) and the BSD version (macOS):

OptionWhat it does
-Hadd column headings
-qonly the names and the number of users logged in
-btime of the last system boot
-uadd idle time and the process ID of each session
-Tshow if each user accepts messages: + yes, - no, ? error
-monly the current terminal, same as who am i
-sname, line and time only (the default)
-amost of the above at once

For example, to see when the machine was last started:

who -b
         system boot  Nov 22 14:14

And to count users:

who -q
flavio      flavio
# users = 2

Note that the count is sessions, not people. The same user logged in twice counts twice.

The GNU version on Linux has a few more options, like -r to print the current runlevel, and -l to list login processes. Run man who to see what your system supports.

Using who in scripts

To get the list of distinct users, take the first column and remove duplicates with sort -u:

who | awk '{print $1}' | sort -u

The users command gives you the logged in names on one line:

users

Where the data comes from

who reads the system’s login records file. On macOS that’s /var/run/utmpx, on Linux it’s usually /var/run/utmp.

You can pass a different file as an argument. On Linux, /var/log/wtmp keeps the history of past logins:

who /var/log/wtmp

For past logins, the last command is easier to read.

The who command works on Linux, macOS, WSL, and anywhere you have a UNIX environment

Want me to talk about your product? You can sponsor this site.

Related posts about cli: