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
| Option | What it shows |
|---|---|
| no options | name, terminal, login time, where from (in parentheses) |
-H | add a header line |
-q | only the names and the total count: # users=3 |
-b | time of the last system boot |
-r | current runlevel |
-u | add idle time (. — active within the last minute, old — more than a day) and PID |
-a | everything at once: -b -d --login -p -r -t -T -u |
-m, who am i | only your own session (the terminal connected to stdin) |
-T | status of accepting write messages: +, -, ? |
file | read 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.
| Column | Meaning |
|---|---|
USER | user |
TTY | terminal: pts/N — an ssh session or terminal window, ttyN — console |
FROM | where the login came from (IP or host name; shown with -f if hidden) |
LOGIN@ | login time |
IDLE | how long the terminal has had no input |
JCPU | CPU time of all processes on this terminal |
PCPU | CPU time of the current process (from WHAT) |
WHAT | the command currently running in the session |
| Option | What it does |
|---|---|
-h | no header |
-s | short format: without LOGIN@, JCPU, PCPU |
-f | toggle the FROM column |
-i | show IPs instead of host names |
-p | show the PIDs of the processes in WHAT |
-o | old format: blank values instead of zero idle time |
w user | only 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 …).
| Option | What it does |
|---|---|
-n 10, -10 | show only the last 10 entries |
user | only this user's entries: last alice |
reboot | reboot history (a pseudo-user) |
terminal | entries for a terminal: last pts/0 |
-s time / -t time | entries since / until the given time: -s yesterday, -s 2024-05-01, -s -2days |
-p time | who was on the system at the given moment: -p '2024-05-01 15:00' |
-F | full login and logout dates and times |
--time-format iso | time format: notime, short, full, iso |
-i | IP addresses as numbers |
-d | resolve IPs to host names |
-a | host name in the last column (not truncated) |
-R | don't show the host column |
-w | don't truncate long user and domain names |
-x | also shutdown and runlevel change entries |
-f file | read 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:
| Entry | Meaning |
|---|---|
still logged in | the session is open now |
still running | (for reboot) the system has been running since this boot |
down | the session closed during a normal shutdown |
crash | the system crashed or rebooted without closing the session |
gone - no logout | the 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, soutmpandwtmpare empty:whoprints nothing,lastprints onlywtmp begins …. wtmpis rotated.lastshows only the current file; for older history look atlast -f /var/log/wtmp.1. Thewtmp beginsline tells you the date the data starts from.whosees only sessions with a terminal. Commands viassh host command, cron jobs and daemons don't show up inwhoandw— look for them inps.crashisn'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
lastare cut to the column width; use-wand-a. lastbrequires root: a regular user gets a permission error on/var/log/btmp.