How to Install Fail2ban on Ubuntu
Install and configure Fail2ban on Ubuntu to ban brute-force SSH and web logins: jails, escalating bans, the Docker firewall caveat, whitelists, testing and troubleshooting.
What Fail2ban does
The moment a server is reachable from the internet, automated bots start hammering it with login attempts. Fail2ban watches your log files, spots repeated failures from the same IP address, and temporarily bans that address at the firewall. It's a simple, effective layer that dramatically cuts brute-force noise and keeps your logs readable.
It works with filters (regular expressions that recognise a failed login in a log file), jails (a filter plus a log path and ban rules) and actions (what to do on a ban, normally adding a firewall rule). Once you understand those three words, the configuration is easy to read.
Do you need it?
If your server is only reachable inside your home network or through a VPN, Fail2ban is optional hardening. It becomes valuable when something is exposed to the internet: SSH, a reverse proxy, a password manager, a photo gallery. Even then it is a complement, not a substitute, for the basics in the home server security checklist.
Step 1: Install
On Debian or Ubuntu:
sudo apt update
sudo apt install fail2banFail2ban starts automatically. Confirm it's running:
sudo systemctl status fail2ban --no-pagerStep 2: Create a local config
Never edit jail.conf directly — it's overwritten on upgrades. Create a jail.local override instead:
sudo nano /etc/fail2ban/jail.local[DEFAULT]
# Ban for 1 hour after 5 failures within 10 minutes
bantime = 1h
findtime = 10m
maxretry = 5
# Never ban your own network
ignoreip = 127.0.0.1/8 ::1 192.168.1.0/24
# Use nftables (modern) or iptables
banaction = nftables-multiport
# Recent Ubuntu releases log SSH to the systemd journal
backend = systemd
[sshd]
enabled = true
port = sshAdjust ignoreip to your LAN so you can't accidentally lock yourself out. If your SSH port is not 22, set port to the real number. On older systems that write /var/log/auth.log, use backend = auto and make sure logpath = %(sshd_log)s exists.
Step 3: Apply and verify
sudo systemctl restart fail2ban
sudo fail2ban-client status
sudo fail2ban-client status sshdThe sshd status shows currently failed and banned IPs. Within a day of internet exposure, you'll usually see bans accumulating. To verify the filter actually matches your log lines, test it directly:
sudo fail2ban-regex systemd-journal sshdThe output reports how many lines matched. If it says zero while you can see failed logins, the backend or log path is wrong.
Step 4: Escalating bans (recommended)
Repeat offenders should be banned for longer. Enable incremental banning in [DEFAULT]:
bantime.increment = true
bantime.factor = 2
bantime.maxtime = 1wNow a persistent attacker's ban time doubles each time, up to a week. You can also enable the built-in recidive jail, which bans an IP for a long time if it keeps getting banned by other jails:
[recidive]
enabled = true
bantime = 1w
findtime = 1d
maxretry = 3Step 5: Protect more than SSH
Fail2ban ships with filters for many services. Useful jails for a homelab:
- Nginx or a reverse proxy — ban IPs failing HTTP basic-auth or probing bad URLs. See Nginx Proxy Manager and Caddy.
- Password manager (Vaultwarden) — a community filter bans failed master-password attempts.
- Home Assistant — ban repeated failed logins.
Each is a small [jail] block pointing at the right log path and filter. An example for a custom service:
[myapp]
enabled = true
port = http,https
filter = myapp
logpath = /var/log/myapp/auth.log
maxretry = 5Create the matching filter in /etc/fail2ban/filter.d/myapp.conf with a failregex line containing <HOST> where the client IP appears, and test it with fail2ban-regex.
Important: Fail2ban and Docker
By default Fail2ban adds bans to the host's input rules. Container ports published by Docker are routed through a different chain, so a ban on the host may not block traffic to a containerised service. To protect containers, use the DOCKER-USER chain, for example by setting chain = DOCKER-USER for the action in that jail, or ban at the reverse proxy level instead. Also remember that services behind a reverse proxy see the proxy's address unless real client IPs are forwarded; configure the proxy to pass X-Forwarded-For and have the service log it. Read Docker networking explained for background.
Step 6: Managing bans
# Unban an IP you banned by mistake
sudo fail2ban-client set sshd unbanip 203.0.113.9
# List banned IPs for a jail
sudo fail2ban-client status sshd
# Reload configuration after changes
sudo fail2ban-client reloadTo be notified of bans, you can configure an email action, but on a busy server this gets noisy. A better approach is to watch ban counts on a dashboard (see Grafana and Prometheus) and only alert on unusual spikes.
Important context
Fail2ban is a complement, not a replacement, for good security. The biggest wins are still:
- Disabling SSH password auth in favour of keys.
- Keeping services off the public internet behind a VPN where possible. See the secure remote access guide.
- Enabling two-factor authentication on exposed dashboards.
- Keeping software updated.
Remember that banning IPs does not help against an attacker who uses thousands of different addresses, or against a vulnerability in the service itself.
Troubleshooting
- Locked yourself out — access via console/KVM, or from another network, and run
fail2ban-client set sshd unbanip <your-ip>. Add your IP or network toignoreip. - No bans ever appear — confirm the log path or backend matches your distribution, the jail is
enabled = true, andfail2ban-regexmatches lines. - Bans don't actually block — verify
banactionmatches your firewall backend (nftables vs iptables), and see the Docker note above. - Service fails to start after editing — run
sudo fail2ban-client -tto test the configuration and show the error. - High CPU use — a very large log file or an expensive regex can slow it down; rotate logs and keep filters tight.
Next steps
Layer Fail2ban with a properly configured firewall and reverse proxy, then check on ban activity occasionally. If you rely on exposed services, consider moving them behind a VPN so Fail2ban has little left to do.
Related articles
The Home Server Security Checklist
A practical, prioritized checklist to secure your home server: SSH keys, firewall (and its Docker gotcha), updates, safe remote access, containers, secrets, backups and monitoring.
How to Set Up Jellyseerr for Media Requests
Install Jellyseerr with Docker, connect it to Jellyfin, Sonarr and Radarr, and set up users, quotas and approvals so your household can request media on its own.
How to Set Up WireGuard VPN
Self-host a WireGuard VPN to reach your homelab securely from anywhere: wg-easy in Docker, a manual wg-quick setup, split vs full tunnel, DNS, CGNAT and troubleshooting.