Install and Configure Fail2ban to Protect SSH on Ubuntu

The moment an Ubuntu machine is reachable from the internet, it becomes interesting to strangers. Within minutes of an SSH port being exposed, automated scanners begin knocking on it, trying password after password against names like ubuntu, admin, root and git. Fail2ban is the quiet sentry that answers this pattern. It reads your system's authentication log, counts failures coming from a single source address, and drops a temporary firewall ban on that address once a threshold is crossed. It is a small package with a large practical effect, and it is the first thing I install on any server I build.

This walkthrough covers how to install and configure Fail2ban on Ubuntu to protect SSH, from a fresh cloud instance to a home lab sitting on the NBN. It assumes Ubuntu Server 22.04 or 24.04 LTS and a reasonable amount of comfort with a terminal, though every command is spelled out. The advice is shaped by Australian conditions: residential connections through Telstra, Optus and TPG, rented VPS instances in a Sydney or Melbourne region, and small businesses that want sensible defaults without a full-time security engineer.

Why SSH attacks keep hitting Australian servers

Nearly every automated intrusion you read about in the news starts with SSH, because it is publicly documented, widely deployed and often left switched on with password authentication enabled. The traffic is noisy but easy to summarise. A server in Brisbane that does nothing more interesting than serve a small website will still collect thousands of login attempts a week, and the same pattern shows up in the logs on a lazy Sunday arvo while you are meant to be relaxing.

Local infrastructure plays a part in how common this is. Most Australian homes and small offices now reach the internet over NBN rather than ADSL or cable, and those connections commonly hand out a dynamic residential address with a sticky lease that can last anywhere from a day to a week. Attackers exploit that constantly, and some of them buy time on proxy networks built for exactly this purpose. Rural properties on fixed wireless or satellite links are in the same boat, and so are the small businesses that rent a VPS in an Australian region. Renting a two-dollar-a-month instance billed in AUD is cheap enough that plenty of hobbyists in Adelaide and Perth now expose a public SSH port from their spare bedroom.

What Fail2ban does is react rather than prevent. It pattern-matches failure messages in the log, then inserts a timed block using the system's firewall rules, whether that is nftables or the older iptables backend. It cannot stop a determined attacker with a clean residential address, and it will not help if your server is already compromised, so it pairs best with key-based logins, timely patching and sensible firewall defaults. What it does give you is a short answer to the noisy knocking, and most of that noise stops quickly.

Preparing the server before Fail2ban goes in

Start with patching, because a ban list built on a vulnerable host is security theatre. The Australian Signals Directorate and the Australian Cyber Security Centre are unapologetic about patching early and often, and the same advice applies whether you run a server for a shop in Geelong or a mate's blog. Ubuntu Server also supports automatic security updates, which take the worry out of remembering to log in and run upgrades after a security notice appears.

sudo apt update
sudo apt upgrade -y
sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades

Next, make SSH itself harder to attack. Generate a key pair on your workstation, copy the public key across with ssh-copy-id, then disable password authentication and direct root login once you have confirmed the key works. Keep a second terminal open while you do this, because a locked-out server in an Australian data centre usually means a support ticket and a wait.

sudoedit /etc/ssh/sshd_config
# PasswordAuthentication no
# PermitRootLogin no
sudo sshd -t
sudo systemctl reload ssh

Finally, learn your public address and note who supplies it. Run curl -4 ifconfig.me and curl -6 ifconfig.me to see both, then write down the provider. If your address changes after a reconnect, any ignore rule you add later will be out of date. If you run a firewall, allow SSH first so Fail2ban's bans never find you locked out of your own rules.

Installing Fail2ban and confirming it is running

Installation is a single package, and the Ubuntu archive keeps the version current enough that the nftables backend works out of the box on 24.04 LTS.

sudo apt install fail2ban
sudo systemctl enable --now fail2ban
sudo systemctl status fail2ban

The service starts quietly, which is the point. Fail2ban is configured around the idea of a jail: a defined log source, a filter that decides what counts as abuse, and an action that applies a ban. Configuration lives in /etc/fail2ban/, split into jail.conf for defaults, jail.d/ for drop-in files, filter.d/ for the patterns that detect failures, and action.d/ for what happens when a ban triggers.

