Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
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.

Bash Reading a record file

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.

Bash Parsing last output (sample data)

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

Terminal
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 rotates wtmp, so older history lives in wtmp.1. Read it with last -f /var/log/wtmp.1.
  • Reading /var/log/btmp needs root. Run sudo lastb to 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 by su or sudo are not recorded, and some containers and minimal systems have no wtmp at 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 last with awk or use -F for stable, complete timestamps. Avoid cutting columns by position, because the host column width varies.
  • last reboot lists only the reboot records, a quick way to find out how often a server restarted.
  • Difference from who and w: those show who is logged in right now (from utmp); last shows history.