If you run a public-facing Linux server, SSH is one of the first services attackers will probe. Automated bots try common usernames, weak passwords, and known credential lists around the clock, and even unsuccessful attempts fill your logs and waste resources. Setting up fail2ban ubuntu 24.04 is a practical first line of defence: it watches your authentication logs, detects repeated failures, and temporarily blocks the offending IP address before the attack can succeed.

Fail2ban does not replace good SSH hygiene. It works best alongside key-based authentication, a non-standard SSH port if you choose to use one, and a firewall that only exposes the services you actually need. Think of it as an automated bouncer: it does not make a weak password strong, but it stops an attacker from trying thousands of passwords against your server in a short period. When combined with the other hardening steps below, it dramatically reduces both noise and risk.
In this guide, you will install Fail2ban on Ubuntu 24.04, enable protection for SSH, tune the ban thresholds to suit your environment, test that bans are working, and learn how to recover if a legitimate address is blocked. You will also see how to make bans persistent for repeat offenders and how to integrate Fail2ban with UFW so blocked addresses are visible in your firewall rules.
Prerequisites
You need an Ubuntu 24.04 server with a user account that has sudo privileges and an SSH connection you can use to test changes. This fail2ban ubuntu 24.04 guide assumes you can run sudo commands and edit files with a text editor. You should also have a second way into the server, such as a cloud console, a VPN connection, or another trusted IP address, before you change any ban settings. This precaution matters because a misconfiguration could temporarily block your own address, and console access lets you recover without waiting for a ban to expire.
Before you start, make sure your firewall is already configured. If you have not set it up yet, follow the UFW firewall guide for Ubuntu 24.04 to allow SSH and other essential services first. You should also review the guide to disabling SSH password login on Ubuntu 24.04 once Fail2ban is working, because key-only authentication removes the main target of brute-force attacks entirely.
Installing fail2ban ubuntu 24.04
Fail2ban is available in the default Ubuntu repositories, so installation is straightforward. Update your package index and install the package with the following commands:
sudo apt update
sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban
sudo systemctl status fail2ban
If the service is running, the status output should show it as active. Once the fail2ban ubuntu 24.04 service is running, Fail2ban starts with a set of default jails, but on Ubuntu you should not edit the main configuration files directly. Files such as jail.conf are overwritten during package upgrades, so all of your custom settings belong in a separate file called jail.local, which takes precedence over the defaults.
Check that Fail2ban is running and list the active jails before you change anything:
sudo fail2ban-client status
sudo fail2ban-client ping
The first command shows the number of active jails, while the second confirms that the Fail2ban server process is responding. If either command reports an error, check the service status and the log file at /var/log/fail2ban.log before continuing.
Configuring the SSH Jail
A jail is a combination of a filter, which defines the log patterns to watch, and an action, which defines what happens when those patterns are matched too often. For SSH, Fail2ban uses the sshd filter. Create a jail.local file with a conservative default policy and an explicitly enabled SSH jail:
sudo nano /etc/fail2ban/jail.local
[DEFAULT]
ignoreip = 127.0.0.1/8 ::1
bantime = 1h
findtime = 10m
maxretry = 5
[sshd]
enabled = true
port = ssh
Save the file, then restart Fail2ban so the new configuration takes effect. Always validate the configuration after a restart by checking the jail status rather than assuming the change worked.
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd
If you run SSH on a custom port, change the port value in the sshd jail to match it, for example port = 2222. If you use a port range or multiple ports, list them separated by commas. A mismatch here is a common reason why Fail2ban appears to run but never bans anyone: it is watching the wrong service definition for your setup.
Tuning Bantime, Findtime, and Maxretry
The three settings in the example above control how strict your fail2ban ubuntu 24.04 setup is. Understanding them helps you balance security against the risk of blocking legitimate users who mistype a password a few times. The table below summarises what each setting does.
| Setting | Meaning | Example value |
| bantime | How long a banned IP stays blocked | 1h |
| findtime | The time window in which failures are counted | 10m |
| maxretry | How many failures trigger a ban within that window | 5 |
With the example values, an address that fails authentication five times within ten minutes is banned for one hour. That is a reasonable starting point for most servers. A lower maxretry value blocks attackers faster, but it also increases the chance that a legitimate user is banned after a few typos. A very long bantime is effective against persistent bots, but it can lock out an entire office or household that shares one public IP address.
For a server under heavy attack, you can increase bantime to 24h or use incremental bans, where repeat offenders are banned for progressively longer periods. Do not set bantime to a permanent value in the main SSH jail without a recovery plan, because a mistake in ignoreip could lock you out for a long time. Instead, use the recidive jail described below for persistent offenders, and keep the main SSH ban short enough to recover from.
Always add your own trusted addresses to ignoreip if you have a static office or home IP. Add one address per line entry, separated by spaces, and restart Fail2ban after changing the list. Never add a broad range you do not control, because any address in ignoreip is never banned, no matter how many failures it generates.
Testing fail2ban ubuntu 24.04 Protection
Do not test Fail2ban by deliberately locking out your main workstation. Use a test virtual machine, a second IP address, or a cloud instance you control, and attempt several failed SSH logins from that address. After the failures, check the jail status from your main session:
sudo fail2ban-client status sshd
sudo tail -n 50 /var/log/fail2ban.log
The status output shows the currently failed and total failed counts, plus a list of banned IP addresses. The log file shows each ban action with a timestamp, which is useful for confirming that the timing matches your findtime and maxretry settings. You can also verify that a banned address is actually blocked by trying to connect from it again; the connection should time out or be refused rather than reaching the password prompt.
If you want to check whether Fail2ban recognises your SSH failure patterns without generating real bans, use the fail2ban-regex tool against your authentication log and the sshd filter. This is a safe way to confirm that log formats are being parsed correctly, especially after an SSH configuration change.
sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf
A healthy result shows matched lines for failed password attempts. If nothing matches, the filter may not understand your log format, and you should investigate the log path and SSH logging configuration before relying on the jail.
Unbanning an IP Address
Legitimate users do get banned, so unbanning is a routine task in any fail2ban ubuntu 24.04 deployment, not an emergency procedure. To remove a single address from the SSH jail, run the following command, replacing the example address with the real one:
sudo fail2ban-client set sshd unbanip 203.0.113.10
After unbanning, confirm that the address no longer appears in the banned list and ask the user to try again. If the same address is banned repeatedly, check whether it belongs to a shared NAT gateway, a VPN exit node, or a misconfigured script that is retrying with an old password. Fixing the root cause is better than unbanning the same address every day.
If your own IP is banned and you have no second connection, wait for bantime to expire or use your hosting provider console to log in and unban yourself. This is why a short initial bantime is safer than a very long one while you are still tuning the configuration.
Persistent Bans with the Recidive Jail
Some attackers return after a short ban expires and start again, which is why a fail2ban ubuntu 24.04 setup benefits from a recidive jail. The recidive jail watches the Fail2ban log itself and bans repeat offenders for much longer. Add it to your jail.local file below the sshd jail:
[recidive]
enabled = true
logpath = /var/log/fail2ban.log
bantime = 1w
findtime = 1d
maxretry = 5
With this configuration, an address banned five times in one day is blocked for one week. Restart Fail2ban after adding the jail and check its status separately. Recidive bans are powerful, so keep monitoring the banned list during the first week to make sure no legitimate shared address is caught by the longer ban.
Integrating Fail2ban with UFW
By default, Fail2ban on Ubuntu manages its own firewall rules. If you prefer to see blocked addresses in UFW, set the ban action to ufw in your jail.local file. Add the following line to the DEFAULT section or to the sshd jail, then restart Fail2ban:
banaction = ufw
After the change, a banned address should appear when you check UFW status. This makes auditing easier, because your firewall output shows both your permanent rules and the temporary Fail2ban blocks in one place. If you change the ban action, test a ban again from a test address to confirm that the new action works, because a typo in the action name can silently prevent bans from being applied.
For more background on how Fail2ban actions work, including the differences between firewall backends, see the official Fail2ban documentation on GitHub. The upstream documentation is the best reference when you move beyond the basic SSH jail to custom filters or actions.
Troubleshooting
Jail not starting or sshd jail missing
If fail2ban-client status sshd reports that the jail does not exist, the jail is not enabled. Check that jail.local contains enabled = true under sshd, that the file has no syntax errors, and that you restarted the service after editing it. Run journalctl to see startup errors, which often point directly to a bad setting.
sudo journalctl -u fail2ban --no-pager -n 100
sudo fail2ban-client -d | head -n 100
Fail2ban runs but never bans anyone
This usually means Fail2ban is not seeing the failures. Confirm that SSH is logging failed attempts, that the log path matches your system, and that the sshd port in the jail matches your real SSH port. Use fail2ban-regex to test the filter against a log containing real failures, and check whether another service or a custom SSH configuration has changed the log format.
Your own IP is banned
Log in through the console or another trusted address, unban your IP, and add it to ignoreip before restarting Fail2ban. Also check whether your SSH client is retrying with an expired key or an old password, because automated retries can trigger a ban even when you are not typing anything wrong.
Bans disappear after a reboot
Standard Fail2ban bans are temporary by design and do not survive a restart unless you configure persistent storage or use the recidive jail for repeat offenders. If you need bans to survive reboots, consider a persistent ban database or an external blocklist, but weigh that against the risk of permanently blocking a reassigned dynamic address.
Conclusion
Fail2ban is a small service with an outsized effect on SSH security. With a short installation, a conservative jail.local file, and a few minutes of testing, a fail2ban ubuntu 24.04 installation can stop most brute-force noise before it reaches your login prompt. Keep your thresholds conservative at first, monitor the banned list and logs during the first week, and add trusted addresses to ignoreip. Once Fail2ban is stable, complete your hardening by moving to key-only authentication and keeping UFW rules tight. Together, those steps turn SSH from an easy target into a much harder one.