Linux Command Reference ยท Permissions and Users
Linux last command
Reference for the Linux last command: read /var/log/wtmp to see past logins, reboots and session lengths, filter by user or time, and use lastb for failures.
last reads a login record file, normally /var/log/wtmp, and prints one line for every session it finds, newest first. Each line shows the user, the terminal, where the login came from, when it started and how long it lasted. Reboots and shutdowns appear as pseudo-users named reboot and shutdown, so the same output tells you when the machine was restarted.
The records are written by login, sshd and similar programs and are stored in a binary format, which is why you read them with last instead of cat. Its sibling lastb does the same for the file of failed logins, /var/log/btmp, and lastlog shows only the most recent login of every account.
last [OPTION]... [USER | TTY]...
Try it
The sandbox has no login records, so the first example shows what last does when the file is missing. The second one parses a sample listing, which is exactly the kind of text a real run produces.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
last [-n NUM] [-f FILE] [-s TIME] [-t TIME] [-F] [-i] [-a] [-x] [-R] [-w] [USER | TTY]... lastb [OPTION]...
| Option | Description |
|---|---|
-n NUM, -NUM |
Show only the newest NUM lines. |
-f FILE |
Read FILE instead of /var/log/wtmp. |
-s TIME, -t TIME |
Show sessions since / until a time (YYYYMMDDhhmmss, yesterday, -1hour and similar). |
-F |
Print full login and logout times including the date and year. |
-i |
Show the remote address as a numeric IP instead of a host name. |
-a |
Put the host name in the last column, which is easier to read for long names. |
-x |
Also show shutdown and runlevel-change records. |
-R |
Hide the host column. |
-w |
Do not truncate long user and host names. |
USER, TTY |
Only show records that match. A TTY may be given as pts/0 or just 0. |
Returns: 0 on success; a non-zero status if the record file cannot be opened.
Time column: a session that is still open shows still logged in; one that ended with a crash or reboot shows crash or down. The number in parentheses is the session length as hh:mm or d+hh:mm.
Usage notes
- The output ends with a line such as
wtmp begins Sun Mar 2 .... That date is the start of the file, not of the machine: logrotate rotateswtmp, so older history lives inwtmp.1. Read it withlast -f /var/log/wtmp.1. - Reading
/var/log/btmpneeds root. Runsudo lastbto see failed logins; many failures from one address is the classic sign of a password-guessing attack. - A user must have logged in through a program that writes
wtmp. Sessions started bysuorsudoare not recorded, and some containers and minimal systems have nowtmpat all. - Do not treat the file as proof. Anyone with root can edit or truncate it. For an audit trail use the journal or a remote log host (see Logging with journald).
- Scripts: parse
lastwithawkor use-Ffor stable, complete timestamps. Avoid cutting columns by position, because the host column width varies. last rebootlists only the reboot records, a quick way to find out how often a server restarted.- Difference from
whoandw: those show who is logged in right now (fromutmp);lastshows history.
Related
whoandw,id,su,passwd- Lessons: Users and permissions, Logging with journald