How to Install fail2ban on Ubuntu 24.04 for SSH Protection

Serdar Tekin
Serdar Tekin
Co-Founder & CTO
BeginnerUpdated Nov 17, 202616 min read~30 minutes total
fail2banUbuntu 24.04SSH SecurityServer HardeningLinuxnftables
Last reviewed:
Tutorial
How to Install fail2ban on Ubuntu 24.04 for SSH Protection
rafftechnologies.com/learn
On this page

Prerequisites

  • A Raff Linux VM running Ubuntu 24.04, SSH access with a non-root sudo user, a confirmed second SSH session, and preferably SSH key authentication already configured

Block SSH brute-force attempts on your own Ubuntu 24.04 server.

Tested on 1 vCPU and 2 GB RAM: $8.99 a month

  1. 1Pick a Linux imageUbuntu, Debian and more, with your SSH key added.
  2. 2Choose a sizeFrom $4.99 a month. Resize when your app grows.
  3. 3Connect over SSHYour server is up in about 55 seconds.

Ubuntu, Debian and other Linux distributions with full root and SSH access, up and running in about 55 seconds.

fail2ban monitors authentication events and temporarily blocks source IP addresses that repeatedly match failure patterns. On an internet-facing Ubuntu server, it is useful as an additional SSH protection layer alongside SSH keys, restricted firewall rules, timely updates, and disabled password authentication where practical. In this tutorial, you will install fail2ban on a Raff Linux VM running Ubuntu 24.04, enable the SSH jail, use the systemd journal backend, tune retry and ban windows, validate the configuration, test a ban without locking yourself out, inspect logs, and cleanly roll the setup back if needed.

Ubuntu 24.04 currently provides fail2ban 1.0.2-3ubuntu0.1 in the Noble Updates package line. The package supports systemd journal monitoring and works with modern firewall backends including nftables. This guide does not claim that every public server receives a fixed number of attacks or that fail2ban eliminates brute-force traffic; actual login noise varies by IP, exposure, SSH configuration, and time. fail2ban reduces repeated attempts from matching sources, but it should not replace SSH key authentication or firewall access controls.

You need Ubuntu 24.04, SSH access using a non-root sudo user, and preferably SSH key authentication already working before you change ban rules.

Step 1 — Confirm Ubuntu and Your SSH Access Path

Check the operating system:

Bash
cat /etc/os-release

Confirm the SSH service is active:

Bash
systemctl is-active ssh

Before changing fail2ban settings, keep your current SSH session open and confirm you can open a second SSH session from your normal workstation. This gives you a recovery path if you make a configuration mistake.

If you have not configured SSH keys yet, follow How to Set Up SSH Keys on Ubuntu 24.04 first.

Verify: Ubuntu should report 24.04, the SSH service should be active, and a second SSH session should connect successfully before you continue.

Step 2 — Install fail2ban from the Ubuntu Repository

Update package metadata and install fail2ban:

Bash
sudo apt update sudo apt install -y fail2ban

Check the installed package version:

Bash
apt-cache policy fail2ban

Ubuntu 24.04's current Noble Updates package is fail2ban 1.0.2-3ubuntu0.1, with Ubuntu security and maintenance revisions applied through package updates. You can verify the current Ubuntu package at packages.ubuntu.com.

Enable fail2ban at boot:

Bash
sudo systemctl enable fail2ban

Do not assume the SSH jail is enabled simply because the service package is installed; jails are disabled by default unless distribution or local configuration enables them.

Verify: fail2ban-client --version should report Fail2Ban 1.0.2, apt-cache policy fail2ban should show the current Ubuntu package revision, and systemctl is-enabled fail2ban should report enabled.

Step 3 — Create a Local SSH Jail Override

Do not edit /etc/fail2ban/jail.conf directly. Upstream fail2ban recommends putting local overrides in jail.local or .local files under /etc/fail2ban/jail.d/, so package upgrades can update defaults without overwriting your custom settings.

Create a dedicated SSH override:

Bash
sudo nano /etc/fail2ban/jail.d/sshd.local

Add:

Ini
[sshd] enabled = true backend = systemd port = ssh bantime = 1h findtime = 10m maxretry = 5

