Using Fail2Ban to Protect Against Brute Force Attacks
Olatunde salami

Olatunde salami @salamilinux

About: Cloud Engineer | DevOps| Linux | Automation

Location:
Ibadan Nigeria
Joined:
Apr 7, 2025

Using Fail2Ban to Protect Against Brute Force Attacks

Publish Date: May 17
1 0

Table Of Content

Introduction

Brute force attacks are like a thief trying every key on a ring to unlock your server. They’re relentless, automated, and can overwhelm your system if left unchecked. Enter Fail2Ban, a lightweight, open-source tool that acts like a vigilant security guard, banning malicious IPs after too many failed login attempts.

In this article, we’ll walk through how to set up Fail2Ban to protect your server, with practical examples and tips to keep things engaging.

Let’s lock down your server! 🔒


Why Fail2Ban?

Imagine this: your SSH server logs show hundreds of login attempts from a single IP in minutes. Without protection, your server could be compromised, or at least slowed to a crawl.

Fail2Ban monitors logs, detects suspicious patterns, and temporarily bans IPs using firewall rules (like iptables or firewalld). It’s simple, effective, and works for services like SSH, Apache, Nginx, and more.

Fun Fact: Fail2Ban has been around since 2004 and is still a go-to tool for sysadmins. Its simplicity is its superpower!


Step 1: Installing Fail2Ban

Let’s get Fail2Ban up and running. Most Linux distros make this a breeze.

On Ubuntu/Debian:

sudo apt update
sudo apt install fail2ban

Enter fullscreen mode Exit fullscreen mode

On CentOS/RHEL:

sudo yum install epel-release
sudo yum install fail2ban
Enter fullscreen mode Exit fullscreen mode

After installation, start and enable Fail2Ban:

sudo systemctl start fail2ban
sudo systemctl enable fail2ban
Enter fullscreen mode Exit fullscreen mode

Interactive Tip: Run fail2ban client status in your terminal. What do you see? Share your output in the comments it’s a great way to confirm your setup!

Step 2: Configuring Fail2Ban

Fail2Ban’s magic happens in its config files, located in /etc/fail2ban/.

Note: Don’t edit jail.conf directly it might get overwritten. Instead, create a jail.local file or use the jail.d/ directory for custom rules.

Example: jail.local for SSH

[DEFAULT]
bantime  = 600
findtime = 600
maxretry = 5

[sshd]
enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 3
bantime  = 3600
Enter fullscreen mode Exit fullscreen mode

What’s Happening Here?

  • bantime: How long (in seconds) an IP is banned (600 = 10 minutes).

  • findtime: The time window (in seconds) to look for failed attempts.

  • maxretry: Number of failed attempts before banning.

  • [sshd]: Protects the SSH service, using the sshd filter and monitoring /var/log/auth.log.

Engage with This: Try tweaking maxretry to 2 or bantime to 7200 (2 hours). What’s the trade off? Too strict, and you might lock out legit users; too lenient, and attackers slip through. Share your thoughts below!

Step 3: Testing Your Setup

Let’s simulate a brute force attack (safely, of course).

From another machine, try logging into your server via SSH with incorrect credentials multiple times:

ssh user@your-server-ip
Enter fullscreen mode Exit fullscreen mode

After hitting the maxretry limit, check Fail2Ban status:

sudo fail2ban-client status sshd
Enter fullscreen mode Exit fullscreen mode

You should see the offending IP in the Banned IP list.

To unban an IP (for testing):

sudo fail2ban-client unban <IP_ADDRESS>

Enter fullscreen mode Exit fullscreen mode

Interactive Challenge: Set up Fail2Ban on a test server and try this. How many attempts did it take to get banned? Post your results!

Step 4: Protecting Other Services

Fail2Ban isn’t just for SSH. Want to secure your web server? Enable jails for Apache or Nginx.

Example: Protect WordPress Login
[wordpress]
enabled  = true
port     = http,https
filter   = wordpress
logpath  = /var/log/apache2/access.log
maxretry = 3
bantime  = 3600
Enter fullscreen mode Exit fullscreen mode

You’ll need to create a custom filter file, e.g., /etc/fail2ban/filter.d/wordpress.conf:

[Definition]
failregex = ^.*wp-login\.php.* 401
ignoreregex =
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Check Fail2Ban’s GitHub for pre-made filters for popular services.

Got a service you want to protect? Drop it in the comments, and let’s brainstorm a filter together!

Step 5: Monitoring and Fine Tuning

Fail2Ban logs to /var/log/fail2ban.log. To monitor activity:

sudo tail -f /var/log/fail2ban.log
Enter fullscreen mode Exit fullscreen mode

Adjust maxretry or findtime if you see too many false positives.

Want notifications? Integrate with email, Slack, etc., via the action directive in jail.local.

Engagement Prompt: How do you monitor your server security? Do you pair Fail2Ban with tools like Logwatch, Prometheus, or Grafana? Share your stack!

Common Pitfalls (and How to Avoid Them)
Enter fullscreen mode Exit fullscreen mode

Wrong Log Path
Ensure logpath matches your system’s log location (e.g., /var/log/secure on CentOS).

Overly Aggressive Bans
Test your settings to avoid locking out legitimate users.

Firewall Conflicts

Ensure Fail2Ban’s firewall rules don’t conflict with other firewall tools.

Quick Poll: What’s the biggest security oops you’ve made?
No judgment mine was leaving port 22 open with a weak password.
Share yours below!

Wrapping Up: Stay One Step Ahead

Fail2Ban is a powerful ally against brute force attacks, but it’s not a silver bullet. Combine it with:

  • Strong passwords

  • SSH key authentication

  • Regular log monitoring

...for a robust defense. Your server deserves it!

Summary

Fail2Ban is an open source intrusion prevention tool that helps protect Linux servers from brute force attacks. It works by monitoring log files for suspicious activity (e.g., repeated failed login attempts) and automatically bans offending IP addresses using firewall rules like iptables or firewalld.

Key Steps to Use Fail2Ban:

1.Installation

  • Available via package managers (apt for Ubuntu/Debian, yum for CentOS/RHEL).

  • Enable and start the Fail2Ban service.

2.Configuration

  • Customize rules using jail.local or files in jail.d/.

  • Define parameters like bantime, findtime, and maxretry.

3.Testing

  • Simulate failed SSH logins and verify bans using fail2ban client status.

  • Unban test IPs if needed.

4.Protecting Other Services

  • Extend Fail2Ban to web servers (e.g., Apache, Nginx) and applications like WordPress by writing custom filters.

5.Monitoring & Fine Tuning

  • View logs in /var/log/fail2ban.log.

  • Adjust thresholds to avoid false positives.

  • Set up alerts for better visibility.

I would love to hear your thoughts, experiences, or tips about Linux!
Feel free to share in the comments and join the conversation.
Connect with me on LinkedIn !

#30DaysLinuxChallenge #CloudWhistler #RedHat #Cloudsecurity #DevOps #Linux #OpenSource #CloudComputing #RedHatEnterpriseLinux #SystemLogs #EnterpriseIT #Observability #Logging #SysAdmin #Automation #CloudEngineer #TechForBusiness #ITSupport #SRE #CloudOps

Comments 0 total

    Add comment