How to Setup Fail2ban on the Raspberry Pi?

Fail2ban is a popular intrusion prevention software framework that protects computer servers from brute-force attacks. It works by monitoring log files for signs of an attack, banning IP addresses that show malicious signs, and protecting services. Fail2ban is easy to install on the Raspberry Pi and provides robust security for your devices.

How to Setup Fail2ban on the Raspberry Pi?

Overview of Fail2ban

Fail2ban monitors logs for repeated failed login attempts and bans offending IP addresses temporarily. This prevents attackers from continuously trying passwords or exploits. Some key features of Fail2ban include:

  • Monitoring services like SSH, HTTP, SMTP, IMAP, POP3, etc.
  • Banning IP addresses with firewall rules
  • Customizable banning timeframes and thresholds
  • Jail configurations for services
  • Python framework extensible with custom scripts

Fail2ban is available in most Linux distributions and works on the Raspberry Pi platform. Installing and configuring it takes only a few minutes.

Prerequisites for Using Fail2ban

Before installing Fail2ban, ensure your Raspberry Pi environment meets these requirements:

  • Raspberry Pi OS (previously called Raspbian)
  • Python 2 or 3
  • iptables firewall rules enabled
  • Log locations of services you want to monitor

An Ethernet or WiFi connection is necessary initially to install packages on Raspberry Pi OS. Once configured, Fail2ban can run safely over SSH sessions too.

Step-by-Step Guide to Install Fail2ban

Follow these steps to install and configure Fail2ban on your Raspberry Pi:

Install Fail2ban Package

Log in to Raspberry Pi remotely or via monitor/keyboard, then install the Fail2ban package:

sudo apt update
sudo apt install fail2ban -y

This installs the fail2ban service, default configurations, and initiation scripts.

Copy the Default Config File

To customize Fail2ban, start by copying its default configuration file:

sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

This creates jail.local which overrides the default settings in jail.conf.

Set Ban Times

Open the jail.local file:

sudo nano /etc/fail2ban/jail.local

Within the [DEFAULT] section set bantime, findtime, maxretry to:

bantime = 1800  

findtime = 1800  

maxretry = 5

This bans any IP for 30 minutes (1800 seconds) if it fails to login correctly 5 times within 30 minutes. Tweak as necessary.

Enable SSH Monitoring

Scroll down to the [sshd] section and add:

enabled = true

This enables Fail2ban monitoring on SSH logins.

Save Config & Restart Service

Save changes in nano with Ctrl+X > Y > Enter.

Then restart the fail2ban service:

sudo systemctl restart fail2ban

Fail2ban is now monitoring logs and ready to block intruders!

Test Fail2ban

You can verify correct operation of Fail2ban by intentionally causing banned behavior:

ssh root@localhost

When prompted for password, enter incorrect password 5 times. On the 5th attempt, the connection will fail as Fail2ban blocks that IP address i.e. your own machine for 30 minutes as per the bantime defined.

After testing, you can lift your own ban immediately with:

sudo fail2ban-client set sshd unbanip <your_ip_here>

Any other unauthorized IPs trying repeated logins will get banned automatically by Fail2ban, preventing brute force attacks.

Adding Extra Protection with Port Knocking

An additional layer of security beyond Fail2ban can be achieved using port knocking. This technique hides services behind closed firewall ports, only “knocking” them open when correct secret knock sequences are used.

To add port knocking protection on the Raspberry Pi:

  1. Install the knockd daemon – sudo apt install knockd
  2. Choose ports to hide your SSH server behind e.g. 1234, 4096 and 7777
  3. Set up firewall rules to allow knocking to open SSH port
  4. Create /etc/knockd.conf with this content:




    sequence    = 1234,4096,7777 

    seq_timeout = 10

    command     = /sbin/iptables -A INPUT -s %IP% -p tcp –dport 22 -j ACCEPT    

    tcpflags    = syn


    sequence    = 1234,4096,7777

    seq_timeout = 10

    command     = /sbin/iptables -D INPUT -s %IP% -p tcp –dport 22 -j ACCEPT

    tcpflags    = syn

This allows your chosen sequence to open SSH access.

  1. Start knockd daemon – sudo systemctl start knockd

With port knocking + Fail2ban, your SSH server is now doubly protected on Raspberry Pi!