The systemd backend reads authentication events from the journal. When this backend is used, do not add a logpath directive; fail2ban's systemd backend uses the filter's journalmatch instead. This avoids depending on /var/log/auth.log being present on every Ubuntu installation.

If SSH listens on a custom port, replace port = ssh with the actual port number.

Verify: Run sudo cat /etc/fail2ban/jail.d/sshd.local and confirm the jail is enabled, the backend is systemd, and the SSH port matches the server's real SSH listener.

Step 4 — Decide Whether Any Administrative IP Should Be Ignored

fail2ban can exclude trusted addresses with ignoreip, but only add addresses or networks that are stable and under your control. Whitelisting a temporary hotel, mobile, ISP, or shared office address can create an unintended bypass later if that address is reassigned.

If you have a stable administrative IP or VPN subnet, add it to a [DEFAULT] override:

Bash
sudo nano /etc/fail2ban/jail.d/00-local-defaults.local

For example:

Ini
[DEFAULT] ignoreip = 127.0.0.1/8 ::1 203.0.113.10

Replace 203.0.113.10 with a stable source you actually control, or omit it entirely if you do not have one.

Check your current public source address from the workstation you use for SSH and compare it with any configured ignoreip entry before testing bans.

Verify: Run sudo fail2ban-client -d | grep -i ignoreip after the configuration test in the next step and confirm only intended trusted addresses are excluded.

Step 5 — Validate the Configuration Before Restarting

Test the full fail2ban configuration:

Bash
sudo fail2ban-client -t

A successful test should end without a configuration error. If it fails, inspect the local files you just created before restarting the service.

You can also inspect the resolved configuration for the SSH jail:

Bash
sudo fail2ban-client -d | grep -A 20 "'sshd'"

Do not restart fail2ban after a failed configuration test.

Verify: sudo fail2ban-client -t should report that the configuration is OK.

Step 6 — Start fail2ban and Confirm the SSH Jail

Restart fail2ban so the local SSH jail is loaded:

Bash
sudo systemctl restart fail2ban

Check the service:

Bash
systemctl is-active fail2ban

List active jails:

Bash
sudo fail2ban-client status

Then inspect SSH specifically:

Bash
sudo fail2ban-client status sshd

The SSH jail status shows currently failed attempts, total failures, current bans, total bans, and the banned IP list. A new server may legitimately show zero bans.

Verify: systemctl is-active fail2ban should return active, and sudo fail2ban-client status should list sshd as an active jail.

Step 7 — Confirm fail2ban Is Reading SSH Journal Events

Inspect the SSH journal directly:

Bash
sudo journalctl -u ssh --since '30 minutes ago' --no-pager

Then check fail2ban's SSH jail:

Bash
sudo fail2ban-client status sshd

Review fail2ban service logs:

Bash
sudo journalctl -u fail2ban -n 100 --no-pager

The jail may show zero failed attempts if no matching authentication failures occurred during the observation window. That is not itself an error.

If the SSH jail is active but never sees events that are visible in the SSH journal, check the backend and filter configuration before changing retry thresholds.

Verify: The SSH jail should remain active without repeated backend or journal errors in the fail2ban service log.

Step 8 — Test the Ban Action Without Locking Yourself Out

Do not intentionally enter bad SSH passwords repeatedly from your only administrative connection. First inspect the resolved SSH jail configuration so you know which ban action is active:

Bash
sudo fail2ban-client -d | grep -A 40 "sshd"

You can also inspect the loaded jail actions directly:

Bash
sudo fail2ban-client get sshd actions

Then use fail2ban's manual ban command with a documentation-only test address that is not your own source IP:

Bash
sudo fail2ban-client set sshd banip 198.51.100.25

Check the jail:

Bash
sudo fail2ban-client status sshd

The test address should appear in the banned IP list.

If the resolved action uses nftables, inspect the corresponding fail2ban rules or sets:

Bash
sudo nft list ruleset | grep -i -A 8 f2b

If your system uses a different action backend, inspect that backend instead of assuming nftables. Do not assume a fail2ban ban must appear as a permanent UFW rule; fail2ban's active action can manage firewall state independently of the baseline rules shown by ufw status.

Remove the test ban:

