Date Created: 2024-10-26
By: 16BitMiker
[ BACK.. ]
In todayโs increasingly hostile internet landscape, brute-force attacks on exposed services like SSH are constant. A simple, effective defense is Fail2Banโa tool that monitors logs and automatically bans malicious IPs.
This guide walks you through a complete, script-based Fail2Ban installation and configuration for Debian 12, with clear commentary and 2024 best practices baked in. Ideal for sysadmins, security-focused developers, and anyone managing Linux servers.
Fail2Ban is an intrusion prevention framework that monitors log files and bans IP addresses based on suspicious behavior patternsโmost commonly repeated failed login attempts.
โ Use cases include protecting:
SSH
FTP
Web logins (Apache, Nginx)
Mail servers (Postfix, Dovecot)
It integrates with firewalls like iptables or nftables to temporarily block malicious IPs.
Below is the full Bash script designed to:
Remove any existing Fail2Ban installation
Install required packages
Configure Fail2Ban securely
Start services and check configuration
Output logs and diagnostic info
โx
#
# _nnnn_
# dGGGGMMb
# @p~qp~~qMb
# M|@||@) M|
# @,----.JM|
# JS^\__/ qKL
# dZP qKRb
# dZP qKKb
# fZP SMMb
# HZM MMMM
# FqM MMMM
# __| ". |\dS"qML
# | `. | `' \Zq
# _) \.___.,| .'
# \____ )MMMMMP| .'
# `-' `--'
#
# Debian 12 Fail2Ban Clean Install Script
# Author: 16BitMiker (v2024-10-27)
#
# ~~~~~~~~~~~~~~~~ BEGIN
# Enable debugging output
set -x
# Allow script to continue on errors
set +e
# Define simple error logging function
log_error() {
echo "ERROR: $1" >&2
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ REMOVAL
echo "Checking if Fail2Ban is installed..."
if dpkg -s fail2ban &> /dev/null; then
echo "Fail2Ban is installed. Proceeding with removal..."
echo "Stopping and disabling Fail2Ban..."
sudo systemctl stop fail2ban || log_error "Failed to stop Fail2Ban"
sudo systemctl disable fail2ban || log_error "Failed to disable Fail2Ban"
echo "Purging Fail2Ban package..."
sudo DEBIAN_FRONTEND=noninteractive apt-get purge --auto-remove fail2ban -y || log_error "Failed to purge Fail2Ban"
else
echo "Fail2Ban not found. Skipping removal."
fi
echo "Removing residual Fail2Ban configuration..."
sudo rm -rf /etc/fail2ban 2>/dev/null || log_error "Could not remove /etc/fail2ban"
sudo rm -f /var/lib/fail2ban/fail2ban.sqlite3 2>/dev/null || log_error "Could not remove fail2ban.sqlite3"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ INSTALLATION
echo "Updating package lists..."
sudo DEBIAN_FRONTEND=noninteractive apt-get update -y || log_error "Failed to update packages"
echo "Installing Fail2Ban and rsyslog..."
sudo DEBIAN_FRONTEND=noninteractive apt-get install fail2ban rsyslog -y || log_error "Failed to install packages"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CONFIGURATION
echo "Creating Fail2Ban configuration..."
sudo mkdir -p /etc/fail2ban || log_error "Failed to create config directory"
sudo tee /etc/fail2ban/jail.local > /dev/null <<EOF || log_error "Failed to write jail.local"
[DEFAULT]
bantime = 86400
findtime = 3600
maxretry = 5
ignoreip = 127.0.0.1/8 ::1
backend = systemd
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 5
EOF
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SERVICE MANAGEMENT
echo "Ensuring rsyslog is running..."
sudo systemctl start rsyslog || log_error "Failed to start rsyslog"
sudo systemctl enable rsyslog || log_error "Failed to enable rsyslog"
echo "Starting and enabling Fail2Ban..."
sudo systemctl start fail2ban || log_error "Failed to start Fail2Ban"
sudo systemctl enable fail2ban || log_error "Failed to enable Fail2Ban"
# Allow Fail2Ban time to initialize
sleep 5
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ STATUS CHECKS
echo "==== Fail2Ban Service Status ===="
sudo systemctl status fail2ban --no-pager || log_error "Could not get Fail2Ban status"
echo "==== SSH Jail Status ===="
sudo fail2ban-client status sshd || log_error "Could not get SSH jail status"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ LOGS
echo "==== Last 50 Fail2Ban Log Entries ===="
sudo journalctl -u fail2ban -n 50 --no-pager || log_error "Could not read Fail2Ban logs"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ TROUBLESHOOTING
echo "==== Testing Fail2Ban Configuration ===="
sudo fail2ban-client -t || log_error "Configuration test failed"
echo "==== Checking for auth.log ===="
ls -l /var/log/auth.log || log_error "/var/log/auth.log not found"
echo "==== Checking Fail2Ban Socket ===="
ls -l /var/run/fail2ban/fail2ban.sock || log_error "Fail2Ban socket missing"
echo "==== rsyslog Service Status ===="
sudo systemctl status rsyslog --no-pager || log_error "Could not get rsyslog status"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END
# Disable debugging output
set +x
echo "โ
Fail2Ban installation and configuration complete."
echo "Review any ERROR messages above for issues."
Parameter | Value | Purpose |
---|---|---|
bantime | 86400 seconds | Ban duration (24h) |
findtime | 3600 seconds | Look-back window for failures (1h) |
maxretry | 5 | Allowed failures before banning |
ignoreip | 127.0.0.1/8 ::1 | Prevent banning localhost |
backend | systemd | Use systemd journal instead of logfiles |
SSH jail settings are tuned for secure defaults and log parsing via /var/log/auth.log
.
This script is ideal for:
Initial server setup
Automated pipelines (CI, provisioning)
Troubleshooting broken Fail2Ban installations
Teaching junior sysadmins how Fail2Ban works
Itโs designed to be idempotent, safe, and informative.
Use jail.local instead of jail.conf to avoid overwrites.
Regularly run apt update && apt upgrade
to patch vulnerabilities.
Add email notifications if needed using the action
parameter.
Use systemd
backend for performance and accuracy.
Add trusted IPs to ignoreip
to prevent accidental lockouts.
Monitor logs with journalctl -u fail2ban
or via central syslog.
Periodically vacuum the Fail2Ban SQLite database:
xxxxxxxxxx
0 2 * * 0 /usr/bin/sqlite3 /var/lib/fail2ban/fail2ban.sqlite3 'VACUUM;'
Customize jails for additional services (Postfix, Nginx, etc).
Fail2Ban remains one of the most effective and lightweight tools for hardening a Debian server. This script provides a clean, repeatable way to install and validate your setup, while incorporating logging, error handling, and diagnostics.
By following these steps, youโre not just installing softwareโyouโre building a more secure and resilient server environment for 2024 and beyond. ๐
๐งฐ Fail2Ban Filters
[ BACK.. ]