Install fail2ban on Ubuntu

fail2ban watches log files for brute-force patterns and bans the source IPs at the firewall. Out of the box it handles SSH; with a few extra jail files it covers Nginx auth, Postfix, and the n8n/Portainer/Open WebUI admin endpoints too.

Disclosure: contains an affiliate link to Inferno VPS.

Install

sudo apt update
sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban
sudo fail2ban-client status

You should see at least the sshd jail active by default on recent Ubuntu.

The right way to configure: jail.local

Never edit /etc/fail2ban/jail.conf — updates overwrite it. Create jail.local instead:

sudo nano /etc/fail2ban/jail.local
[DEFAULT]
bantime  = 1h
findtime = 10m
maxretry = 5
backend  = systemd
ignoreip = 127.0.0.1/8 ::1 YOUR.HOME.IP

[sshd]
enabled = true
mode    = aggressive

[nginx-http-auth]
enabled  = true
port     = http,https
logpath  = /var/log/nginx/error.log

[nginx-botsearch]
enabled  = true
port     = http,https
logpath  = /var/log/nginx/access.log

Key knobs:

Apply and verify

sudo systemctl restart fail2ban
sudo fail2ban-client status sshd
# Status for the jail: sshd
# |- Filter
# |  |- Currently failed: 0
# |  |- Total failed: 17
# |  `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd
# `- Actions
#    |- Currently banned: 2
#    |- Total banned: 3
#    `- Banned IP list: 198.51.100.4 203.0.113.99

If Currently banned climbs over the first day, the jail works. It always climbs. Brute force scanners are constant.

Useful commands

# List jails
sudo fail2ban-client status

# Status of one jail
sudo fail2ban-client status sshd

# Manually ban / unban
sudo fail2ban-client set sshd banip 198.51.100.4
sudo fail2ban-client set sshd unbanip 198.51.100.4

# Watch log
sudo tail -F /var/log/fail2ban.log

Recidive: escalate repeat offenders

The default jails ban for 1 hour; a determined scanner returns after expiry. The recidive jail bans IPs that have been banned multiple times — escalating the lockout to a week:

# /etc/fail2ban/jail.local
[recidive]
enabled  = true
bantime  = 1w
findtime = 1d
maxretry = 5
logpath  = /var/log/fail2ban.log

Custom jails: Nginx basic auth endpoints

If you protect admin URLs with Nginx basic auth (Portainer, Grafana behind auth, etc.), 401 attempts get logged as user X was not found or similar. The built-in nginx-http-auth jail catches these. For non-default error messages, write your own filter:

# /etc/fail2ban/filter.d/admin-portal.conf
[Definition]
failregex = ^<HOST> -.*"(GET|POST) /admin.*" 401
ignoreregex =
# /etc/fail2ban/jail.local
[admin-portal]
enabled  = true
port     = http,https
filter   = admin-portal
logpath  = /var/log/nginx/access.log
maxretry = 4
findtime = 10m
bantime  = 2h

Test a jail (carefully)

From a different IP (your phone on cellular), try the wrong credential a few times. Watch fail2ban.log — you should see the ban fire after maxretry attempts. Then unban yourself before you accidentally use the same IP for real:

sudo fail2ban-client set sshd unbanip YOUR.TEST.IP

Common fail2ban mistakes

"fail2ban is running but nothing gets banned"

Wrong log path or wrong backend. On systemd distros use backend = systemd globally. Confirm the filter matches: sudo fail2ban-regex /var/log/nginx/error.log /etc/fail2ban/filter.d/nginx-http-auth.conf should show matches.

"Banning works for sshd, fails for Nginx jails"

Nginx logs in a non-standard location or format. Update logpath; if you use JSON logs, the default regex won't match — write a custom filter.

"I banned myself"

Use the provider's VNC / console to unban: sudo fail2ban-client set sshd unbanip YOUR.IP. Add your IP to ignoreip in jail.local.

"Behind a reverse proxy, all bans hit the proxy IP"

The downstream service logs the proxy's IP, not the real client's. Either configure the proxy to forward X-Forwarded-For AND have the downstream log it, or run fail2ban on the proxy itself.

VPS for a fail2ban-protected stack?

fail2ban is < 30 MB RAM. Fits on every Inferno plan. With Inferno's network-level DDoS protection ahead of it, you're covered from both directions.

View Inferno plans →

What to do next


Author: Daniel Ferro — Lisbon. Last tested: 2026-05-22 on Ubuntu 22.04, fail2ban 0.11.