Bash
sudo fail2ban-client set sshd unbanip 198.51.100.25

Verify: The test IP should appear while banned, the loaded action should match the firewall mechanism you inspect, and the IP should disappear from sudo fail2ban-client status sshd after the unban command.

Step 9 — Tune Ban Time and Retry Thresholds Conservatively

The initial values in this guide are:

Ini
bantime = 1h findtime = 10m maxretry = 5

These mean that five matching failures within ten minutes trigger a one-hour ban. There is no universal ideal value. Shared administrator addresses, automation, monitoring systems, and users behind NAT can make aggressive thresholds more likely to block legitimate access.

If repeated offenders are a problem, fail2ban supports incremental ban times. Add these settings under [DEFAULT] in /etc/fail2ban/jail.d/00-local-defaults.local:

Ini
[DEFAULT] bantime.increment = true bantime.factor = 2 bantime.maxtime = 1d

After changing values, validate before restart:

Bash
sudo fail2ban-client -t sudo systemctl restart fail2ban

Verify: sudo fail2ban-client -t should pass, and sudo fail2ban-client get sshd bantime should show the effective base ban duration in seconds.

Step 10 — Manage Bans and Review Activity

List SSH jail status:

Bash
sudo fail2ban-client status sshd

Ban an address manually when there is a specific operational reason:

Bash
sudo fail2ban-client set sshd banip 203.0.113.50

Unban it:

Bash
sudo fail2ban-client set sshd unbanip 203.0.113.50

To inspect recent fail2ban activity through systemd:

Bash
sudo journalctl -u fail2ban --since today --no-pager

Do not interpret raw ban counts as a security score. Higher or lower counts can result from IP exposure, scanning activity, logging configuration, retry thresholds, and whether password authentication is enabled.

Verify: Manual ban and unban commands should change the SSH jail's banned IP list immediately.

Step 11 — Harden SSH Beyond fail2ban

fail2ban reacts to matching failures; it does not remove the underlying SSH authentication surface. For administrative servers, prioritize SSH keys and disable password authentication only after confirming key-based login works from a second session.

Check effective SSH settings:

Bash
sudo sshd -T | grep -E 'passwordauthentication|permitrootlogin|pubkeyauthentication'

If you change SSH configuration, validate it before reload:

Bash
sudo sshd -t

Then reload SSH only after a successful validation:

Bash
sudo systemctl reload ssh

Keep your existing session open until you confirm a new session works. Combine this with UFW firewall rules, the broader Ubuntu server hardening checklist, and Cloud Firewall Best Practices.

Verify: A new SSH session should authenticate successfully using the intended method after any SSH hardening change.

Step 12 — Troubleshoot Common fail2ban Problems

If fail2ban will not start, test configuration first:

Bash
sudo fail2ban-client -t sudo journalctl -u fail2ban -n 100 --no-pager

If sshd does not appear in the jail list, confirm:

Bash
sudo cat /etc/fail2ban/jail.d/sshd.local sudo fail2ban-client status

If the jail starts but does not detect SSH failures, confirm SSH events exist:

Bash
sudo journalctl -u ssh --since '30 minutes ago' --no-pager

For a jail using backend = systemd, do not configure logpath; fail2ban's documentation states that logpath is not valid for the systemd backend because journal matching is used instead.

If bans are created but connectivity is unaffected, inspect the active action and firewall rules rather than assuming UFW must contain the ban:

Bash
sudo fail2ban-client get sshd actions sudo nft list ruleset | grep -i -A 8 f2b

Verify: Resolve configuration errors first, then confirm the service, jail, log source, and firewall action in that order.

Step 13 — Run the Final End-to-End Verification

Check the service and jail:

Bash
systemctl is-active fail2ban sudo fail2ban-client status sudo fail2ban-client status sshd

Validate the configuration again:

Bash
sudo fail2ban-client -t

Confirm SSH is healthy:

Bash
systemctl is-active ssh sudo sshd -t

Review recent fail2ban errors:

Bash
sudo journalctl -u fail2ban -p warning --since today --no-pager

If you want one final action test, repeat the documentation-address ban and immediately remove it:

Bash
sudo fail2ban-client set sshd banip 198.51.100.25 sudo fail2ban-client status sshd sudo fail2ban-client set sshd unbanip 198.51.100.25

Verify: fail2ban and SSH should both be active, the sshd jail should be loaded, configuration validation should pass, and the manual test ban/unban should work without affecting your administrator IP.

Step 14 — Roll Back or Remove fail2ban Safely

To disable only the custom SSH jail while keeping fail2ban installed, move the local override out of the active configuration path:

Bash
sudo mv /etc/fail2ban/jail.d/sshd.local \ /etc/fail2ban/sshd.local.disabled sudo fail2ban-client -t sudo systemctl restart fail2ban

Confirm the SSH jail is gone:

Bash
sudo fail2ban-client status

To stop fail2ban entirely:

Bash
sudo fail2ban-client unban --all sudo systemctl disable --now fail2ban

If you intentionally want to remove the package and its configuration:

Bash
sudo apt purge -y fail2ban sudo apt autoremove -y

Do not remove or weaken SSH key authentication or firewall rules merely because fail2ban has been removed; those controls solve different problems.

Verify: After a full removal, systemctl status fail2ban should report that the unit is unavailable or inactive, while SSH access should continue to work through your existing SSH configuration.

Conclusion

You now have fail2ban protecting SSH on Ubuntu 24.04 with a dedicated local jail override, systemd journal monitoring, explicit retry and ban windows, configuration validation, safe ban testing, activity inspection, troubleshooting, and rollback instructions.

Compare current Raff pricing for Linux VM configurations and billing terms. fail2ban itself is lightweight; size the VM for the services it protects and use SSH keys, minimal public ports, timely updates, and a tested administrative recovery path as part of the security baseline.

Deploy a Raff Ubuntu VM and secure its SSH access with fail2ban, SSH keys, and restricted network exposure. Select VM resources for your actual workloads.

Tested on 1 vCPU and 2 GB RAM: $8.99 a month · 14-day money-back guarantee

Deploy Your Server
Was this article helpful?
Serdar Tekin
Serdar Tekin
Co-Founder & CTO

Co-Founder & CTO at Raff, focused on cloud infrastructure, systems architecture, security, reliability, and production operations.

Block SSH brute-force attempts on your own Ubuntu 24.04 server.

Tested on 1 vCPU and 2 GB RAM: $8.99 a month

Ubuntu, Debian and other Linux distributions with full root and SSH access, up and running in about 55 seconds.

14-day money-back guarantee

Frequently asked questions

What does fail2ban do?
fail2ban monitors logs or journal events for repeated matching failures and temporarily blocks the responsible source addresses using a configured firewall action. It is commonly used to reduce repeated SSH authentication attempts.
Does Ubuntu 24.04 support fail2ban?
Yes. Ubuntu 24.04 provides fail2ban 1.0.2 in the Noble package line; the current Noble Updates package is 1.0.2-3ubuntu0.1.
Should fail2ban use /var/log/auth.log on Ubuntu 24.04?
Not necessarily. This tutorial explicitly uses the systemd backend for the SSH jail so fail2ban reads SSH events from the systemd journal. With the systemd backend, a logpath setting should not be added.
Does fail2ban require UFW?
No. fail2ban uses configurable firewall actions and modern installations can work with nftables or iptables-based backends. UFW can still define the server's baseline firewall policy, but fail2ban bans do not need to appear as persistent UFW rules.
Can fail2ban lock me out of SSH?
Yes if your source address matches the jail's failure conditions and is not ignored. Keep an existing SSH session open while testing, confirm a second session works first, and do not intentionally trigger failures from your only administrative connection.
Should I permanently whitelist my home IP?
Only if the address or network is stable and under your control. Avoid whitelisting temporary or reassigned ISP, hotel, mobile, or shared-network addresses because they can later become an unintended bypass.
Is fail2ban enough to secure SSH?
No. Use it alongside SSH key authentication, appropriate firewall rules, software updates, restricted administrative access, and careful SSH configuration.
How much does a Raff Linux VM for fail2ban cost?
Raff Linux VM pricing depends on the VM class and resources selected. Check rafftechnologies.com/pricing for current plans and billing terms. fail2ban itself is lightweight, so select VM capacity based on the applications and other services running on the server.