How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Install fail2ban on Ubuntu (SSH, nginx, and WordPress Filters)
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Server Management > How to Install fail2ban on Ubuntu (SSH, nginx, and WordPress Filters)
Server Management

How to Install fail2ban on Ubuntu (SSH, nginx, and WordPress Filters)

how7o
By how7o
Last updated: May 24, 2026
10 Min Read
fail2ban shield blocking incoming brute-force probes, log file feeding the scanner
SHARE

If you’ve already switched to SSH keys and disabled passwords, you’ve stopped the loudest attackers. fail2ban stops the quiet ones — bots probing your nginx 404 logs, WordPress login pages, and any other service that exposes a “wrong attempt” signal. This guide shows how to install fail2ban on Ubuntu, configure the SSH jail correctly, add nginx and WordPress filters, and avoid the one configuration mistake that bans you from your own server.

Contents
  • TL;DR
  • How fail2ban actually works
  • Step 1 — Install fail2ban
  • Step 2 — Create your own jail.local (never edit jail.conf)
  • Step 3 — Add nginx jails
  • Step 4 — Add a WordPress jail
  • Step 5 — Apply the config
  • Day-to-day operations
  • Troubleshooting
    • “fail2ban started but no IPs are being banned”
    • “I banned myself”
    • “Filter not matching even though I see the attack in the log”
  • Related guides

TL;DR

sudo apt install fail2ban on Ubuntu 22.04 / 24.04 / 26.04, create /etc/fail2ban/jail.local (never edit jail.conf — it gets overwritten on every apt update), enable the SSH jail, add your own IP to ignoreip, restart with systemctl restart fail2ban, and verify with fail2ban-client status. nginx and WordPress filters are extra [jail-name] blocks in the same file.

How fail2ban actually works

fail2ban is a log scanner wired to a firewall trigger. It tails service log files (/var/log/auth.log for SSH, /var/log/nginx/error.log for nginx, etc.), runs each line through a regex called a filter, and if the same source IP fails the filter more than maxretry times within findtime seconds, fail2ban tells the kernel firewall (iptables, nftables, or ufw) to drop that IP for bantime seconds.

It doesn’t read your application code, doesn’t patch anything in the protected service, and adds no runtime overhead to legitimate traffic — only banned IPs hit the kernel-level drop. Memory footprint is around 30 MB.

Step 1 — Install fail2ban

sudo apt update
sudo apt install fail2ban

The package ships with the daemon, a default /etc/fail2ban/jail.conf, and pre-built filters in /etc/fail2ban/filter.d/. After install, fail2ban is already running with the default [sshd] jail enabled.

Step 2 — Create your own jail.local (never edit jail.conf)

The cardinal rule: don’t edit jail.conf. Every apt upgrade of the fail2ban package overwrites it. Your customisation lives in jail.local, which fail2ban reads after jail.conf and which apt leaves alone.

sudo nano /etc/fail2ban/jail.local

Start with a sensible global default + SSH jail:

[DEFAULT]
# Ban for 1 hour after 5 failures in 10 minutes
bantime = 1h
findtime = 10m
maxretry = 5

# Never ban these IPs — your office, your home, monitoring
ignoreip = 127.0.0.1/8 ::1 198.51.100.42

# Use nftables (the modern backend)
banaction = nftables-multiport
banaction_allports = nftables-allports

[sshd]
enabled = true
port = ssh
filter = sshd
logpath = %(sshd_log)s
backend = %(sshd_backend)s

Critical: put your own public IP in ignoreip before restarting fail2ban. If you accidentally trigger the SSH jail from your own machine (a few failed key attempts during testing), you’ll be locked out for bantime. The CIDR notation is fine: 198.51.100.0/24 covers a small office subnet.

Find your current public IP with curl ifconfig.me from the machine you’ll be SSHing from.

fail2ban architecture — log scan, filter regex match, jail trigger, firewall drop

Step 3 — Add nginx jails

Append these to the same /etc/fail2ban/jail.local:

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

[nginx-badbots]
enabled = true
port = http,https
filter = nginx-badbots
logpath = /var/log/nginx/access.log
maxretry = 2

[nginx-noscript]
enabled = true
port = http,https
filter = nginx-noscript
logpath = /var/log/nginx/access.log
maxretry = 6

[nginx-noproxy]
enabled = true
port = http,https
filter = nginx-noproxy
logpath = /var/log/nginx/access.log
maxretry = 2
  • nginx-http-auth — bans IPs that fail HTTP Basic Auth (used for protected admin paths).
  • nginx-badbots — bans known scrapers and exploit scanners by User-Agent.
  • nginx-noscript — bans IPs hitting .php, .aspx, or other script extensions on a static-only site.
  • nginx-noproxy — bans IPs trying to use your nginx as an open proxy (the classic CONNECT probe).

Step 4 — Add a WordPress jail

fail2ban doesn’t ship a WordPress filter out of the box. Create one in /etc/fail2ban/filter.d/wordpress.conf:

[Definition]
failregex = ^<HOST> .* "POST .*wp-login\.php
            ^<HOST> .* "POST .*xmlrpc\.php
ignoreregex =

Then add the jail to jail.local:

[wordpress]
enabled = true
port = http,https
filter = wordpress
logpath = /var/log/nginx/access.log
maxretry = 3
findtime = 5m
bantime = 24h

The longer bantime here is deliberate — WordPress brute-force bots come back the moment they’re unbanned. A 24-hour ban moves them on to other targets.

Step 5 — Apply the config

sudo systemctl restart fail2ban
sudo systemctl enable fail2ban

Verify everything’s running:

sudo fail2ban-client status
sudo fail2ban-client status sshd

