Overview
Tracking user access patterns stands as a fundamental pillar of Linux server administration and defense-in-depth security. In this hands-on tutorial, we explore practical techniques using the lastlog utility—from basic syntax and single-account inspection to hunting down inactive or abandoned accounts during routine security posture assessments.
Inspecting Global Access Records with lastlog
Executing lastlog queries the /var/log/lastlog database file to report the most recent access details for every configured account across the system, including timestamps, terminal devices (TTY/PTS), and originating IP addresses.
Because standard Linux installations register numerous service users, piping the output into head -n 15 keeps the initial display manageable by returning the first fifteen entries.
As revealed in typical baselines, non-interactive system daemon accounts show the **Never logged in** indicator by default.
⚙️ [Essential Command-Line Options]
-u (--user) : Restricts the output to a specified username or a numerical UID range.-b (--before-days) : Filters accounts whose most recent login predates a specified threshold in days.-t (--time) : Limits results to users who logged in within the past specified number of days.-C (--clear) : Erases the stored last-login record for a target user profile.-S (--set) : Overwrites or manually sets the target account’s last-login timestamp and metadata.

To isolate accounts with legitimate interactive histories from unauthenticated service accounts, pair the query with grep -v 'Never logged in'.
The -v invert-match argument suppresses lines matching the specified string, presenting only valid accounts with verifiable sign-in events.
On freshly provisioned or quiet systems, this command returns only the table header, confirming zero previous interactive logins.

Querying Specific User Activity (-u Flag)
Targeting specific credentials such as administrative accounts helps quickly verify access hygiene. Applying the -u flag against root isolates the superuser record.
In environments where direct administrative console logins are disabled or unused, the output reflects **Never logged in**.

To demonstrate user lifecycle monitoring, we verify the presence of a test profile and provision it alongside a dedicated home directory and standard shell by executing id -u testuser >/dev/null 2>&1 || useradd -m -s /bin/bash testuser.

Checking the state of the freshly registered profile using lastlog -u testuser demonstrates how newly minted credentials appear.
Because no interactive sessions have occurred since registration, the record naturally reports **Never logged in**.

To maintain system cleanliness after testing, remove the disposable account and purge its corresponding home folder structure via userdel -r testuser.

Identifying Active and Stale Accounts (-b, -t Flags)
To track recent authentication traffic, the -t argument filters for accounts that initiated sessions within a specific sliding window. Running lastlog -t 7 searches for logins recorded over the past seven days.
If no matching events occurred throughout this period, the terminal prints an empty set.

Conversely, locating abandoned or inactive credentials requires looking back across older timeframes. Executing lastlog -b 30 | head -n 15 identifies users that have remained idle for at least thirty days or have never logged in, using head -n 15 to limit the initial view.
Identifying these accounts allows administrators to audit dormant privileges and lock down unused attack vectors.

Log File Architecture and Complementary Audit Tools
Examining the underlying database provides insight into how authentication records persist. Running ls -lh /var/log/lastlog displays file metadata, confirming ownership by root and group permissions assigned to utmp.

To determine the binary format and storage nature of the record, issue file /var/log/lastlog.
In pristine installations prior to sparse indexing, an initial state of zero bytes displays simply as an empty file.

For real-time visibility into active sessions across the server, run who.
An empty return indicates that no active terminal sessions or concurrent user connections currently exist.

While lastlog reports strictly the single most recent access point for each account, last -n 5 queries /var/log/wtmp to show chronological historical connections alongside system reboot events.
Reviewing these recent boot intervals and session records offers broad situational context when cross-referencing anomalies flagged during security reviews.

Integrating lastlog into scheduled auditing procedures empowers administrators to spot anomalous sign-in behavior and decommission dormant access points before they become liabilities. Routine inspection of these audit trails is a simple yet vital habit for sustaining a hardened Linux environment.