To configure Fail2ban on a Linux server, install the package, create /etc/fail2ban/jail.local, enable jails (for example, sshd) with bantime, findtime, maxretry, and the correct logpath, then restart and enable the service. Verify with fail2ban-client, test bans from a second host, and monitor /var/log/fail2ban.log.
Looking for a 2026-ready walkthrough on how to configure Fail2ban on a Linux server? This step-by-step guide shows you how to install, configure, test, and maintain Fail2ban to block brute-force attacks on SSH, Nginx/Apache, and WordPress. We’ll use best-practice settings, explain key options, and cover real-world troubleshooting from a hosting perspective.
What Is Fail2ban and Why You Need It

Fail2ban is a log-parsing intrusion prevention tool. It monitors service logs (SSH, web server, mail, etc.) and automatically bans IPs that show malicious behavior—typically via firewall rules (iptables/nftables, UFW, or firewalld). It’s lightweight, highly configurable, and a must-have for Internet-facing Linux servers.
How Fail2ban Works?
- Filters: Regular expressions that detect suspicious log entries.
- Jails: Bind a filter to a service and action with thresholds (max retries within a time window).
- Actions: What happens on a match—usually add a firewall rule to drop traffic from the offending IP.
- Backend: How logs are read (pyinotify/polling/systemd-journal).
Prerequisites and Supported Environments
- A Linux server (Ubuntu/Debian, Rocky/AlmaLinux/CentOS, Fedora, openSUSE).
- Root/sudo access.
- OpenSSH or web server logs accessible.
- Firewall available: iptables/nftables, UFW, or firewalld.
Tip: On cloud providers (AWS, GCP, Azure), Fail2ban complements security groups/firewalls—it doesn’t replace them.
Step-by-Step: Install and Configure Fail2ban (2026)
1) Install Fail2ban
# Ubuntu/Debian
sudo apt update
sudo apt install -y fail2ban
# Rocky/AlmaLinux/RHEL 8/9 (EPEL required on some versions)
sudo dnf install -y epel-release
sudo dnf install -y fail2ban
# Fedora
sudo dnf install -y fail2ban
# openSUSE
sudo zypper install -y fail2ban
Enable the service at boot and start it:
sudo systemctl enable --now fail2ban
sudo systemctl status fail2ban
2) Create jail.local (Never edit jail.conf)
Fail2ban ships with /etc/fail2ban/jail.conf as a reference. Put your custom configuration in /etc/fail2ban/jail.local so updates don’t overwrite your changes.
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
Start with secure, beginner-friendly defaults:
[DEFAULT]
# Who gets email alerts
destemail = admin@example.com
sender = fail2ban@yourserver.example
sendername = Fail2ban
# Ban policy
bantime = 1h
findtime = 15m
maxretry = 5
bantime.increment = true
bantime.factor = 1.5
bantime.formula = bantime * (1 + failures / 2)
# Trusted IPs (never banned)
ignoreip = 127.0.0.1/8 ::1 10.0.0.0/8 192.168.0.0/16
# Use systemd journal if available, else auto
backend = auto
# Choose your firewall integration (set one globally or per jail)
banaction = iptables-multiport
# Alternatives:
# banaction = nftables-multiport
# banaction = ufw
# banaction = firewallcmd-multiport
action = %(action_mwl)s
# action_mw = ban + whois report
# action_mwl = ban + whois + email loglines
3) Enable and Tune the SSH Jail
SSH attacks are the most common. Enable the built-in sshd jail and set the correct log path for your distro.
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log ; Ubuntu/Debian
# logpath = /var/log/secure ; RHEL/Rocky/Alma
maxretry = 5
findtime = 15m
bantime = 1h
Restart to apply:
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd
4) Add a Recidive Jail (Catch Repeat Offenders)
The recidive jail reads Fail2ban’s own log and aggressively bans IPs that trigger multiple jails or repeated bans across time.
[recidive]
enabled = true
filter = recidive
logpath = /var/log/fail2ban.log
bantime = 24h
findtime = 1d
maxretry = 5
banaction = %(banaction)s
action = %(action_mwl)s
5) Email Alerts (Optional but Recommended)
Ensure your server can send email (Postfix/SSMTP/API relays). The action_mwl sends a whois report and log excerpts. Adjust destemail and sender in [DEFAULT].
6) Pick the Right Firewall Backend
Fail2ban can talk to different firewalls. Use one that matches your setup:
- UFW (Ubuntu): set
banaction = ufw. - firewalld (RHEL/Rocky/Alma/Fedora): set
banaction = firewallcmd-multiport. - iptables/nftables: set
banaction = iptables-multiportornftables-multiport.
Consistency matters—don’t mix multiple firewall tools unless you know the implications.
Protecting Web Servers and WordPress
Nginx Basic Auth and 401 Floods
To block repeated 401s (bad passwords) against an admin area protected by Nginx basic authentication:
# /etc/fail2ban/jail.local
[nginx-http-auth]
enabled = true
filter = nginx-http-auth
port = http,https
logpath = /var/log/nginx/error.log
maxretry = 10
findtime = 10m
bantime = 1h
Apache has a similar jail (apache-auth) using /var/log/apache2/error.log or /var/log/httpd/error_log.
WordPress Login Abuse (XML-RPC and wp-login.php)
You can rate-limit wp-login.php and XML-RPC using web server rules, then let Fail2ban block persistent abusers. For Nginx access logs with HTTP 401/403/404 repeats:
# /etc/fail2ban/filter.d/wp-hard-login.conf
[Definition]
failregex = ^<HOST> - .* "(POST|GET) /wp-login\.php.*" (401|403|429|404)
^<HOST> - .* "POST /xmlrpc\.php.*" (401|403|429|404)
ignoreregex =
# /etc/fail2ban/jail.local
[wp-hard-login]
enabled = true
filter = wp-hard-login
port = http,https
logpath = /var/log/nginx/access.log
findtime = 10m
maxretry = 20
bantime = 2h
Adjust failregex to match your access log format. Always test with fail2ban-regex before enabling.
Testing Your Configuration
- Dry-run your filters:
sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf - Force failures from a different IP: Try 6–8 bad passwords over SSH; watch
sudo tail -f /var/log/fail2ban.log. - Check status:
sudo fail2ban-client statusandsudo fail2ban-client status sshd. - Unban quickly:
sudo fail2ban-client set sshd unbanip 203.0.113.10.
Monitoring and Maintenance
- Review
/var/log/fail2ban.logweekly for noisy filters. - Use persistent logging (systemd-journald or logrotate defaults).
- Audit
ignoreipranges regularly—don’t whitelist the world. - Update packages quarterly to get new filters and bug fixes.
Troubleshooting Common Issues
- No bans happen: Wrong
logpathor backend. Confirm the file exists and contains failures. Trybackend=systemdif services log to the journal. - Conflicting firewall tools: Don’t mix UFW and firewalld. Pick one
banaction. - Custom log formats: Default filters may not match. Adapt
failregexand validate withfail2ban-regex. - Bans don’t persist: They should, but if a reboot clears rules, ensure Fail2ban starts early and your firewall saves rules.
- False positives: Add trusted IPs to
ignoreipand widenmaxretry/findtimefor noisy apps.
Best-Practice Settings for 2026
- Use incremental bans:
bantime.increment = trueto penalize repeat offenders progressively. - Set realistic thresholds: Common defaults:
maxretry=5,findtime=15m,bantime=1h. Tighten gradually if you see abuse. - Leverage recidive: Essential on public servers to stop rotating attacks.
- Per-service tuning: Web jails often need higher
maxretrythan SSH to avoid catching legitimate traffic. - Harden SSH: Change the port, disable password auth, and use keys. Fail2ban is a layer, not the only control.
Security Hardening Checklist (Beyond Fail2ban)
- Enforce SSH keys, disable root SSH login.
- Use a host firewall with default-deny inbound.
- Keep OS and services patched via unattended upgrades or automation.
- Enable 2FA on control panels and dashboards.
- Back up configs and logs to off-server storage.
Prefer turnkey security? YouStable’s managed servers ship with hardened SSH, curated Fail2ban policies, web server rate limits, and continuous monitoring—so you can focus on your apps, not the noise. Speak with our team to tailor jails to your stack.
Advanced: Useful Fail2ban Commands
# Global status
sudo fail2ban-client status
# Status of a single jail
sudo fail2ban-client status sshd
# Ban or unban an IP manually
sudo fail2ban-client set sshd banip 203.0.113.10
sudo fail2ban-client set sshd unbanip 203.0.113.10
# Reload config without dropping existing bans
sudo fail2ban-client reload
# Test a filter against a log
sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-http-auth.conf
Real-World Example: Minimal, Safe SSH-Only Setup
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
bantime.increment = true
ignoreip = 127.0.0.1/8 ::1
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
# For RHEL/Rocky/Alma use: /var/log/secure
This configuration blocks common SSH brute-force bursts while avoiding false positives. Expand with recidive and web jails as your exposure grows.
FAQs: Fail2ban Configuration on Linux
Does Fail2ban work with UFW or firewalld?
Yes. Set banaction = ufw for UFW or banaction = firewallcmd-multiport for firewalld. The default iptables/nftables actions also work if those are your primary firewalls. Avoid running multiple firewall managers simultaneously.
How do I unban an IP quickly?
Run sudo fail2ban-client set <jail> unbanip <IP>. For example: sudo fail2ban-client set sshd unbanip 203.0.113.10. You can check current bans with sudo fail2ban-client status sshd.
Where should I put my custom settings?
Always put changes in /etc/fail2ban/jail.local (or files under jail.d/). Don’t edit jail.conf, as it is overwritten by package updates.
What are safe bantime and maxretry values?
Good starting points are bantime=1h, findtime=10–15m, maxretry=5. Use bantime.increment=true to escalate for repeat offenders. For web apps, increase maxretry to avoid blocking legitimate users.
Fail2ban vs. a WAF—do I need both?
They’re complementary. Fail2ban blocks abusive IPs based on log patterns; a WAF inspects HTTP requests in real time. Use Fail2ban for brute-force and scan suppression, and a WAF/CDN (e.g., ModSecurity/Cloudflare) for application-layer threats.
Wrap-Up and Next Steps
Now you know how to configure Fail2ban on a Linux server: install it, create jail.local, enable critical jails, test thoroughly, and monitor logs. Keep thresholds realistic, enable recidive, and match your firewall backend.
Need a pre-hardened stack? YouStable can provision servers with best-practice Fail2ban policies from day one.