The first command lists active jails; the second shows the SSH jail’s currently-banned IPs and total bans. Within an hour on a public-facing server, you’ll see banned IPs accumulate.

Day-to-day operations

  • Unban an IP: sudo fail2ban-client set sshd unbanip 198.51.100.42
  • Ban an IP manually: sudo fail2ban-client set sshd banip 198.51.100.42
  • See total bans across all jails: sudo fail2ban-client status
  • Reload after config change: sudo systemctl reload fail2ban (faster than restart, doesn’t lose existing bans)
  • View banned IPs in iptables/nftables directly: sudo nft list table inet f2b-table

Troubleshooting

“fail2ban started but no IPs are being banned”

Run sudo fail2ban-client status sshd — if “Currently failed” is increasing, fail2ban is seeing failures; if it’s zero, the log path is wrong. On modern Ubuntu (22.04+), SSH logs go to systemd-journald, not /var/log/auth.log. Set backend = systemd in the [sshd] block.

“I banned myself”

Use your VPS provider’s web console to log in directly, then sudo fail2ban-client set sshd unbanip YOUR_IP. Add yourself to ignoreip in jail.local. Reload.

“Filter not matching even though I see the attack in the log”

Test the regex against the actual log line with fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/wordpress.conf. It’ll tell you exactly how many lines match and which lines didn’t. The most common cause is log format mismatch — custom nginx log_format directives don’t match the default filter regexes.

Does fail2ban still work if I’m already using SSH keys?

Yes, and it’s still worth running. Keys block password brute-force, but fail2ban catches other attack patterns — WordPress xmlrpc abuse, nginx HTTP Basic Auth probing, bad-bot scanners, and any service-level abuse that shows up in logs. The SSH jail does less work in a key-only setup, but the nginx and WordPress jails are doing the real protection.

How long should I set bantime?

1 hour is a sane default for SSH and most service jails. For WordPress login/xmlrpc attacks, 24 hours is better — the bots are aggressive and come back fast. For repeat offenders, fail2ban supports a recidive jail that watches its own logs and applies a multi-day ban to IPs that get banned repeatedly. The recidive jail is in the default jail.conf; enable it in your jail.local.

Does fail2ban work with ufw or only iptables?

Both, plus nftables. ufw is a frontend for iptables/nftables, so the underlying integration is the same. The recommended banaction on modern Ubuntu (22.04+) is nftables-multiport, which works regardless of whether you also use ufw. On older systems, iptables-multiport is the default.

Will fail2ban slow my server down?

No measurable impact in normal use. The daemon uses about 30 MB of RAM and scans logs in batches. Banned IPs are dropped by the kernel firewall before any service sees them, which is actually faster than letting them connect and fail. The only scenario with noticeable cost is enormous log files (gigabytes) — set maxlines in jail.local to limit how much history fail2ban scans on startup.

Should I install fail2ban or use Cloudflare instead?

They’re complementary, not alternatives. Cloudflare drops attacks at the edge (before traffic reaches your server), which is excellent for high-volume scrapers and DDoS. fail2ban handles attacks that get past Cloudflare — direct-IP scans, traffic from misconfigured Cloudflare rules, and brute force on services that aren’t behind the proxy at all (SSH, mail). Use both.

How do I unban an IP without restarting fail2ban?

sudo fail2ban-client set <jail> unbanip <ip>. The jail name is the one in square brackets in jail.local (e.g. sshd, nginx-http-auth, wordpress). For a full unban across all jails: sudo fail2ban-client unban <ip>.

Related guides

  • Set up SSH key authentication on Ubuntu — first line of defence before fail2ban does anything.
  • Harden an Ubuntu VPS — the broader hardening checklist fail2ban fits into.
  • Change the SSH port on Linux — moves brute-force noise off port 22 entirely.
  • Let’s Encrypt with Certbot — for the HTTPS that nginx-http-auth protects.

For the full fail2ban configuration reference, the official fail2ban wiki documents every option, filter, and jail action.

TAGGED:configurationNginxSecurityUbuntuwordpress

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article SSH key authentication — keypair, terminal window, lockout-protection parallel session How to Set Up SSH Key Authentication on Ubuntu (Without Locking Yourself Out)
Next Article MariaDB 11.4 LTS database cylinder being installed on Ubuntu How to Install MariaDB 11.4 LTS on Ubuntu 24.04 (Fresh Install, Secured)
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026
Caddy server — automatic HTTPS, 3-line Caddyfile vs 25-line nginx config
How to Install Caddy Server on Ubuntu (Automatic HTTPS, Drop-in nginx Alternative)
May 24, 2026
Cloudflare Tunnel — outbound-only connection from server, no inbound port forward
How to Install Cloudflare Tunnel on Ubuntu (Expose Local Services, No Port Forwarding)
May 24, 2026
WireGuard encrypted tunnel between server and clients with lock icons
How to Set Up WireGuard VPN on Ubuntu (Server, Linux Client, and iOS)
May 24, 2026

You Might Also Like

aaPanel MySQL root password reset via the Databases page
Server Management

How to Reset the MySQL Root Password in aaPanel

6 Min Read
Install MySQL on Ubuntu 22.04 — terminal with apt command and database cylinder icon
Server Management

How to Install MySQL on Ubuntu 22.04: Step-by-Step Guide

9 Min Read
Securely hash passwords in PHP with password_hash
Web Development

Securely Hash Passwords in PHP (password_hash, Argon2id)

6 Min Read
MySQL primary and replica database cylinders connected by replication arrows
Server Management

How to Set Up MySQL Primary-Replica Replication on Ubuntu (Production Guide)

10 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?