Linux last Command Guide: Audit Ubuntu Server Logins

Overview

Maintaining an accurate record of who accessed your Ubuntu server, when they connected, and their remote endpoints forms the backbone of infrastructure security and audit compliance. This guide demonstrates how systems administrators can leverage the native last command to track user sessions, investigate reboot events, and accurately interpret connection accounting data.

Reading wtmp Logs with the Default last Command

To verify the permissions and presence of the underlying binary database that tracks session accounting, run ls -lh /var/log/wtmp.

Checking wtmp log file attributes

The last utility parses this binary file to render historic login sessions and machine boot cycles in reverse chronological order.
Supplying the -n 10 parameter restricts the terminal view to only the 10 most recent entries.
⚙️ [Essential Options]

-n (or -<number>) : Limits output to the designated number of recent entries.
-f : Directs the command to parse a custom log file instead of the default /var/log/wtmp.
-t : Displays historical login states up to a specified snapshot time (YYYYMMDDHHMMSS).
-s : Constrains results to sessions established after a given point in time.
-i : Prints the remote client’s numeric IP address instead of resolving its hostname.
-F : Outputs full, high-precision timestamps including calendar dates and seconds.
-x : Incorporates system shutdowns and runlevel changes directly into the session timeline.

Filtering recent login entries using last -n 10

Displaying Numeric IP Addresses and Optimizing Output Layout

Invoking last alongside the -i flag forces the tool to bypass reverse DNS lookup and present the caller’s raw IP address.
Combining this with -n 5 produces a clean overview of the five latest logins along with their originating network addresses.

Exposing client IP addresses with last -n 5 -i

Passing the -a argument moves the host identification or IP address field directly to the final column on the right.
This structural realignment prevents column misalignment triggered by long fully qualified domain names (FQDNs) or kernel release strings, significantly improving screen readability.

Shifting hostname column to the far right with last -n 5 -a

Tracking Detailed Timestamps and Isolating Specific Events

When forensic investigations demand exact timing, appending the -F flag instructs last to print full year markers and second-level precision for login and logout events.
Running it with -n 5 enables administrators to pinpoint exact uptime windows down to the exact second.

Displaying full timestamps down to the second with last -n 5 -F

Executing last reboot strips standard user sessions from the report, highlighting strictly operating system boot cycles.
Using -n 3 isolates the three most recent restart sequences alongside their active kernel versions.

Auditing recent system reboots with last reboot -n 3

To audit sessions belonging exclusively to your active shell account, execute last $(whoami) -n 5.
Leveraging command substitution through $(whoami) provides an effortless way to track targeted usernames without manually typing out account identifiers.

Auditing active user sessions with last $(whoami) -n 5

Monitoring Failed Authentication via lastb and Setting Time Windows

To ensure that rejected login attempts are properly captured on disk, initialize the log file and apply restricted permissions via sudo touch /var/log/btmp && sudo chmod 660 /var/log/btmp.

Initializing /var/log/btmp with secure permissions

The sudo lastb command queries /var/log/btmp to display all recorded authentication failures.
Because this log reveals sensitive security events, root privileges via sudo are mandatory; restricting output with -n 5 gives administrators immediate visibility into brute-force attempts and anomalous connection activity.

Inspecting failed login events with sudo lastb -n 5

Applying -s -1days instructs last to evaluate connection activity strictly within the past 24 hours.
Combining this timeframe filter with -n 5 provides a quick health check of recent access attempts and system boot stability over the previous day.

Filtering log records within the past 24 hours using last -s -1days -n 5

Through routine inspection of Ubuntu authentication logs with the last utility, administrators can gain comprehensive visibility into server activity. Incorporating these auditing commands into regular maintenance workflows ensures unauthorized access patterns are caught and addressed early.