Do not edit jail.conf directly. The package overwrites it on upgrade and your changes vanish without warning. Put your own settings in /etc/fail2ban/jail.local or a new file under jail.d/, both of which survive updates. Ubuntu also ships a small defaults file that enables the ssh jail out of the box, so protection frequently starts the moment the service is running, before you have changed a single value.

Check that the log source exists. Some Ubuntu installations keep the traditional rsyslog file, while minimal cloud images log only to the systemd journal.

ls -l /var/log/auth.log
sudo journalctl -u ssh --no-pager | tail -n 20

If /var/log/auth.log is missing, your jail will need backend = systemd instead of a log path.

Tuning a jail that blocks repeat attackers

Here is a sensible baseline for /etc/fail2ban/jail.local. It tightens the shipped defaults, keeps the response proportionate, and leaves room for your own address.

[sshd]
enabled  = true
port     = ssh
filter   = sshd
backend  = systemd
maxretry = 5
findtime = 10m
bantime  = 12h
ignoreip = 127.0.0.1/8 ::1 192.168.0.0/16 10.0.0.0/8 203.0.113.45

The three timing values are worth understanding. maxretry is the number of failures allowed inside the window, findtime is that window, and bantime is how long the ban lasts once the limit is hit. Ten minutes is the shipped default ban, which clears the ordinary background noise but lets a patient attacker keep trying later. Twelve hours handles most home and small business situations without permanently blocking a stray visitor who mistyped a password in a café.

Log timestamps follow your system clock, so on Australian servers that means AEST or AEDT depending on the season. findtime measures elapsed time internally, so daylight saving changes do not break the logic, but if you are comparing Fail2ban output against a provider's UTC logs, the offset will make you scratch your head.

Your own public address belongs in ignoreip, along with any private ranges. This matters more here than almost anywhere else, because a dynamic NBN address can change overnight and a stale entry either bans you when you return home or fails to protect you at all. If you run a VPS with a static address, note that address once and forget about it. If you are on a dynamic residential connection, re-check the value after every reconnection. There is no need to be clever here, just accurate.

Reading logs and fixing a filter that misses

The default ssh filter recognises the common failure lines, including invalid user attempts, repeated password failures and failed public key attempts. Watch the live feed while you try a deliberately wrong password from another machine, and confirm the entries look like what the filter expects.

sudo journalctl -u ssh -f
sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf

If the filter reports zero matches while your log is full of failures, the backend and log path are the usual culprits, followed by a setup where the messages are worded differently, such as a hardened SSH daemon with a custom LogLevel. Adjust the pattern in a copy under jail.d/ rather than the packaged filter file.

Sometimes the problem is not the server at all. If your connection to the box suddenly refuses every attempt, it is worth ruling out the local network before you start rewriting jail settings, and a guide to command-line WiFi troubleshooting covers that side with nmcli, ip and DNS checks. A quiet local fault and an active ban can look identical from the other end of the connection.

Confirm a ban landed by looking at the jail's table, then check the firewall rules that were inserted to enforce it.

sudo fail2ban-client status sshd
sudo nft list ruleset | grep -i f2b

Checking bans and keeping the habit going

The command to remember is fail2ban-client status sshd, which lists the jail, the number of currently banned addresses and the total tally since the service started. Follow the service log for ban and unban events, and test the whole loop deliberately from a phone on 5G, which is a convenient way to generate failures from an address that is not your own.

sudo tail -f /var/log/fail2ban.log
sudo fail2ban-client set sshd unbanip 203.0.113.90

If you host anything for other people, think about log retention. Under the Privacy Act 1988 and the Australian Privacy Principles, an IP address can be personal information in the right context, so a Melbourne consultancy running a client portal should keep its ban logs for a defined period rather than indefinitely, and document that decision. Trimming the journal and Fail2ban logs on a schedule keeps the server tidy and the obligation simple.

If you keep one habit from this guide, make it this: confirm the sshd jail is active, make sure ignoreip holds your current public address, and glance at the ban list once a week. That short routine is what turns an installed package into protection that actually works, and it costs about as much effort as making a coffee.