How to Enable Fail2Ban to Block Repeated Failed Login Attempts on Linux

Enable Fail2Ban to block failed login attempts

Learn how to configure Fail2Ban to block repeated failed login attempts on Linux. Step-by-step guide with commands, configuration examples, and security tips for SSH and other services.

Table of Contents

🔈Introduction

In today’s threat landscape, brute-force attacks are a common way attackers attempt to gain unauthorized access to Linux servers. A simple and effective defense against this type of attack is Fail2Ban — a log-monitoring tool that scans for suspicious activity and temporarily bans malicious IP addresses.

This guide walks you through how to enable and configure Fail2Ban to block repeated failed login attempts, specifically for services like SSH, but applicable to others like Apache, Postfix, and nginx.


🔍 What is Fail2Ban?

Fail2Ban is an open-source intrusion prevention tool that protects Linux systems from brute-force attacks by monitoring log files and dynamically updating firewall rules to ban IPs exhibiting malicious behavior.

Fail2Ban works by:

  • Scanning log files for predefined failure patterns (like failed login attempts)
  • Triggering bans when thresholds are exceeded
  • Automatically unblocking IPs after a cooldown period (optional)

🖥️ Supported Platforms

Fail2Ban is supported on most major Linux distributions:

DistributionInstallation Method
Ubuntu/Debianapt install fail2ban
CentOS/RHEL/Fedoradnf install fail2ban
Arch Linuxpacman -S fail2ban

📦 Step 1: Install Fail2Ban

▶️ On Debian/Ubuntu

				
					sudo apt update
				
			
				
					sudo apt install fail2ban -y
				
			

▶️ On CentOS/RHEL/Fedora

				
					sudo dnf install fail2ban -y
				
			

🔧 Step 2: Enable and Start the Fail2Ban Service

After installation, enable and start the Fail2Ban service so it persists across reboots.

				
					sudo systemctl enable --now fail2ban
				
			

Check the service status:

				
					sudo systemctl status fail2ban
				
			

🛠️ Step 3: Configure Fail2Ban to Block Repeated Failed Login Attempts

Fail2Ban uses configuration jails to define which services to protect. These jails specify which log file to monitor, how many failures to tolerate, and how long to ban offending IPs.

⚠️ Never edit /etc/fail2ban/jail.conf directly. Instead, create /etc/fail2ban/jail.local to override settings safely.

▶️ Create/Edit jail.local:

				
					sudo vim /etc/fail2ban/jail.local
				
			

Add the following configuration for SSH protection:

				
					[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log   # For Ubuntu/Debian
# logpath = /var/log/secure   # For CentOS/RHEL
maxretry = 5
findtime = 600
bantime = 3600
				
			

💬 Explanation of Parameters:

SettingDescription
enabledActivates the SSH jail
portThe SSH port (default is 22; change if custom)
filterReferences a regex filter in /etc/fail2ban/filter.d/sshd.conf
logpathPath to SSH log file
maxretryNumber of failed attempts before banning an IP
findtimeTime window (in seconds) to evaluate failures
bantimeDuration (in seconds) to ban the IP address

🔁 Step 4: Restart Fail2Ban to Apply Changes

				
					sudo systemctl restart fail2ban
				
			

🔍 Step 5: Monitor Fail2Ban Status and Banned IPs

Check the overall Fail2Ban status:

				
					sudo fail2ban-client status
				
			

Check the status of the SSH jail:

				
					sudo fail2ban-client status sshd
				
			

Expected output:

				
					Status for the jail: sshd
|- Filter
|  |- Currently failed: 1
|  |- Total failed: 6
|  `- File list: /var/log/auth.log
`- Actions
   |- Currently banned: 1
   `- Banned IP list: 192.168.1.100
				
			

🧯 Step 6: Unban an IP Address (if necessary)

Sometimes a legitimate user may be banned. To unban:

				
					sudo fail2ban-client set sshd unbanip 192.168.1.100
				
			

🔐 Optional: Permanently Ban Persistent Offenders

To make bans permanent:

				
					bantime = -1
				
			
⚠️ Use this with caution. Permanent bans are powerful but may unintentionally affect users behind shared IPs.

📈 Comparison Table: Fail2Ban Settings Example

ParameterDefault ValueRecommended ValueDescription
maxretry53–5Max failures before ban
findtime600 (10 min)300–600Timeframe to track failures
bantime600 (10 min)1800–3600How long the IP is banned
bantime = -1N/AOptionalPermanently ban IPs

🛠️ Advanced: Protect Other Services with Fail2Ban

Fail2Ban supports many services. Here’s how to enable jails for nginx, Postfix, or Dovecot:

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

🔔 Optional: Enable Email Notifications for Bans

To receive email alerts when an IP is banned. In your jail.local:

				
					destemail = admin@yourdomain.com
sender = fail2ban@yourdomain.com
action = %(action_mwl)s
				
			
💡 You must have sendmail or a configured SMTP server for this to work

🔥 Firewall Integration

Fail2Ban works with iptables and firewalld to block IPs.

▶️ Check iptables rules

				
					sudo iptables -L -n
				
			

▶️ If using firewalld

				
					sudo firewall-cmd --state
				
			

Ensure your firewall is active so bans are enforced.


📝 Summary Checklist

TaskCommand/Path
Install Fail2Bansudo apt install fail2ban
Enable Fail2Bansudo systemctl enable fail2ban
Configure SSH Jail/etc/fail2ban/jail.local
Restart Fail2Bansudo systemctl restart fail2ban
Check Ban Statussudo fail2ban-client status sshd
Unban IPsudo fail2ban-client set sshd unbanip IP
Enable Notifications (Optional)Add action = %(action_mwl)s in jail.local

🎯 Final Thoughts

Fail2Ban is one of the simplest and most powerful tools for protecting Linux systems from brute-force attacks. By monitoring log files and applying automated IP bans, you reduce the attack surface of your servers significantly — all with minimal system overhead.

If you’re running a public-facing Linux server (especially with SSH exposed), configuring Fail2Ban should be part of your baseline hardening process.

Did you find this article helpful? Your feedback is invaluable to us! Feel free to share this post with those who may benefit, and let us know your thoughts in the comments section below.


📕 Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *