Fail2Ban Setup – Automatically Block Hackers on Your Server

πŸ“Œ Quick Summary
    🧱 Table of Contents

    If your server is exposed to the internet, it’s constantly being attacked β€” especially on SSH, web logins, and APIs.

    πŸ‘‰ That’s where Fail2Ban comes in.

    It automatically:

    • Detects suspicious activity
    • Blocks malicious IPs
    • Protects your server in real-time

    πŸ”Ή Install Fail2Ban (Ubuntu)

    sudo apt update
    sudo apt install fail2ban -y

    Enable it:

    sudo systemctl enable fail2ban
    sudo systemctl start fail2ban

    πŸ”Ή Basic Configuration

    Copy default config:

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

    Edit:

    sudo nano /etc/fail2ban/jail.local

    πŸ”Ή Protect SSH

    Find [sshd] section:

    [sshd]
    enabled = true
    port = ssh
    maxretry = 5
    bantime = 3600
    findtime = 600

    βœ” This means:

    • 5 failed attempts β†’ ban
    • Ban lasts 1 hour

    πŸ”Ή Protect Nginx (Optional)

    [nginx-http-auth]
    enabled = true
    
    [nginx-botsearch]
    enabled = true

    πŸ”Ή Restart Fail2Ban

    sudo systemctl restart fail2ban

    πŸ”Ή Check Status

    sudo fail2ban-client status

    Check SSH jail:

    sudo fail2ban-client status sshd

    πŸ”Ή Unban IP

    sudo fail2ban-client set sshd unbanip <IP>

    πŸ”Ή Pro Tips (Advanced)

    • πŸ”₯ Use longer bans (e.g. 24h)
    • πŸ” Combine with firewall (UFW / iptables)
    • πŸ“© Add email or Telegram alerts
    • 🧠 Whitelist your IP (VERY IMPORTANT)

    πŸ”Ή Common Mistakes

    ❌ Editing jail.conf instead of jail.local
    ❌ Locking yourself out (always whitelist your IP!)
    ❌ Not restarting service after changes

    πŸ”Ή Conclusion

    With Fail2Ban:

    • Your server actively defends itself
    • Brute-force attacks are stopped instantly
    • Security becomes automated

    Leave a comment

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