Operating System Ubuntu

How to Set Up Fail2ban on Ubuntu 24.04 LTS

Install Fail2ban on Ubuntu 24.04, tune the SSH jail in jail.local, and learn the commands to check, ban and unban IP addresses.

Fail2ban watches your server's logs for repeated authentication failures and blocks the offending IP addresses in the firewall for a set time. This guide installs it on Ubuntu 24.04 LTS, configures the SSH jail with sensible limits, shows how to check and lift bans, and adds optional jails for web servers. The same configuration works on Ubuntu 22.04, and the one extra setting Debian 12 needs is called out.

Ubuntu 18.04, covered by older tutorials, no longer receives standard updates. Two things have changed since then: SSH failures are now read from the systemd journal rather than /var/log/auth.log, and copying jail.conf to jail.local is no longer the recommended way to configure Fail2ban.

Requirements

  • A server running Ubuntu 24.04 LTS with all updates applied (sudo apt update && sudo apt upgrade). The original 24.04 package did not start under Python 3.12; the fix arrived as a regular update.
  • A user with sudo rights.
  • The public IP address you connect from, if it is static, so you can exempt it. The examples use 203.0.113.10.

Step 1: Install Fail2ban

sudo apt update
sudo apt install -y fail2ban

Make sure the service is enabled at boot and running:

sudo systemctl enable --now fail2ban
sudo systemctl status fail2ban

The Ubuntu package already protects SSH: it ships /etc/fail2ban/jail.d/defaults-debian.conf, which enables the sshd jail, reads from the systemd journal and bans through nftables. Confirm that the jail is active:

sudo fail2ban-client status

The output should list sshd under "Jail list".

Step 2: Understand where configuration goes

Fail2ban reads its jail configuration in this order, and later files override earlier ones:

  1. /etc/fail2ban/jail.conf – the defaults shipped with the package
  2. /etc/fail2ban/jail.d/*.conf
  3. /etc/fail2ban/jail.local
  4. /etc/fail2ban/jail.d/*.local

Never edit jail.conf; package updates replace it. Do not copy it to jail.local either, as older tutorials suggest: a full copy freezes every default at today's values and hides improvements in later package versions. Put only the settings you want to change into jail.local.

Step 3: Create jail.local

sudo nano /etc/fail2ban/jail.local

Add the following, replacing 203.0.113.10 with your own address or removing it:

[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 203.0.113.10
bantime  = 1h
findtime = 10m
maxretry = 5

[sshd]
enabled = true

What the options mean:

  • ignoreip – addresses and networks that are never banned, separated by spaces. Add your own static address so a mistyped password cannot lock you out.
  • bantime – how long an address stays blocked. The package default is 10 minutes.
  • findtime – the window in which failures are counted.
  • maxretry – the number of failures within findtime that triggers a ban.

Times accept suffixes such as m, h, d and w; a bare number is seconds. With the values above, five failed logins within ten minutes block an address for one hour. Settings in [DEFAULT] apply to every jail unless a jail section overrides them.

If your SSH daemon listens on a non-standard port, tell the jail by adding a port line to the [sshd] section, for example port = 2222.

Debian 12: set the journal backend

Debian 12 has no /var/log/auth.log by default, and its Fail2ban package does not select the journal automatically, so the service fails to start with an error about a missing log file for the sshd jail. Install the journal bindings and add one line to the [sshd] section:

sudo apt install -y python3-systemd
[sshd]
enabled = true
backend = systemd

Ubuntu 24.04 and Debian 13 ship this setting already.

Step 4: Apply and verify

Test the configuration, then restart the service:

sudo fail2ban-client -t
sudo systemctl restart fail2ban

Check the SSH jail:

sudo fail2ban-client status sshd

The output shows the number of currently failed attempts, the total number of bans and the list of banned addresses. Confirm that your values were picked up:

sudo fail2ban-client get sshd bantime
sudo fail2ban-client get sshd maxretry

On an internet-facing server you will normally see the first bans within hours. Fail2ban records every action in /var/log/fail2ban.log:

sudo tail -n 50 /var/log/fail2ban.log

Step 5: Ban and unban addresses manually

To release an address from one jail, or from all jails at once:

sudo fail2ban-client set sshd unbanip 198.51.100.23
sudo fail2ban-client unban 198.51.100.23

To block an address yourself for the jail's ban time:

sudo fail2ban-client set sshd banip 198.51.100.23

If you lock yourself out, log in through your provider's web console and run the unban command there, then add your address to ignoreip.

Step 6: Enable more jails (optional)

jail.conf contains ready-made definitions for many services, all disabled. Enable one by adding its section name with enabled = true to jail.local. Only enable jails for software that is installed, because a jail whose log file does not exist stops Fail2ban from starting. Two useful examples:

[nginx-http-auth]
enabled = true

[recidive]
enabled = true

nginx-http-auth bans clients that repeatedly fail HTTP basic authentication on Nginx (the Apache equivalent is apache-auth). recidive reads Fail2ban's own log and blocks addresses that keep getting banned, for one week and on all ports by default. Run sudo fail2ban-client -t and restart the service after each change.

Troubleshooting

  • Service will not start: run sudo fail2ban-client -t and sudo journalctl -u fail2ban -n 50. The usual causes are a typo in jail.local or an enabled jail pointing at a log file that does not exist.
  • Which settings are actually in effect? sudo fail2ban-client -d dumps the merged configuration from all files.
  • No bans appear: check that failures are being logged at all with sudo journalctl -u ssh -n 20, and that the address is not covered by ignoreip.

Reference: the jail.conf manual page and the upstream Fail2ban configuration guide.

Frequently asked questions

Does Fail2ban replace SSH keys or a firewall?

No. It reduces log noise and slows down password guessing, but an attacker with many addresses can stay under the limits. Use key-based SSH logins with password authentication disabled, keep a firewall that only opens the ports you need, and treat Fail2ban as an additional layer.

Does Fail2ban work together with UFW?

Yes. With the default nftables action, Fail2ban maintains its own set of blocked addresses independently of UFW's rules, so you do not have to change your UFW configuration. Banned addresses will not show up in ufw status; use fail2ban-client status sshd to see them.

Do bans survive a restart?

Fail2ban keeps active bans in a small database and restores those that have not yet expired when the service starts again, so restarting it after a configuration change does not release blocked addresses. Use sudo fail2ban-client unban --all if you want to clear everything.

Can repeat offenders be banned for longer each time?

Yes. Add bantime.increment = true to the [DEFAULT] section. Fail2ban then looks up an address's previous bans and lengthens each new one. The recidive jail achieves a similar effect with a fixed long ban.

Share story:

Continue reading other guides on How VPS.