Installing Fail2ban provides robust intrusion protection on the Raspberry Pi. It blocks brute force login attempts and bans offenders’ IP addresses temporarily. Sensible default configurations work well for most use cases. Beyond Fail2ban, approaches like port knocking adds more security.

With these protections in place, you get logging and alerting of threats along with automatic banning of suspicious IP patterns trying to access your Pi. This lets you run Internet-exposed services more securely.

Key Takeaways

  • Fail2ban monitors service logs and bans suspicious repeated login failures
  • Easy to install on Raspberry Pi via apt and configure jails
  • Customizable banned times and max retry thresholds
  • Test protection using intentional invalid logins
  • Combine with port knocking to hide and reveal services
  • Logs threats & prevents real brute-force attacks from unknown IPs

Frequently Asked Questions

  1. What protocols does Fail2ban protect?
    Fail2ban works with any text-based network service logs that record client IP addresses on failed requests. Common examples are SSH, HTTP, SMTP, POP3, IMAP, FTP, etc.

  2. Does Fail2ban work over WiFi?
    Yes, Fail2ban functions fully over WiFi connections, Ethernet, or any other network your Pi uses. As long as logs record an IP address on incidents, Fail2ban can ban it.

  3. Can I customize banned IP duration?
    Definitely, the bantime setting under [DEFAULT] jail determines the ban duration. You can set it from 60 seconds up to years. But keep it reasonable to prevent collateral lock outs.

  4. Why set a findtime less than bantime?
    Setting findtime lower than bantime allows early detection of attacks even if previous bans have not expired yet, allowing the additional attacks to extend the ban.

  5. Will Fail2ban overwhelm my Pi resources?
    No, Fail2ban’s memory and CPU footprint is very low. It won’t strain Pi system resources even on heavy traffic servers. Make sure your SD card has spare disk space for logs.

  6. Can banned IPs hurt my Pi?
    No, banned IP addresses can’t directly harm your system. Fail2ban simply drops malicious connection requests via iptables firewall rules.

  7. What Pi models support Fail2ban?
    All Raspberry Pi models support installing Fail2ban provided they run Raspberry Pi OS. Performance differs of course – faster Pis log and parse quicker.

  8. Where does Fail2ban log banned events?
    Fail2ban logs all banned IP events and alerts via the system log accessible with journalctl. Errors go to /var/log/fail2ban.log.

  9. Why enable Fail2ban on a private home Pi?
    Even private networks are vulnerable to brute force attacks from any compromised devices connected. Fail2ban adds monitoring & auto-blocking of suspicious activity.

  10. Can I whitelist trusted IP addresses?
    Yes, edit jail.local and add IPs to the ignoreip list section to exclude them fully from monitoring.

  11. How do I unban my own IP if accidentally banned?
    Run sudo fail2ban-client set <jailname> unbanip <ip-address> with your IP & jail service name.

  12. Why not block countries instead of IP addresses?
    Geoblocking bypasses are easy with VPNs. Monitoring access patterns and blocking specific IPs allows smarter tracking of real attackers.

  13. Should I disable root SSH logins?
    Disabling root remote logins is good security practice. Fail2ban with public key authentication for a non-root user provides best security.

  14. Can attackers bypass Fail2ban?
    Attackers can try to randomize IP addresses but it is difficult to automate. Fail2ban tracking catches outliers not matching human usage patterns accurately.

  15. Will Fail2ban catch application layer DDoS attempts?
    No. Services like Cloudflare deal with HTTP floods better. Fail2ban focuses on login spamming/brute force attacks instead of bandwidth floods.

  16. Can I set custom ban thresholds per service jail?
    Yes, you can override the global maxretry value with maxretry = <number> setting under each [SERVICE] jail block in jail.local.

  17. Why enable Fail2ban on HTTP services?
    It catches fake bots brute-forcing paths and common exploits. Many attacks try known CMS, WordPress and PHP vulnerabilities through password guessing.

  18. Will Fail2ban break my PiHole adblocking?
    No. Carefully test PiHole after adding Fail2ban monitoring. But PiHole logs rarely trigger bans during normal usage.

  19. Can I get email alerts for banned events?
    Yes, Fail2ban integrates with most mail services to send alerts on bans. Customize notification settings in the [DEFAULT] section.

  20. Is Fail2ban resource intensive on web servers?
    No, the lightweight daemon uses little memory and CPU even on very busy servers. Just ensure your Pi has enough disk space for log data when traffic scales up.

Leave a Comment