Fail2ban Hardening for SSH and Nginx on Ubuntu Linux
This deep-dive covers fail2ban ssh nginx ubuntu with actionable analysis, practical implications, and expert insights.
Why Default Fail2ban Settings Leave Web Servers Exposed
Installing Fail2ban on Ubuntu enables only a basic SSH jail with a short 10-minute ban. Modern automated scanners rotate through credential stuffing on /login, probe for .env and wp-login.php files, and return the instant a short ban expires. A properly tuned jail.local pairs kernel-level nftables bans, exponential backoff, custom Nginx filters, and a recidive jail that silences repeat offenders for a full week.
Step 1: Install Fail2ban and Verify the Journal Backend
Install Fail2ban along with the Python systemd binding required to read journald on Ubuntu 24.04:
sudo apt update
sudo apt install -y fail2ban python3-systemd
Never modify /etc/fail2ban/jail.conf directly because package updates overwrite it. All custom rules belong in /etc/fail2ban/jail.local.
Step 2: Configure Incremental Bans, SSH, Nginx, and Recidive Jails
Create /etc/fail2ban/jail.local with your admin IP whitelisted in ignoreip and exponential ban escalation enabled:
[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 203.0.113.50
bantime = 15m
findtime = 10m
maxretry = 5
bantime.increment = true
bantime.factor = 2
bantime.maxtime = 7d
banaction = nftables-multiport
banaction_allports = nftables-allports
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 3
findtime = 10m
bantime = 1h
[nginx-http-auth]
enabled = true
port = http,https
logpath = /var/log/nginx/error.log
maxretry = 4
[nginx-limit-req]
enabled = true
port = http,https
logpath = /var/log/nginx/error.log
findtime = 2m
maxretry = 8
bantime = 30m
[nginx-botsearch]
enabled = true
port = http,https
logpath = /var/log/nginx/access.log
maxretry = 3
findtime = 5m
bantime = 2h
[recidive]
enabled = true
logpath = /var/log/fail2ban.log
banaction = %(banaction_allports)s
bantime = 7d
findtime = 1d
maxretry = 3
Step 3: Add a Custom Nginx Filter for Login Brute Force and Bad Probes
Create a dedicated filter at /etc/fail2ban/filter.d/nginx-bad-probes.conf to catch bots repeatedly hitting sensitive paths like /.env, /.git/config, or hammering login routes with 401/403/429 responses:
[Definition]
failregex = ^<HOST> - \S+ \[.*?\] "(?:GET|POST|HEAD) /(?:wp-login\.php|xmlrpc\.php|\.env|\.git/|phpmyadmin)[^"]*" (?:400|401|403|404|444)
^<HOST> - \S+ \[.*?\] "POST /(?:login|admin/login)[^"]*" (?:401|403|429)
ignoreregex =
Then enable the custom jail at the bottom of /etc/fail2ban/jail.local:
[nginx-bad-probes]
enabled = true
port = http,https
filter = nginx-bad-probes
logpath = /var/log/nginx/access.log
maxretry = 3
findtime = 5m
bantime = 4h
Step 4: Test Filters Against Real Logs Before Restarting
Always dry-run new regex filters with fail2ban-regex so a typo does not break the daemon or match legitimate users:
sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-bad-probes.conf
If the summary shows clean matches without syntax warnings, restart Fail2ban and inspect all active jails:
sudo systemctl enable --now fail2ban
sudo systemctl restart fail2ban
sudo fail2ban-client status
Step 5: Monitor Active Bans and Real Client IPs Behind Proxies
Inspect an individual jail to see total and currently banned IPs:
sudo fail2ban-client status sshd
sudo fail2ban-client status nginx-bad-probes
sudo nft list table inet f2b-table
Important Cloudflare / Reverse Proxy Note: If your Nginx server sits behind Cloudflare or a load balancer, configure Nginx's ngx_http_realip_module (set_real_ip_from and real_ip_header CF-Connecting-IP;) so access logs record the visitor's true IP rather than the proxy IP. Otherwise Fail2ban could ban your own reverse proxy.
Summary
By switching Ubuntu 24.04's SSH jail to backend = systemd, using nftables, enabling exponential bans with recidive, and adding targeted Nginx probe filters, you stop brute-force noise at the firewall layer while keeping CPU and log overhead minimal.
Was this technical analysis helpful?
Your feedback informs our benchmark updates and ongoing article audits.
FAQ
Frequently Asked Questions
Structured for search engines and AI answer systems (AEO/GEO).
Ubuntu 24.04 no longer installs rsyslog by default, so /var/log/auth.log does not exist. Setting backend = systemd inside [sshd] tells Fail2ban to read SSH events directly from journald.
Yes. Setting banaction = nftables-multiport or banaction = ufw integrates cleanly with Ubuntu 24.04 without legacy iptables conflicts.
Run sudo fail2ban-client set <jail-name> unbanip <your-ip> from a secondary console or provider recovery shell, then add your IP to ignoreip in jail.local.
No. Always disable SSH password authentication first (PasswordAuthentication no). Fail2ban is a defense-in-depth layer that drops noisy scanners and protects HTTP endpoints like login forms.
More answers in our FAQ hub.
Newsletter
Practical Linux & Applied AI Newsletter
Get new tested tutorials and benchmark comparisons delivered directly to your inbox. Zero spam.
Join Weekly guides readers getting our weekly digest.
- Practical Linux administration & terminal workflows
- Tested open-source AI setups & benchmarks
- Zero spam - unsubscribe anytime