CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Users & Groups / who

who

List logged-in users

Beginneruserssessionsaudit

who lists the login sessions currently open on the machine: user, terminal, login time and where they connected from. It also tells you when the system last booted. Check it before a reboot and after a suspicious alert.

Syntaxwho [OPTION]... [FILE | am i]

Watch it run

You need to reboot devops-lab for a kernel patch and want to know who you'd kick off.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
who

One line per session: user, terminal, login time and origin. priya has an SSH session and a tmux window, which tmux registers as its own entry.

Anatomy of a command

$
commandwho

Reads the utmp login records (coreutils).

How it works

Login programs (sshd, login, terminal emulators, tmux) write a record to /var/run/utmp when a session starts and mark it dead when it ends; the same records are appended to /var/log/wtmp as history. who simply reads utmp and prints the USER_PROCESS entries: name, terminal line (pts/N for SSH and terminals, ttyN for consoles), login time and the remote host or display. With -u it stats the terminal device to compute idle time (time since anything was typed) and shows the PID of the session leader. who -b reads the BOOT_TIME record. who does not query sshd or systemd-logind, so sessions that never write utmp (su, sudo, non-interactive ssh commands, scp) don't appear, and a crashed session can leave a stale entry.

Options you'll actually use

-HPrint column headings
-uAdd idle time and session PID
-bTime of last system boot
-qOnly names and a user count
-aEverything: boot, runlevel, login processes, users
-rCurrent runlevel
-TAdd + / - : whether the user accepts write/wall messages
am iOnly your own session (same as -m)
FILERead another file, e.g. /var/log/wtmp for history

Common mistakes

Using who to see everyone who touched the server

who shows only interactive sessions recorded in utmp. scp, sftp, ssh host 'cmd', cron and su don't appear.

Use last for history, journalctl -u ssh or /var/log/auth.log for every SSH auth, and auditd for commands
Expecting who am i to print root after sudo -i

who am i looks up the utmp entry for your terminal, which records the original login name.

Use whoami or id -un to see the current effective user
Trusting an old who entry as a live session

If a session crashed, its utmp entry can be left behind.

Cross-check with who -u (the PID) and ps -p PID, or loginctl list-sessions

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    List current sessions with column headings and idle times.

  2. 2

    Show when the system was last booted.

  3. 3

    Show only your own session, then compare with whoami after sudo -i.

Related commands

wShow who is logged in and what they're doinglastShow login history, reboots and failed loginsidShow user and group identityuptimeHow long the system has run, and its load average
PreviousidNextw
Linux / Users & Groups / who

who

List logged-in users

Beginneruserssessionsaudit

who lists the login sessions currently open on the machine: user, terminal, login time and where they connected from. It also tells you when the system last booted. Check it before a reboot and after a suspicious alert.

Syntaxwho [OPTION]... [FILE | am i]

Watch it run

You need to reboot devops-lab for a kernel patch and want to know who you'd kick off.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
who

One line per session: user, terminal, login time and origin. priya has an SSH session and a tmux window, which tmux registers as its own entry.

Anatomy of a command

$
commandwho

Reads the utmp login records (coreutils).

How it works

Login programs (sshd, login, terminal emulators, tmux) write a record to /var/run/utmp when a session starts and mark it dead when it ends; the same records are appended to /var/log/wtmp as history. who simply reads utmp and prints the USER_PROCESS entries: name, terminal line (pts/N for SSH and terminals, ttyN for consoles), login time and the remote host or display. With -u it stats the terminal device to compute idle time (time since anything was typed) and shows the PID of the session leader. who -b reads the BOOT_TIME record. who does not query sshd or systemd-logind, so sessions that never write utmp (su, sudo, non-interactive ssh commands, scp) don't appear, and a crashed session can leave a stale entry.

Options you'll actually use

-HPrint column headings
-uAdd idle time and session PID
-bTime of last system boot
-qOnly names and a user count
-aEverything: boot, runlevel, login processes, users
-rCurrent runlevel
-TAdd + / - : whether the user accepts write/wall messages
am iOnly your own session (same as -m)
FILERead another file, e.g. /var/log/wtmp for history

Common mistakes

Using who to see everyone who touched the server

who shows only interactive sessions recorded in utmp. scp, sftp, ssh host 'cmd', cron and su don't appear.

Use last for history, journalctl -u ssh or /var/log/auth.log for every SSH auth, and auditd for commands
Expecting who am i to print root after sudo -i

who am i looks up the utmp entry for your terminal, which records the original login name.

Use whoami or id -un to see the current effective user
Trusting an old who entry as a live session

If a session crashed, its utmp entry can be left behind.

Cross-check with who -u (the PID) and ps -p PID, or loginctl list-sessions

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    List current sessions with column headings and idle times.

  2. 2

    Show when the system was last booted.

  3. 3

    Show only your own session, then compare with whoami after sudo -i.

Related commands

wShow who is logged in and what they're doinglastShow login history, reboots and failed loginsidShow user and group identityuptimeHow long the system has run, and its load average
PreviousidNextw