Monitor Login Activity on Linux: 5 Costly Mistakes to Avoid
To monitor login activity on a Linux server you used to need three commands: last, lastb and lastlog. On Debian 13 "trixie" two of them are gone and the third works differently. I built a Debian 13 server container with OpenSSH 10.0p1, wtmpdb 0.73.0 and lastlog2 2.41.5, logged in to it from a second container with good and bad passwords, and then checked what each tool recorded. The short version: the systemd journal saw everything, lastlog2 saw every successful login, and last saw only the session that had a terminal.

Contents
Why the old commands changed
The Debian 13 release notes have a section titled "The last, lastb and lastlog commands have been replaced". util-linux no longer ships last or lastb, and the login package no longer ships lastlog. The reason is the year 2038: the old files /var/log/wtmp, /var/log/btmp and /var/log/lastlog "do not allocate enough space to store the login time". The replacements are wtmpdb (with libpam-wtmpdb) for last and lastlog2 (with libpam-lastlog2) for lastlog. For lastb, the notes point to lslogins --failed.
If you monitor login activity with scripts, check what your distribution ships before you copy a command from an old guide. On the Debian 13 test server, the old commands gave this:
srv$ lastlog
bash: line 1: lastlog: command not found
srv$ lastb
bash: line 1: lastb: command not found
There was also no /var/log/auth.log and no /var/log/secure, so the usual grep sshd /var/log/auth.log found nothing to read. On Debian 13 the authentication messages live in the systemd journal unless you install a syslog daemon.
The test: six SSH login attempts
To see how each tool can monitor login activity, I ran these attempts from a client at 10.89.1.3, in order: a successful login for alice that ran one command, three wrong passwords for bob, one attempt as the non-existent user admin, a successful bob login that ran sleep 30, and finally a successful alice login with a terminal (ssh -tt, which printed /dev/pts/0). That is a realistic mix: scripts and automation log in without a terminal, people log in with one.
Monitor login activity with the journal
The best place to monitor login activity on Debian 13 is the journal. It recorded every attempt, successful or not, with the source address and port:
srv$ journalctl -u ssh --no-pager -o short-iso | grep -E 'Accepted|Failed|Invalid|Connection closed|Disconnected'
2026-11-17T15:05:05+00:00 srv sshd-session[62]: Accepted password for alice from 10.89.1.3 port 48200 ssh2
2026-11-17T15:05:09+00:00 srv sshd-session[87]: Failed password for bob from 10.89.1.3 port 41984 ssh2
2026-11-17T15:05:10+00:00 srv sshd-session[87]: Connection closed by authenticating user bob 10.89.1.3 port 41984 [preauth]
2026-11-17T15:05:12+00:00 srv sshd-session[90]: Failed password for bob from 10.89.1.3 port 42000 ssh2
2026-11-17T15:05:12+00:00 srv sshd-session[90]: Connection closed by authenticating user bob 10.89.1.3 port 42000 [preauth]
2026-11-17T15:05:14+00:00 srv sshd-session[93]: Failed password for bob from 10.89.1.3 port 42002 ssh2
2026-11-17T15:05:14+00:00 srv sshd-session[93]: Connection closed by authenticating user bob 10.89.1.3 port 42002 [preauth]
2026-11-17T15:05:15+00:00 srv sshd-session[96]: Invalid user admin from 10.89.1.3 port 42006
2026-11-17T15:05:17+00:00 srv sshd-session[96]: Failed password for invalid user admin from 10.89.1.3 port 42006 ssh2
2026-11-17T15:05:19+00:00 srv sshd-session[96]: Connection closed by invalid user admin 10.89.1.3 port 42006 [preauth]
2026-11-17T15:05:19+00:00 srv sshd-session[104]: Accepted password for bob from 10.89.1.3 port 58142 ssh2
2026-11-17T15:05:22+00:00 srv sshd-session[127]: Accepted password for alice from 10.89.1.3 port 58156 ssh2
Note the process name: in OpenSSH 10.0 the per-connection work is done by sshd-session, not sshd, so a log filter written for the program name sshd needs updating. Filtering by the systemd unit with -u ssh caught every line. Counting failures is one grep (journalctl -u ssh --no-pager | grep -c 'Failed password' printed 4), and "Invalid user" lines show which account names a scanner is guessing. The journalctl manual covers --since, -o json and the other output formats for feeding these lines into a script or SIEM.
Why last cannot monitor login activity alone on Debian 13
last still exists when wtmpdb is installed, but it is now a link to the wtmpdb program (dpkg -S reported wtmpdb: /usr/bin/wtmpdb). After all six attempts it showed one login:
srv$ last
alice pts/0 10.89.1.3 Tue Nov 17 15:05 - 15:05 (00:00)
reboot system boot 7.3.3-200.fc44.x Tue Nov 17 12:14 - still running
wtmpdb begins Tue Nov 17 12:14:34 2026
Only the ssh -tt session with pts/0 is there. Bob's 30-second session and alice's first command-only login are missing, and last bob printed nothing but the header line. (The reboot line shows the host's kernel because the server was a container.)
The PAM configuration explains part of it: /etc/pam.d/common-session has pam_wtmpdb.so skip_if=sshd. The wtmpdb README.Debian says that sshd "can record richer login information directly with libwtmpdb0", so the PAM module skips sshd "to avoid duplicate login entries". In this lab the sshd records appeared only for the session with a terminal. So if you monitor login activity with last alone, automated logins that just run a command, such as backups, CI jobs or an attacker's one-liner, can be invisible.
lastlog2: the last login of every account
lastlog2 replaces lastlog and keeps its data in an SQLite file, /var/lib/lastlog/lastlog2.db. It recorded both users, including the logins without a terminal:
srv$ lastlog2
Username Port From Latest
alice ssh 10.89.1.3 Tue Nov 17 15:05:22 +0000 2026
bob ssh 10.89.1.3 Tue Nov 17 15:05:19 +0000 2026
When you monitor login activity for service accounts, lastlog2 is the quick answer to "has this service account ever been used, and from where?". It shows only the latest login per user, not the history, and it does not show failures. See the lastlog2 manual for its options.
Who is logged in right now
To monitor login activity live, while bob's sleep 30 was running, who listed him:
srv$ who
bob sshd Nov 17 15:05 (10.89.1.3)
loginctl list-sessions showed the systemd-logind view, with bob's sessions and alice's. Use either for a live check, and the journal for history.
A routine to monitor login activity
- Daily: count
Failed passwordandInvalid userlines injournalctl -u ssh --since yesterday, grouped by source address. - Weekly: run
lastlog2and look for accounts that should never log in, such as service users. - For interactive history,
last(wtmpdb) is fine, but remember it may not show command-only SSH sessions. - Keep the journal persistent and long enough for your retention needs, or ship it to a central log server.
- Block repeated failures automatically with fail2ban; the fail2ban setup for a Zimbra server shows the jail and filter logic.
Monitor login activity together with hardening: key-only logins and a firewall cut the noise in these logs a lot. The guide to enabling SSH on Ubuntu covers key authentication and rate limiting with ufw.
Comments
Post a Comment