Website Vidya
SecurityStep-by-Step Guide

who Command in Linux: Check Currently Logged-In Users and Sessions

Learn who command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.

RR
Rajesh RayLead Systems Educator
Published:
12 min read
PDF Cheat Sheet
who Command in Linux: Check Currently Logged-In Users and Sessions
AI Executive SummaryWebsiteVidya AI

12 min read

Learn who command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.

Key Highlights
  • What who Does and When to Use It
  • Availability and Installation Across Linux Distributions
  • Command Syntax, Arguments, and Exit Status Codes
Grounded in full engineering runbookWebsiteVidya AI Engine

Mastering who command in linux is an essential skill for Linux system administrators, developers, and DevOps engineers. Knowing how to use the who command in Linux is essential for multi-user server administration, live incident response, and maintenance coordination. Before rebooting a production server, restarting the SSH daemon, or unmounting a shared filesystem, a responsible system administrator always checks whether colleagues or automated deployment pipelines have active terminal sessions open on the host.

Included in GNU coreutils, who reads the system's active session state and prints one line per logged-in terminal session: the username, the terminal line (tty1 for local virtual consoles or pts/0 for SSH/multiplexer pseudo-terminals), the login timestamp, and the remote hostname or IP address from which the session originated.

Beyond listing active human sessions, the who command in Linux also inspects system-level accounting records stored in /var/run/utmp—allowing you to check the exact time the system last booted (who -b), inspect current system runlevel states (who -r), identify idle time and login shell PIDs (who -u), or determine the original login owner of your current terminal even after switching accounts with su or sudo (who am i / who -m).

Advertisement
who Command in Linux: Check Currently Logged-In Users and Sessions — syntax, flags, and practical terminal workflow diagram
who Command in Linux: Check Currently Logged-In Users and Sessions — command syntax, key options, and production workflow reference.

What who Does and When to Use It

Unlike ps or top, which scan the /proc virtual filesystem for running processes, who reads binary utmpx accounting structures from /var/run/utmp (typically symlinked to /run/utmp). Whenever a user authenticates via sshd, local agetty/login, or a graphical display manager, PAM (pam_systemd / login) writes a USER_PROCESS struct record into /run/utmp containing the username (ut_user), terminal device (ut_line), login process PID (ut_pid), timestamp (ut_tv), and remote IP (ut_host). When the session closes, the record is cleared from /run/utmp and appended to /var/log/wtmp (which is read by last).

Because who relies on /run/utmp records, non-interactive processes (such as cron jobs, systemd background services, or ssh user@host 'single-command' calls that do not allocate a pseudo-terminal) do not register a USER_PROCESS entry in /run/utmp and will not appear in who. Similarly, terminal multiplexers like tmux or screen may or may not register utmp entries per window depending on whether utempter is enabled.

Linux enforces discretionary access control (DAC) by evaluating the effective User ID (EUID), effective Group ID (EGID), and supplementary groups against inode permission bits. When who executes, the Virtual File System (VFS) and active Linux Security Modules (SELinux or AppArmor) validate the caller's privileges before committing changes to disk or account databases.

Availability and Installation Across Linux Distributions

The who utility is packaged in coreutils across mainstream Linux distributions. It is pre-installed on standard Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Fedora, and Arch Linux systems, while minimal container images may require manual installation:

Advertisement

Before running administrative tasks or automation scripts, verify whether who is installed and inspect which binary path and version your shell resolves:

type -a who
who --version 2>/dev/null || man -f who

# Illustrative output (reviewed against documentation)
who is /usr/bin/who
who (coreutils)
# Debian / Ubuntu
sudo apt-get update && sudo apt-get install -y coreutils

# RHEL / Rocky / AlmaLinux / Fedora
sudo dnf install -y coreutils

# Arch Linux
sudo pacman -S --noconfirm coreutils

# Alpine Linux
sudo apk add --no-cache coreutils

Command Syntax, Arguments, and Exit Status Codes

who [OPTION]... [ FILE | ARG1 ARG2 ]

The basic syntax is who [OPTION]... [FILE | ARG1 ARG2]. When invoked with no non-option arguments, who reads /run/utmp. If you pass a single FILE path (such as who /var/log/wtmp), who parses that historical utmp-formatted binary file instead of the live session file.

As a special POSIX feature, if you pass exactly two non-option arguments—traditionally who am i or who mom likes (equivalent to who -m)—who filters its output to show only the utmp entry associated with the standard input (stdin TTY) of your current terminal session.

  • 0 — Session information retrieved and printed successfully.
  • 1 — Invalid command-line option or unreadable target utmp file.

Important Options and Flags Reference

Option / FlagLong Form / ArgumentTechnical Behavior and Purpose
-a--allEnable all diagnostic flags (-b -d --login -p -r -t -T -u) for a complete utmp report.
-b--bootDisplay the exact date and time of the last system boot.
-u--usersList logged-in users along with terminal idle time and the numeric PID of each login shell.
-H--headingPrint a descriptive column header line (NAME, LINE, TIME, IDLE, PID, COMMENT) above the output.
-mwho am iDisplay only the username and terminal entry associated with the current stdin terminal.
-q--countPrint only the login names and the total count of all currently logged-in sessions.
-r--runlevelPrint the current system runlevel (or systemd target equivalent recorded in utmp).
-T, -w--mesgShow each user's terminal message status (+ = writable by write/wall, - = disabled, ? = unknown).
--ipsIP addressesPrint numeric IP addresses instead of hostnames from utmp records when available.

For interactive system administration, who -Hu is the most informative everyday flag combination. Adding -H prints column headers so fields are easy to read, and -u adds two vital columns: the session's IDLE timer (., meaning active within the last minute, or HH:MM since the user last typed a keystroke) and the PID of the user's top-level login shell process.

Advertisement

Understanding who -m (or who am i with spaces) vs whoami (one word) is a classic Linux interview and production scripting question: after you escalate privileges with sudo -i or su -, whoami reports root (your effective UID), whereas who -m (who am i) inspects the controlling terminal /dev/pts/N in /run/utmp and reports the original human user (alex) who opened the SSH connection.

When combining short flags on the command line, POSIX and GNU utilities allow clustering boolean flags behind a single hyphen, provided any option taking an argument appears last. In automated shell scripts, using long-form GNU options where portable makes self-documenting code that is easier to audit during peer review.

Practical Basic and Advanced Usage Examples

1. List All Currently Logged-In Users (Basic who)

Run who with no arguments to see every active console and SSH terminal session on the host.

who

# Illustrative output (reviewed against documentation)
ubuntu   tty1         2026-10-22 08:16
alex     pts/0        2026-10-22 14:22 (192.0.2.45)
priya    pts/1        2026-10-22 15:10 (198.51.100.12)
alex     pts/2        2026-10-22 16:04 (192.0.2.45)

The four columns show the username (alex), terminal device (tty1 = local console, pts/0 = first SSH pseudo-terminal), login timestamp, and remote client IP address in parentheses.

2. Show Column Headers, Idle Time, and Login Shell PIDs (-Hu)

Combine -H (--heading) and -u (--users) to see which sessions are actively typing versus idle, along with their process IDs.

who -Hu

# Illustrative output (reviewed against documentation)
NAME     LINE         TIME             IDLE          PID COMMENT
ubuntu   tty1         2026-10-22 08:16 04:12        1140
alex     pts/0        2026-10-22 14:22   .          4821 (192.0.2.45)
priya    pts/1        2026-10-22 15:10 00:38        5319 (198.51.100.12)
alex     pts/2        2026-10-22 16:04 00:05        6104 (192.0.2.45)

In the IDLE column, a dot (.) means the terminal had activity within the last 60 seconds (alex on pts/0), whereas 04:12 means ubuntu on tty1 has been idle for 4 hours and 12 minutes. The PID column (5319) lets you inspect or terminate a stale session if needed.

3. Check Last System Boot Time and Runlevel (-b and -r)

Use -b to print the exact timestamp when the kernel/system last booted and -r to display the current runlevel.

who -b -r

# Illustrative output (reviewed against documentation)
system boot  2026-10-22 08:15
         run-level 5  2026-10-22 08:15

who -b reads the BOOT_TIME record written to /run/utmp at system startup, matching the boot time shown by uptime -s.

4. Identify the Original Login User of the Current Terminal (who am i / -m)

Even after running sudo -i or su - to become root, who -m (or who am i) reveals the original user account tied to your terminal.

sudo sh -c 'echo "Effective user (whoami): $(whoami)"; echo "Terminal owner (who -m):"; who -m'

# Illustrative output (reviewed against documentation)
Effective user (whoami): root
Terminal owner (who -m):
alex     pts/0        2026-10-22 14:22 (192.0.2.45)

While whoami reports root, who -m maps stdin (/dev/pts/0) against /run/utmp and reports that the session belongs to alex from 192.0.2.45.

5. Quickly Count Logged-In Users (-q)

Pass -q (--count) for a compact summary of logged-in usernames and the total session count.

who -q

# Illustrative output (reviewed against documentation)
ubuntu alex priya alex
# users=4

Notice that alex appears twice because alex has two open SSH terminals (pts/0 and pts/2), resulting in # users=4 active sessions.

6. Display All utmp Records Including Dead Processes (-a)

Run who -Ha to dump every record in /run/utmp, including system boot time, login prompts (LOGIN), active user sessions, and terminated processes.

who -Ha

# Illustrative output (reviewed against documentation)
NAME       LINE         TIME             IDLE          PID COMMENT  EXIT
           system boot  2026-10-22 08:15
           run-level 5  2026-10-22 08:15
LOGIN      tty2         2026-10-22 08:15              1145 id=tty2
ubuntu   + tty1         2026-10-22 08:16 04:12        1140
alex     + pts/0        2026-10-22 14:22   .          4821 (192.0.2.45)
priya    - pts/1        2026-10-22 15:10 00:38        5319 (198.51.100.12)

The + or - symbol next to the username (from -T, included in -a) indicates whether terminal messaging (write / mesg y) is enabled (+) or disabled (-) on that TTY.

Real-World Workflows, Pipelines, and Shell Automation

Pre-Reboot Safety Check Script: Warn If Other Administrators Are Active

Before running automated patching or rebooting a server, check who to see if any other SSH sessions are connected and print a clear alert.

#!/usr/bin/env bash
set -euo pipefail

ACTIVE_SESSIONS="$(who | wc -l)"
UNIQUE_USERS="$(who | awk '{print $1}' | sort -u | tr '
' ' ')"

echo "Active terminal sessions: $ACTIVE_SESSIONS (Users: $UNIQUE_USERS)"
if [ "$ACTIVE_SESSIONS" -gt 1 ]; then
  echo "WARNING: Multiple terminal sessions are active on $(hostname):"
  who -Hu
fi

# Illustrative output (reviewed against documentation)
Active terminal sessions: 4 (Users: alex priya ubuntu )
WARNING: Multiple terminal sessions are active on prod-web-01:
NAME     LINE         TIME             IDLE          PID COMMENT
ubuntu   tty1         2026-10-22 08:16 04:12        1140
alex     pts/0        2026-10-22 14:22   .          4821 (192.0.2.45)
priya    pts/1        2026-10-22 15:10 00:38        5319 (198.51.100.12)
alex     pts/2        2026-10-22 16:04 00:05        6104 (192.0.2.45)

Checking who | wc -l and printing who -Hu prevents accidental disruption of another engineer's active maintenance session.

Identify and Terminate a Stale or Unauthorized SSH Terminal Session

Using the PID column from who -Hu, an administrator can send SIGHUP (or SIGTERM) to close a specific hung or unauthorized pts/N session without affecting other sessions belonging to the same user.

who -Hu | grep "pts/2"
# Terminate the shell process on pts/2 (PID 6104):
sudo kill -HUP 6104
who -Hu

# Illustrative output (reviewed against documentation)
alex     pts/2        2026-10-22 16:04 00:05        6104 (192.0.2.45)
NAME     LINE         TIME             IDLE          PID COMMENT
ubuntu   tty1         2026-10-22 08:16 04:13        1140
alex     pts/0        2026-10-22 14:22   .          4821 (192.0.2.45)
priya    pts/1        2026-10-22 15:10 00:39        5319 (198.51.100.12)

Sending SIGHUP (kill -HUP 6104) to the specific login shell PID shown by who -Hu cleanly logs out pts/2 while leaving alex's active session on pts/0 untouched.

Pipelines, Redirection, Quoting, and Shell Expansion

When embedding who inside shell pipelines (|), conditional chains (&& and ||), or command substitutions ($(...)), the calling shell performs tilde expansion, variable interpolation, and globbing before invoking the binary. Always wrap paths, patterns, and variables in quotes so whitespace and wildcard characters (*, ?) are not expanded prematurely, and use set -euo pipefail in unattended scripts.

Permissions, Security Implications, and Safe Execution

Because /run/utmp is writable by root (and the utmp group if utempter is installed), an attacker who has already achieved root compromise can scrub their session entry from /run/utmp so they remain invisible to who and w. If you suspect a hidden SSH session, compare who -Hu against actual live sshd processes in kernel memory using sudo ps -ef | grep sshd: and ls -l /dev/pts/.

Note that Year-2038 readiness on 32-bit architectures and modern systemd distributions is leading some distributions to supplement or transition from legacy /run/utmp toward loginctl list-sessions (systemd-logind over D-Bus).

GNU/Linux, BSD, and BusyBox Implementation Differences

The who utility and flags -a, -b, -d, -H, -l, -m, -p, -q, -r, -s, -t, -T, and -u are part of the POSIX.1-2017 standard and work across GNU/Linux, FreeBSD, OpenBSD, and macOS.

On Alpine Linux (BusyBox), the default who applet supports only -a and -H unless you install GNU coreutils (apk add coreutils). Also note that Docker and Kubernetes containers rarely run a full login manager that populates /run/utmp, so who inside a container often returns empty output even when docker exec -it sessions are active.

Troubleshooting Common Errors and Diagnostics

who returns completely blank output even though I am logged in

Root Cause: You are running inside a container (docker exec), a non-login subshell (su without utmp registration), or on a minimal system where /run/utmp is not populated.

Resolution: Check active sessions with loginctl list-sessions, w, or inspect open pseudo-terminals directly with ls -l /dev/pts/ and ps -ef.

loginctl list-sessions 2>/dev/null || ps -ef | grep -E "pts/|tty"

who -m (who am i) prints nothing in a cron job or automated script

Root Cause: who -m looks up the terminal device attached to standard input (stdin). Non-interactive scripts and cron jobs do not have a controlling TTY attached to stdin.

Resolution: Use id -un or whoami (for effective user) or ${SUDO_USER:-$(id -un)} in non-interactive scripts.

echo "Effective user: $(id -un), Original sudo caller: ${SUDO_USER:-$(id -un)}"

Stale / ghost user session still shows in 'who' after SSH disconnect

Root Cause: If an SSH connection drops abruptly (network timeout) before TCP keepalives expire, sshd and /run/utmp still consider the session open until the process exits.

Resolution: Check who -Hu to find the PID of the stale session, verify whether the process still exists with ps -p PID, and kill the hung sshd / shell process if needed.

who -Hu
ps -fp 6104

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
users prints only a flat space-separated list of logged-in usernames. who prints one line per logged-in session showing username, terminal (tty/pts), login time, and remote IP. w shows everything in who plus system uptime, load averages, and the exact foreground command each user is currently running.
whoami (one word) prints your current effective username (so it prints 'root' after you run sudo su). 'who am i' (three words, equivalent to who -m) inspects your terminal session in /run/utmp and prints the original username you logged in with before using su or sudo.
By default, who reads active session records from the binary file /var/run/utmp (or /run/utmp), which is maintained by login programs such as sshd, login, and display managers.
Run who -b (or who --boot). It reads the BOOT_TIME entry from /run/utmp and prints the exact date and time of the last system startup.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout