Configurer Fail2ban
Fail2ban surveille les logs (SSH, Nginx, etc.) et bannit temporairement les IP qui dépassent un seuil d'échecs. Couplé à UFW ou iptables, il réduit le bruit des attaques sur un VPS Linux HolyCloud exposé sur Internet.
Prérequis
- Ubuntu/Debian avec OpenSSH et logs dans
/var/log/auth.log - UFW ou pare-feu actif (recommandé)
- Accès sudo
- SSH sécurisé par clé (pour ne pas vous bannir lors de tests — utilisez une IP stable ou
ignoreip)
Étape 1 : installation
sudo apt update
sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban
Étape 2 : configuration locale (jail.local)
Ne modifiez pas jail.conf directement ; créez une surcharge :
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
Extrait recommandé dans [DEFAULT] :
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
backend = systemd
banaction = ufw
ignoreip = 127.0.0.1/8 VOTRE_IP_FIXE/32
Remplacez VOTRE_IP_FIXE par l'IP de votre bureau ou VPN pour éviter un auto-bannissement.
Étape 3 : activer la jail SSH
Dans le même fichier, section [sshd] :
[sshd]
enabled = true
port = ssh
filter = sshd
maxretry = 3
bantime = 24h
Si SSH écoute sur un port custom (ex. 2222) :
port = 2222
Étape 4 : jail Nginx (optionnel)
Après installation de Nginx :
sudo nano /etc/fail2ban/jail.d/nginx.conf
[nginx-http-auth]
enabled = true
port = http,https
filter = nginx-http-auth
logpath = /var/log/nginx/error.log
maxretry = 5
[nginx-botsearch]
enabled = true
port = http,https
filter = nginx-botsearch
logpath = /var/log/nginx/access.log
maxretry = 2
Étape 5 : appliquer
sudo fail2ban-client -t
sudo systemctl restart fail2ban
Vérification
sudo fail2ban-client status
sudo fail2ban-client status sshd
sudo fail2ban-client banned
Simuler un bannissement (depuis une IP de test non listée dans ignoreip) :
# plusieurs échecs SSH volontaires, puis :
sudo fail2ban-client status sshd
Débannir une IP :
sudo fail2ban-client set sshd unbanip 198.51.100.10
Logs :
sudo journalctl -u fail2ban -n 40 --no-pager
Besoin d'aide HolyCloud
- Banni par erreur : console KVM →
sudo fail2ban-client set sshd unbanip VOTRE_IPou ajoutez l'IP dansignoreippuissudo systemctl restart fail2ban - Jail inactive :
sudo fail2ban-client statusdoit listersshdavec « currently banned » possible - Support HolyCloud : fournissez
fail2ban-client status sshdet extrait de/var/log/auth.log