Website Vidya
SecurityStep-by-Step Guide

last Command in Linux: Check User Login, Reboot, and Shutdown History

Learn last command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.

RR
Rajesh RayLead Systems Educator
Published:
10 min read
PDF Cheat Sheet
last Command in Linux: Check User Login, Reboot, and Shutdown History
AI Executive SummaryWebsiteVidya AI

10 min read

Learn last command in linux with syntax, essential flags, practical terminal examples, real-world sysadmin workflows, security tips, and troubleshooting.

Key Highlights
  • What last Does and When to Use It
  • Availability and Installation Across Linux Distributions
  • Command Syntax, Arguments, and Exit Status Codes
Grounded in full engineering runbookWebsiteVidya AI Engine

Mastering last command in linux is an essential skill for Linux system administrators, developers, and DevOps engineers. Using the last command in Linux is a cornerstone of server forensics, security auditing, and uptime diagnostics. While commands like who and w only show who is connected at this very second, last looks backward in time—revealing everyone who has logged into or out of the machine, which IP address they connected from, when they disconnected, how long their session lasted, and whether the server was gracefully shut down or suffered an unexpected power loss.

Whenever an SSH session, console login, or system reboot occurs, the Linux login subsystem appends a binary accounting record to /var/log/wtmp. The last command parses this binary log from the end of the file back to the beginning so the most recent activity always appears at the top of your terminal.

Alongside last, the util-linux package provides its twin binary lastb, which reads /var/log/btmp using the exact same flags to list bad (failed) login attempts—allowing administrators to spot SSH brute-force attacks and unauthorized access attempts immediately.

Advertisement
last Command in Linux: Check User Login, Reboot, and Shutdown History — syntax, flags, and practical terminal workflow diagram
last Command in Linux: Check User Login, Reboot, and Shutdown History — command syntax, key options, and production workflow reference.

What last Does and When to Use It

Traditionally, last reads utmpx C structures from /var/log/wtmp, while lastb reads identical structures from /var/log/btmp. Unlike /var/log/wtmp, which is world-readable (0664 root:utmp), /var/log/btmp is strictly restricted (0600 or 0660 root:utmp) because users frequently mistype their plaintext password into the Username: prompt by mistake; hiding /var/log/btmp from non-root users prevents accidental credential exposure.

On newest Linux releases (such as Debian 13+, openSUSE Tumbleweed, and modern Fedora/Ubuntu releases adopting Year-2038-safe 64-bit timestamps), last may be provided by wtmpdb (storing records in /var/lib/wtmpdb/wtmp.db SQLite database via pam_wtmpdb.so) while supporting the same familiar CLI flags. When logrotate rotates /var/log/wtmp monthly into /var/log/wtmp.1, you can pass -f /var/log/wtmp.1 to inspect older months.

Linux enforces discretionary access control (DAC) by evaluating the effective User ID (EUID), effective Group ID (EGID), and supplementary groups against inode permission bits. When last executes, the Virtual File System (VFS) and active Linux Security Modules (SELinux or AppArmor) validate the caller's privileges before committing changes to disk or account databases.

Availability and Installation Across Linux Distributions

The last utility is packaged in util-linux / wtmpdb across mainstream Linux distributions. It is pre-installed on standard Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Fedora, and Arch Linux systems, while minimal container images may require manual installation:

Advertisement

Before running administrative tasks or automation scripts, verify whether last is installed and inspect which binary path and version your shell resolves:

type -a last
last --version 2>/dev/null || man -f last

# Illustrative output (reviewed against documentation)
last is /usr/bin/last
last (util-linux / wtmpdb)
# Debian / Ubuntu
sudo apt-get update && sudo apt-get install -y util-linux

# RHEL / Rocky / AlmaLinux / Fedora
sudo dnf install -y util-linux

# Arch Linux
sudo pacman -S --noconfirm util-linux

# Alpine Linux
sudo apk add --no-cache util-linux-misc

Command Syntax, Arguments, and Exit Status Codes

last [OPTIONS] [USERNAME...] [TTY...]

The command syntax is last [OPTIONS] [USERNAME...] [TTY...]. If you pass one or more usernames (last alex priya) or terminal names (last tty1 pts/0), last filters the output to show only matching sessions. Passing the pseudo-user reboot (last reboot) prints a log of every system boot event.

