Knowledge

Blocking SSH password guessing with fail2ban and reading login logs on Ubuntu 24.04

Updated 2026-11-26 · Present

  • fail2ban watches the ssh logs and temporarily blocks addresses that guess wrong many times, using the firewall.
  • Read login logs with journalctl -u ssh, and view history with last to know who has logged in.
  • fail2ban is an extra layer; it does not replace SSH keys and turning passwords off.

Goal

A server with an SSH port open to the Internet receives a great many failed logins from automated probes. Once you use keys and have turned passwords off, these attempts cannot get in, but they still use resources and fill the logs with noise. This article shows how to install fail2ban (version 1.0.2 in the Ubuntu 24.04 repository) to block those addresses temporarily, and how to read login logs to spot anything unusual.

Preparation

Steps

Steps

Step 1: Install fail2ban

sudo apt update
sudo apt install fail2ban

Step 2: Create your own configuration file

Do not edit jail.conf, because it can be overwritten when the package is upgraded. Create /etc/fail2ban/jail.local:

[DEFAULT]
# Addresses that are never blocked (your machine, the office network)
ignoreip = 127.0.0.1/8 ::1 192.0.2.0/24

# Ban time, counting window and number of failures allowed
bantime  = 1h
findtime = 10m
maxretry = 5

[sshd]
enabled = true
backend = systemd
# If you changed the SSH port, remove the # and edit the number:
# port = 2222

Meaning: if an address fails to log in more than maxretry times within findtime, it is blocked for bantime. backend = systemd tells fail2ban to read the logs from the journal, which suits Ubuntu 24.04 because a minimal machine may not have the file /var/log/auth.log.

Step 3: Enable and start the service

sudo systemctl enable --now fail2ban

Checking

View the status of the sshd jail:

sudo fail2ban-client status sshd

The output shows the number of failures counted, the number of addresses currently blocked and their list. If you want to unblock an address (for example, you mistyped many times yourself):

sudo fail2ban-client set sshd unbanip 198.51.100.7

Reading SSH login logs

On Ubuntu the SSH service is called ssh, so its logs are read with journalctl (filtering by service and time is explained in detail in journalctl: filtering logs by service and by time on Ubuntu 24.04):

# SSH logs from the past hour
sudo journalctl -u ssh --since "1 hour ago"

# Only the failed login lines
sudo journalctl -u ssh --since today -g 'Failed password|Invalid user'

Lines worth noticing:

View the history of finished logins:

last -n 10
sudo lastb -n 10

last lists recent successful logins. lastb lists failed ones; it reads /var/log/btmp and may be empty if the system does not write to that file. If you see a successful login you do not recognise, treat it as an incident: change your keys and check ~/.ssh/authorized_keys for any strange line.

Common errors

Note: this article gives only a minimal configuration. Read the fail2ban documentation before using advanced blocking actions.

Summary

fail2ban reads the SSH logs and temporarily blocks addresses that fail many times; journalctl -u ssh, last and lastb help you check who has tried and who has got in. Together with SSH keys, a tight sshd_config and regular log reading, you get a layered defence for the SSH port.

References