Count Failed sshd Logins per IP Address
Count sshd "Failed password" lines per IP. Why the popular $11 one-liner breaks on invalid-user lines, the $(NF-3) fix, and the same count from journalctl.
The common one-liner for counting Failed password assumes the IP is the 11th field. That breaks for login attempts with a non-existent account. The output below comes from a sample log file; the addresses are documentation ranges (203.0.113.x and so on).
[1] Find where the log lives. Ubuntu has both /var/log/auth.log and the journal (ssh unit); RHEL-family systems use /var/log/secure and the sshd unit.
# systemctl list-unit-files 'ssh*'UNIT FILE STATE PRESETssh.service enabled enabledsshd.service alias -ssh.socket disabled enabled[2] Create a test log of 13 lines in the real format.
# cat /srv/lab/auth.logOct 19 09:12:01 server1 sshd[2101]: Failed password for root from 203.0.113.5 port 51122 ssh2Oct 19 09:12:04 server1 sshd[2101]: Failed password for root from 203.0.113.5 port 51122 ssh2Oct 19 09:12:09 server1 sshd[2104]: Failed password for invalid user admin from 203.0.113.5 port 51130 ssh2Oct 19 09:13:15 server1 sshd[2110]: Failed password for invalid user test from 198.51.100.7 port 40022 ssh2Oct 19 09:13:19 server1 sshd[2110]: Failed password for invalid user test from 198.51.100.7 port 40022 ssh2Oct 19 09:13:25 server1 sshd[2113]: Failed password for invalid user oracle from 198.51.100.7 port 40030 ssh2Oct 19 09:14:02 server1 sshd[2120]: Accepted password for deploy from 192.0.2.50 port 60110 ssh2Oct 19 09:14:40 server1 sshd[2125]: Failed password for deploy from 192.0.2.50 port 60120 ssh2Oct 19 09:15:11 server1 sshd[2130]: Failed password for root from 203.0.113.9 port 33001 ssh2Oct 19 09:15:14 server1 sshd[2130]: Failed password for root from 203.0.113.9 port 33001 ssh2Oct 19 09:15:18 server1 sshd[2130]: Failed password for root from 203.0.113.9 port 33001 ssh2Oct 19 09:16:00 server1 sshd[2140]: Invalid user guest from 203.0.113.5 port 51140Oct 19 09:16:02 server1 sshd[2140]: Connection closed by invalid user guest 203.0.113.5 port 51140 [preauth][3] Run the common version first. It counts the 11th whitespace-separated field.
# grep "Failed password" /srv/lab/auth.log | awk '{print $11}' | sort | uniq -c | sort -rn 3 203.0.113.9 2 test 2 203.0.113.5 1 oracle 1 admin 1 192.0.2.50test, oracle and admin show up where IPs should be. With a valid account the line reads Failed password for root from <IP>, so the IP is field 11. With an unknown one it reads for invalid user admin from <IP>, two words longer, which pushes the IP to field 13 and leaves the user name in field 11. 203.0.113.5 failed 3 times but is reported as 2.
[4] Count from the end instead of the start. These lines all end with from <IP> port <port> ssh2, so the IP is the fourth field from the end: $(NF-3).
# grep "Failed password" /srv/lab/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn 3 203.0.113.9 3 203.0.113.5 3 198.51.100.7 1 192.0.2.50203.0.113.5 is now correct: 3 (root twice, admin once). The result was identical with GNU Awk 5.2.1 and mawk 1.3.4.
[5] Run the same count on the journal by piping journalctl into the same pipeline. The message part is identical, so $(NF-3) still fits.
# journalctl -u ssh --no-pager | grep "Failed password" | awk '{print $(NF-3)}' | sort | uniq -c | sort -rnYou can also filter inside the journal with -g. Add -q to drop the -- Boot … -- separator lines, and -o cat if you do not need the timestamp and host prefix.
# journalctl -u ssh -q --no-pager -g 'Failed password' -o cat | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn$(NF-3) points at the same field with -o cat or -o short-iso.
[6] Add --since to narrow the period, and head for the top 10.
# journalctl -u ssh -q --no-pager --since "1 hour ago" | grep "Failed password" | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn | head -10Analyze your syslog · journal log in the browser ↗ — nothing is uploaded.