Each output line displays the username, terminal (pts/0, tty1, or system boot), remote client IP or kernel version, login start time, logout end time (or still logged in, crash, or down), and total session duration in (days+HH:MM) format.

  • 0 — Login history read and formatted successfully.
  • 1 — Invalid option, missing /var/log/wtmp file, or permission denied when reading /var/log/btmp.

Important Options and Flags Reference

Option / FlagLong Form / ArgumentTechnical Behavior and Purpose
-n, -NUMBER--limit NUMBERDisplay only the most recent NUMBER of login records (e.g., last -n 10 or last -10).
-F--fulltimesPrint full login and logout dates and timestamps (including day, month, date, HH:MM:SS, and year).
-w--fullnamesDisplay full usernames and domain names without truncating them to 8 characters.
-i--ipAlways display the remote host as a numeric IPv4/IPv6 address instead of a resolved hostname.
-a--hostlastPrint the remote hostname or IP address in the final column on the right so it is never truncated.
-x--systemDisplay system shutdown entries, runlevel changes, and reboot records alongside user logins.
-s--since TIMEDisplay login sessions that occurred after the specified timestamp (YYYY-MM-DD or 'yesterday').
-t--until TIMEDisplay login sessions that occurred before the specified timestamp (YYYY-MM-DD HH:MM:SS).
-p--present TIMEList only the users who were actively logged in at the exact specified timestamp.
-f--file FILERead login records from a specific wtmp/btmp file (such as /var/log/wtmp.1 or /var/log/btmp).

Default last output truncates usernames longer than 8 characters, abbreviates timestamps without seconds or years, and truncates long remote hostnames in the third column. In professional security audits and incident response, always run last -Fwai (--fulltimes --fullnames --hostlast --ip). This combination prints complete usernames, full timestamps with seconds and four-digit years, and untruncated numeric IP addresses in the last column.

Advertisement

The -p (--present) flag is a forensic superpower: running last -Fwai -p "2026-10-22 14:30:00" instantly tells you every user who had an open session on the server at 14:30:00 during an outage or security incident.

When combining short flags on the command line, POSIX and GNU utilities allow clustering boolean flags behind a single hyphen, provided any option taking an argument appears last. In automated shell scripts, using long-form GNU options where portable makes self-documenting code that is easier to audit during peer review.

Practical Basic and Advanced Usage Examples

1. View the Most Recent User Logins (-n)

Display the 8 most recent login and reboot records from /var/log/wtmp.

last -n 8

# Illustrative output (reviewed against documentation)
alex     pts/2        192.0.2.45       Thu Oct 22 16:04 - 16:40  (00:36)
priya    pts/1        198.51.100.12    Thu Oct 22 15:10   still logged in
alex     pts/0        192.0.2.45       Thu Oct 22 14:22   still logged in
ubuntu   tty1                          Thu Oct 22 08:16   still logged in
reboot   system boot  6.8.0-45-generic Thu Oct 22 08:15   still running
alex     pts/0        192.0.2.45       Wed Oct 21 19:10 - down   (02:05)
priya    pts/1        198.51.100.12    Wed Oct 21 17:45 - 18:30  (00:45)
reboot   system boot  6.8.0-45-generic Wed Oct 21 09:00 - 21:15  (12:15)

wtmp begins Thu Oct  1 00:00:01 2026

Records are shown newest-first. Notice still logged in for currently open sessions and - down when a session remained open until the system shut down cleanly.

2. Print Forensically Complete Timestamps, Full Usernames, and IPs (-Fwai)

Use -Fwai to prevent username truncation, show exact seconds and years, and place numeric IP addresses in the final column.

last -Fwai -n 5

# Illustrative output (reviewed against documentation)
alexander     pts/2        Thu Oct 22 16:04:12 2026 - Thu Oct 22 16:40:05 2026  (00:35)     192.0.2.45
priya         pts/1        Thu Oct 22 15:10:44 2026   still logged in                       198.51.100.12
alexander     pts/0        Thu Oct 22 14:22:09 2026   still logged in                       192.0.2.45
ubuntu        tty1         Thu Oct 22 08:16:02 2026   still logged in                       0.0.0.0
reboot        system boot  Thu Oct 22 08:15:18 2026   still running                         0.0.0.0

Without -w, alexander (9 characters) would have been truncated to alexande. -F reveals exact login/logout seconds, and -ai prints the full client IP at the end of each line.

3. Check System Reboot and Shutdown History (last reboot / -x)

Inspect when the server was rebooted or shut down, and identify whether a reboot was caused by an unexpected crash.

last -Fx reboot shutdown | head -n 6

# Illustrative output (reviewed against documentation)
reboot   system boot  6.8.0-45-generic Thu Oct 22 08:15:18 2026   still running
shutdown system down  6.8.0-45-generic Wed Oct 21 21:15:04 2026 - Thu Oct 22 08:15:18 2026  (11:00)
reboot   system boot  6.8.0-45-generic Wed Oct 21 09:00:11 2026 - Wed Oct 21 21:15:04 2026  (12:14)
reboot   system boot  6.8.0-44-generic Tue Oct 20 14:12:00 2026 - Wed Oct 21 09:00:11 2026  (18:48)

If a reboot line shows - crash instead of a matching shutdown timestamp, the machine lost power or experienced a kernel panic without running a clean shutdown sequence.

4. Filter Login History by Specific User and Time Window (-s and -t)

List all sessions for alex and priya since yesterday.

last -Fwa -s yesterday alex priya

# Illustrative output (reviewed against documentation)
alex     pts/2        Thu Oct 22 16:04:12 2026 - Thu Oct 22 16:40:05 2026  (00:35)     192.0.2.45
priya    pts/1        Thu Oct 22 15:10:44 2026   still logged in                       198.51.100.12
alex     pts/0        Thu Oct 22 14:22:09 2026   still logged in                       192.0.2.45
alex     pts/0        Wed Oct 21 19:10:00 2026 - Wed Oct 21 21:15:04 2026  (02:05)     192.0.2.45
priya    pts/1        Wed Oct 21 17:45:19 2026 - Wed Oct 21 18:30:22 2026  (00:45)     198.51.100.12

-s yesterday (or -s 2026-10-21) filters /var/log/wtmp by login timestamp before printing.

5. Determine Who Was Logged In at a Specific Point in Time (-p)

During post-mortem analysis of an outage that occurred at 2026-10-21 18:00:00, check which users had active sessions at that exact second.

last -Fwa -p "2026-10-21 18:00:00"

# Illustrative output (reviewed against documentation)
priya    pts/1        Wed Oct 21 17:45:19 2026 - Wed Oct 21 18:30:22 2026  (00:45)     198.51.100.12
reboot   system boot  Wed Oct 21 09:00:11 2026 - Wed Oct 21 21:15:04 2026  (12:14)     6.8.0-45-generic

Because priya logged in at 17:45 and logged out at 18:30, -p "2026-10-21 18:00:00" identifies her session as active at 18:00.

6. Inspect Failed Login Attempts with lastb

Read /var/log/btmp using sudo lastb (or sudo last -f /var/log/btmp) to audit failed SSH and console login attempts.

sudo lastb -Fwai -n 6

# Illustrative output (reviewed against documentation)
admin    ssh:notty    Thu Oct 22 16:12:04 2026 - Thu Oct 22 16:12:04 2026  (00:00)     203.0.113.88
root     ssh:notty    Thu Oct 22 16:11:58 2026 - Thu Oct 22 16:11:58 2026  (00:00)     203.0.113.88
root     ssh:notty    Thu Oct 22 16:11:52 2026 - Thu Oct 22 16:11:52 2026  (00:00)     203.0.113.88
ubuntu   ssh:notty    Thu Oct 22 15:40:10 2026 - Thu Oct 22 15:40:10 2026  (00:00)     198.51.100.99

btmp begins Thu Oct  1 00:00:01 2026

sudo lastb reveals repeated failed SSH login attempts (ssh:notty) from 203.0.113.88 targeting root and admin.

Real-World Workflows, Pipelines, and Shell Automation

Summarize Top Attacking IP Addresses from Failed Logins (lastb)

Aggregate /var/log/btmp with sudo lastb -ai, extract the final IP column with awk, and rank the top brute-force source IPs.

sudo lastb -ai | head -n -2 | awk '{print $NF}' | sort | uniq -c | sort -nr | head -n 10

# Illustrative output (reviewed against documentation)
142 203.0.113.88
     37 198.51.100.204
      9 192.0.2.199

Because -a puts the IP address in the last column ($NF), awk '{print $NF}' extracts the IP cleanly regardless of whether intermediate columns vary in width.

Query Historical Login Logs Across Rotated wtmp Archives

Because logrotate rotates /var/log/wtmp monthly, searching for a login from two months ago requires iterating over /var/log/wtmp* files.

for f in /var/log/wtmp*; do
  [ -f "$f" ] || continue
  echo "=== File: $f ==="
  last -Fwa -f "$f" alex | head -n 5
done

# Illustrative output (reviewed against documentation)
=== File: /var/log/wtmp ===
alex     pts/2        Thu Oct 22 16:04:12 2026 - Thu Oct 22 16:40:05 2026  (00:35)     192.0.2.45
alex     pts/0        Thu Oct 22 14:22:09 2026   still logged in                       192.0.2.45
=== File: /var/log/wtmp.1 ===
alex     pts/0        Tue Sep 29 11:05:40 2026 - Tue Sep 29 17:20:11 2026  (06:14)     192.0.2.45

Passing -f "$f" lets last inspect rotated /var/log/wtmp.1 archives seamlessly.

Pipelines, Redirection, Quoting, and Shell Expansion

When embedding last inside shell pipelines (|), conditional chains (&& and ||), or command substitutions ($(...)), the calling shell performs tilde expansion, variable interpolation, and globbing before invoking the binary. Always wrap paths, patterns, and variables in quotes so whitespace and wildcard characters (*, ?) are not expanded prematurely, and use set -euo pipefail in unattended scripts.

Permissions, Security Implications, and Safe Execution

Always keep /var/log/btmp restricted to 0600 (root:utmp or root:root). When users accidentally type their password into the username prompt at login, the mistyped password string is recorded as the 'username' in /var/log/btmp. That is why lastb requires sudo while last does not.

To complement last and lastb, use lastlog (which reads /var/log/lastlog to show only the single most recent login timestamp for every account in /etc/passwd, including accounts that have **Never logged in**) and inspect /var/log/auth.log or journalctl -u ssh for SSH key fingerprint attribution.

GNU/Linux, BSD, and BusyBox Implementation Differences

The last command is available on all Linux distributions (util-linux or wtmpdb), FreeBSD, OpenBSD, NetBSD, and macOS. However, flags like -F (--fulltimes), -w (--fullnames), -p (--present), and -s/-t are util-linux features and are not supported on macOS or BSD last.

On Alpine Linux, BusyBox's minimal last applet takes no advanced options; install util-linux-misc (apk add util-linux-misc) for full GNU/Linux last and lastb functionality.

Troubleshooting Common Errors and Diagnostics

last: /var/log/wtmp: No such file or directory

Root Cause: On minimal container images or fresh installations, /var/log/wtmp (and /var/log/btmp) may not have been created yet; login programs only append to wtmp if the file already exists.

Resolution: Create /var/log/wtmp and /var/log/btmp with the exact required ownership and permissions.

sudo install -m 0664 -o root -g utmp /dev/null /var/log/wtmp
sudo install -m 0600 -o root -g utmp /dev/null /var/log/btmp

lastb: /var/log/btmp: Permission denied

Root Cause: You ran lastb as a standard unprivileged user. /var/log/btmp is readable only by root to protect accidentally typed passwords.

Resolution: Prepend sudo (sudo lastb).

sudo lastb -Fwai -n 15

last truncates long usernames to 8 characters (e.g., 'alexande')

Root Cause: For backward compatibility with historical 80-column terminals, last truncates usernames to 8 characters unless -w is passed.

Resolution: Always add -w (--fullnames), e.g., last -Fwa.

last -Fwa -n 10

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
last reads /var/log/wtmp to show a chronological history of successful logins and system reboots. lastb reads /var/log/btmp to show failed login attempts. lastlog reads /var/log/lastlog to show only the single most recent login timestamp for every user account in /etc/passwd.
'- down' means the user session remained open until the server executed a normal, graceful shutdown or reboot. '- crash' means the system stopped abruptly (such as a power failure or kernel panic) without writing a clean shutdown record to /var/log/wtmp before the next boot.
In production, let logrotate rotate them automatically according to /etc/logrotate.conf. If /var/log/btmp grows huge after an SSH brute-force flood, truncate it in place without breaking permissions using sudo truncate -s 0 /var/log/btmp.
last reads /var/log/wtmp, which only records interactive login sessions that allocate a terminal (TTY/PTY). Non-interactive single-command SSH executions without a PTY are logged in /var/log/auth.log (or /var/log/secure on RHEL) rather than /var/log/wtmp.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout