← Full reference

who, w, last

Who is on the system now and what they're doing, login and reboot history, failed login attempts

These commands answer the questions "who is logged in right now?", "what are they running?" and "who logged in before, and when?". who and w read the list of current sessions from /var/run/utmp, last reads the login and reboot history from /var/log/wtmp, lastb reads failed login attempts from /var/log/btmp. They're used when investigating incidents and before rebooting a server — so you don't kick out people who are working.

Syntax

who [options] [file]
w [options] [user]
last [options] [user…] [terminal…]
lastb [options]               # same as last, but from /var/log/btmp

who — who is logged in

OptionWhat it shows
no optionsname, terminal, login time, where from (in parentheses)
-Hadd a header line
-qonly the names and the total count: # users=3
-btime of the last system boot
-rcurrent runlevel
-uadd idle time (. — active within the last minute, old — more than a day) and PID
-aeverything at once: -b -d --login -p -r -t -T -u
-m, who am ionly your own session (the terminal connected to stdin)
-Tstatus of accepting write messages: +, -, ?
fileread another file: who /var/log/wtmp — all logins from history

Related commands: whoami — the current user's name, users — logged-in names on one line, logname — the name used to log in (doesn't change after su).

w — who is doing what

The first line is like uptime: current time, how long the system has been running, number of users, load average over 1, 5 and 15 minutes.

ColumnMeaning
USERuser
TTYterminal: pts/N — an ssh session or terminal window, ttyN — console
FROMwhere the login came from (IP or host name; shown with -f if hidden)
LOGIN@login time
IDLEhow long the terminal has had no input
JCPUCPU time of all processes on this terminal
PCPUCPU time of the current process (from WHAT)
WHATthe command currently running in the session
OptionWhat it does
-hno header
-sshort format: without LOGIN@, JCPU, PCPU
-ftoggle the FROM column
-ishow IPs instead of host names
-pshow the PIDs of the processes in WHAT
-oold format: blank values instead of zero idle time
w useronly this user's sessions

last — login history

last reads /var/log/wtmp from newest to oldest entries. At the end it prints the date the file starts from (wtmp begins …).

OptionWhat it does
-n 10, -10show only the last 10 entries
useronly this user's entries: last alice
rebootreboot history (a pseudo-user)
terminalentries for a terminal: last pts/0
-s time / -t timeentries since / until the given time: -s yesterday, -s 2024-05-01, -s -2days
-p timewho was on the system at the given moment: -p '2024-05-01 15:00'
-Ffull login and logout dates and times
--time-format isotime format: notime, short, full, iso
-iIP addresses as numbers
-dresolve IPs to host names
-ahost name in the last column (not truncated)
-Rdon't show the host column
-wdon't truncate long user and domain names
-xalso shutdown and runlevel change entries
-f fileread another file, e.g. the old /var/log/wtmp.1

Line format: user terminal host login - logout (duration). Instead of the logout time you may see:

EntryMeaning
still logged inthe session is open now
still running(for reboot) the system has been running since this boot
downthe session closed during a normal shutdown
crashthe system crashed or rebooted without closing the session
gone - no logoutthe session process disappeared without a logout record

Duration: (01:05) — 1 hour 5 minutes, (2+04:00) — 2 days and 4 hours.

lastb — failed logins

lastb takes the same options as last and reads /var/log/btmp — wrong passwords and nonexistent users. The file is readable by root only. Each user's last login is shown by lastlog (lastlog -u name).

Examples

# who is on the system now, with headers
who -H

# how many users are logged in
who -q

# when the system booted
who -b
uptime -s

# what users are doing now, with IPs
w -i

# the last 10 logins
last -n 10

# all of a user's logins over the past day, full dates
last -F -s yesterday alice

# when the server was rebooted
last reboot | head

# who was on the system at a given moment
last -p '2024-05-01 15:00'

# top IPs by failed login attempts
lastb -i | awk '$3 ~ /^[0-9]/ { print $3 }' | sort | uniq -c | sort -rn | head

# view raw wtmp entries
utmpdump /var/log/wtmp | tail -3

Pitfalls

  • Empty output doesn't mean "nobody was here". In containers (and in the sandbox) logins don't go through login/sshd, so utmp and wtmp are empty: who prints nothing, last prints only wtmp begins ….
  • wtmp is rotated. last shows only the current file; for older history look at last -f /var/log/wtmp.1. The wtmp begins line tells you the date the data starts from.
  • who sees only sessions with a terminal. Commands via ssh host command, cron jobs and daemons don't show up in who and w — look for them in ps.
  • crash isn't always a server crash. Any session with no logout record before the next boot is marked this way — including after a hard reset of a virtual machine.
  • Names and hosts are truncated. Long user and host names in last are cut to the column width; use -w and -a.
  • lastb requires root: a regular user gets a permission error on /var/log/btmp.

See also

system-info · ps · id · su · sshd · date