Modello di minaccia
Un VPS appena nato riceve in poche ore tentativi di brute-force su SSH, HTTP, MySQL. Due strumenti riducono il rumore al 99%:
- UFW: firewall — chiude tutte le porte tranne quelle che ti servono
- fail2ban: legge i log e banna IP che provano password sbagliate
Configurare UFW
sudo apt install -y ufw sudo ufw default deny incoming sudo ufw default allow outgoing sudo ufw allow 22/tcp comment 'SSH' sudo ufw allow 80/tcp comment 'HTTP' sudo ufw allow 443/tcp comment 'HTTPS' sudo ufw enable sudo ufw status verbose
Sequenza importante: prima apri SSH, poi abilita il firewall — altrimenti ti chiudi fuori dalla sessione corrente.
Limitare SSH per IP
Se accedi solo da casa con IP statico:
sudo ufw delete allow 22/tcp sudo ufw allow from 93.x.y.z to any port 22 proto tcp
Se l'IP è dinamico, usa una VPN (Wireguard, Tailscale) ed esponi SSH solo dentro la rete VPN.
Installare fail2ban
sudo apt install -y fail2ban sudo systemctl enable --now fail2ban
Configurazione locale
Mai modificare jail.conf. Crea un override:
sudo tee /etc/fail2ban/jail.local <<EOF [DEFAULT] bantime = 1h findtime = 10m maxretry = 5 ignoreip = 127.0.0.1/8 ::1 192.168.0.0/16 [sshd] enabled = true port = ssh logpath = %(sshd_log)s backend = systemd EOF sudo systemctl restart fail2ban
Dopo 5 tentativi falliti in 10 minuti, l'IP viene bannato per 1 ora. Le tue subnet di casa sono in whitelist.
Verificare i ban
sudo fail2ban-client status sudo fail2ban-client status sshd
Vedi quanti IP sono attualmente bannati e l'elenco totale.
Sbloccare un IP per errore
sudo fail2ban-client set sshd unbanip 1.2.3.4
Estensione ad altri servizi
Aggiungi jail per Nginx, Postfix, Dovecot. Esempio Nginx auth fallita:
[nginx-http-auth] enabled = true filter = nginx-http-auth port = http,https logpath = /var/log/nginx/error.log
Hardening SSH aggiuntivo
Su /etc/ssh/sshd_config:
PermitRootLogin no PasswordAuthentication no ChallengeResponseAuthentication no AllowUsers tuoutente
Solo chiavi RSA/Ed25519, niente password. Rendi UFW + fail2ban + chiavi una baseline per ogni nuovo server.