Monday, April 6, 2026
LinuxSecurity

How to Automatically Block Suspicious IPs using iptables and Fail2Ban

How to Automatically Block Suspicious IPs using iptables and Fail2Ban
Automatically Block Suspicious IPs using iptables and Fail2Ban
16views

Your Linux server is under attack right now you just might not know it yet. Every internet-facing machine faces a constant stream of brute-force login attempts, port scans, and automated bots probing for vulnerabilities. Passwords get guessed, services get hammered, and logs fill up with thousands of failed attempts every single day.

The good news is that you do not need expensive security software to stop most of it. Two tools already available in every major Linux distribution iptables and Fail2Ban give you everything you need to detect malicious traffic and cut it off at the network level, automatically and in real time.

In this guide, we will walk through how to install and configure both tools, write a custom IP blocker script, and wire everything together into a robust, automated defense layer. Whether you manage a single VPS, a web server, or a home lab, this setup will dramatically reduce your attack surface with minimal ongoing effort.

Why Relying on Default Firewall Settings is Not Enough

A freshly installed Linux server comes with basic firewall capabilities, but without active monitoring and automated response, your defense is entirely passive. Here is what you are exposed to without iptables rules and Fail2Ban in place:

  • Brute-Force SSH Attacks — Automated scripts attempt thousands of username/password combinations per hour against your SSH port.
  • Service-Specific Exploits — Web servers, mail servers, and database services each have their own attack patterns that need targeted monitoring.
  • Slow-Rate Attacks — Some attackers deliberately pace their attempts to stay below threshold detection, making manual monitoring impractical.
  • Log Flooding — Unmitigated attacks generate enormous log volumes, masking legitimate events and making auditing harder.
  • Resource Drain — Even failed login attempts consume CPU, memory, and network bandwidth — enough to degrade server performance over time.
  • Compliance Exposure — Many regulatory frameworks require demonstrable controls to detect and respond to unauthorized access attempts.

Core Configuration Settings

Setting / ParameterPurpose
maxretry = 5Number of failures before an IP is banned
bantime = 3600Duration of the ban in seconds (3600 = 1 hour)
findtime = 600Time window in seconds during which failures are counted
logpathLog file Fail2Ban monitors for the specified jail/service
filter = sshdBuilt-in Fail2Ban filter that matches SSH failure log patterns
action = customblockPoints Fail2Ban to your custom IP blocker script for banning
nullok (PAM flag)Allows logins for users not yet enrolled in 2FA during rollout

Step-by-Step: Setting Up Automated IP Blocking

Step 1 — Install iptables and Fail2Ban

Start by making sure both tools are installed on your server. They are available in the default package repositories for all major distributions.

On Ubuntu / Debian:

On RHEL / Rocky Linux / AlmaLinux / CentOS:

Step 2 — Create the Custom IP Blocker Script

A dedicated bash script gives you a clean, reusable command for manually blocking any IP address — and later, a hook for Fail2Ban to call automatically.

Create the script file:

Paste the following content into the file:

Save and exit (CTRL+O, then CTRL+X), then make the script executable:

Test it by blocking a sample IP:

You should see: IP 192.168.1.100 has been blocked.

Confirm the rule was added to iptables:

Step 3 — Configure Fail2Ban to Monitor SSH

Fail2Ban organizes its monitoring rules into ‘jails’ — each jail watches a specific service log and responds when attack patterns are detected. We will configure the SSH jail now.

Create or edit the local jail configuration file:

For RHEL-based systems, update the logpath to match where SSH logs are stored:

Restart Fail2Ban to apply the new jail configuration:

Verify the SSH jail is active and monitoring correctly:

This command shows the number of currently banned IPs, total failure counts, and jail status. To unban a specific IP that was automatically blocked:

Step 4 — Connect Fail2Ban to Your Custom Script (Optional)

By default, Fail2Ban manages its own iptables rules internally. If you want Fail2Ban to call your custom script instead — for example, to add extra logging, send alerts, or execute additional logic — you can configure a custom action.

Create a new Fail2Ban action definition file:

Paste the following action definition:

Now update your jail.local to use this custom action:

Restart Fail2Ban to activate the custom action:

Step 5 — Make iptables Rules Persistent Across Reboots

By default, iptables rules are held in memory and are lost when the server restarts. To make your blocks survive reboots, you need to persist them.

On Ubuntu / Debian — install the persistence package and save current rules:

On RHEL / Rocky / AlmaLinux / CentOS:

Verifying Everything is Working

Before rolling this out fully, confirm the setup is behaving as expected. Here are four reliable verification checks:

  • Active Jail Status — Run sudo fail2ban-client status sshd to confirm the jail is running and showing current ban counts.
  • iptables Rule Inspection — Run sudo iptables -L -n to view all active blocking rules, including those added by both your script and Fail2Ban.
  • Deliberate Failure Test — From a secondary machine or IP, attempt to SSH with wrong credentials 5+ times. The IP should be banned and subsequent attempts should time out.
  • Log Review — Check /var/log/fail2ban.log to see ban and unban events in real time.

What Services Should You Protect?

Fail2Ban supports jails for many services beyond SSH. Here is a recommended starting point for most server environments:

ServiceJail NameWhat It Protects Against
SSHsshdBrute-force login attempts on port 22
Apache HTTPapache-authFailed authentication attempts to web app login pages
Nginxnginx-http-authRepeated 401/403 responses indicating login scanning
Postfix (Mail)postfixSMTP authentication failures and spam relay attempts
ProFTPD / vsftpdproftpd / vsftpdFailed FTP login attempts
WordPresswordpress (custom)XML-RPC abuse and wp-login brute-force attacks

Conclusion

Automated IP blocking with iptables and Fail2Ban is one of the most practical and immediately effective security controls you can add to a Linux server. It requires no ongoing management once configured — Fail2Ban silently monitors your logs, detects attacks as they happen, and updates your firewall rules without any manual intervention.

Adding a custom bash script on top of that gives you the flexibility to extend the system however you need whether that means triggering email alerts, writing to a centralized audit log, or syncing bans across a fleet of servers. The building blocks are simple, the logic is transparent, and the protection is real.

Leave a Response

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO