🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now β†’
Menu

Categories

Fail2ban: Intrusion Prevention Setup Guide for Linux (2026)

Fail2ban: Intrusion Prevention Setup Guide for Linux (2026)

Quick Summary: Fail2ban is an intrusion prevention tool that monitors log files for suspicious activity (like repeated failed login attempts) and automatically bans offending IP addresses by creating firewall rules. It is the most effective defense against brute-force attacks on SSH, web servers, and email services. Setup takes under 10 minutes and dramatically reduces attack noise.

Fail2ban intrusion prevention

How Fail2ban Works

  1. Fail2ban monitors specified log files (e.g., /var/log/auth.log)
  2. It matches log entries against filter patterns (regex)
  3. When an IP exceeds the failure threshold, Fail2ban creates a firewall rule to block it
  4. After the ban time expires, the rule is automatically removed

Installation

  • Debian/Ubuntu: sudo apt install fail2ban
  • RHEL/AlmaLinux/Rocky: sudo dnf install fail2ban
  • Enable and start: sudo systemctl enable --now fail2ban

Configuration

Never edit /etc/fail2ban/jail.conf directly β€” it gets overwritten on updates. Create a local override:

  • sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
  • Edit /etc/fail2ban/jail.local for your customizations

Key Configuration Options

SettingDefaultRecommendedPurpose
bantime10m24h (86400)How long to ban an IP
findtime10m10m (600)Time window for counting failures
maxretry53Failures before banning
banactioniptables-multiportfirewallcmd-rich-rules (RHEL)Firewall backend
ignoreip127.0.0.1Add your office IPNever ban these IPs

Protecting Services

SSH Protection

SSH jail is often enabled by default. Verify and customize in jail.local:

  • Set [sshd] section: enabled = true
  • Set port to match your SSH port (if non-standard)
  • Set maxretry = 3 for strict protection

Web Server Protection

JailProtects Against
nginx-http-authFailed HTTP basic authentication
nginx-botsearchBots scanning for vulnerabilities
apache-authFailed Apache authentication
apache-badbotsMalicious bot user agents

Managing Fail2ban

CommandPurpose
fail2ban-client statusList active jails
fail2ban-client status sshdShow banned IPs for SSH jail
fail2ban-client set sshd unbanip 1.2.3.4Manually unban an IP
fail2ban-client set sshd banip 1.2.3.4Manually ban an IP
fail2ban-client reloadReload configuration

Incremental Ban Times

Configure Fail2ban to increase ban duration for repeat offenders:

  • bantime.increment = true β€” Enable incremental bans
  • bantime.factor = 2 β€” Double the ban time each offense
  • bantime.maxtime = 1w β€” Maximum ban duration of 1 week
  • Result: 1st offense = 24h, 2nd = 48h, 3rd = 96h, up to 1 week

Frequently Asked Questions

I accidentally banned myself. How do I unban?

If you can still access the server (console, different IP): fail2ban-client set sshd unbanip YOUR.IP. If locked out, use your hosting provider's console access. To prevent self-banning, always add your IP to ignoreip in jail.local.

Does Fail2ban work with UFW?

Yes. Set banaction = ufw in your jail.local to use UFW instead of iptables directly. Fail2ban will create and remove UFW rules automatically.

How many IPs does Fail2ban typically ban?

On a public-facing server with SSH on port 22, Fail2ban typically bans 50-500 IPs per day. Moving SSH to a non-standard port reduces this to near zero. Fail2ban handles thousands of bans without performance issues.

Should I use Fail2ban even with key-only SSH?

Yes. Even with password authentication disabled, Fail2ban reduces log noise, blocks reconnaissance scanning, and protects other services (web servers, email). It also frees up resources by rejecting connections at the firewall level.

Related Resources

Share this article:
Dargslan Editorial Team (Dargslan)
About the Author

Dargslan Editorial Team (Dargslan)

Collective of Software Developers, System Administrators, DevOps Engineers, and IT Authors

Dargslan is an independent technology publishing collective formed by experienced software developers, system administrators, and IT specialists.

The Dargslan editorial team works collaboratively to create practical, hands-on technology books focused on real-world use cases. Each publication is developed, reviewed, and...

Programming Languages Linux Administration Web Development Cybersecurity Networking

Stay Updated

Subscribe to our newsletter for the latest tutorials, tips, and exclusive offers.