Lo que vas a construir
Un VPS seguro con firewall UFW configurado, fail2ban protegiendo SSH de ataques de fuerza bruta, y autenticación solo por llaves SSH.
Después de este tutorial, tu servidor:
- Bloqueará todo tráfico excepto SSH, HTTP y HTTPS
- Baneará automáticamente IPs que intenten fuerza bruta
- Solo aceptará login con llave SSH (no passwords)
- Tendrá updates de seguridad automáticos
Paso 1: Conecta a tu VPS
ssh root@TU_IP
Paso 2: Actualiza el sistema
apt update && apt upgrade -y
Paso 3: Crea usuario no-root
# Crear usuario
adduser tuusuario
# Darle sudo
usermod -aG sudo tuusuario
# Copiar llaves SSH
mkdir -p /home/tuusuario/.ssh
cp ~/.ssh/authorized_keys /home/tuusuario/.ssh/
chown -R tuusuario:tuusuario /home/tuusuario/.ssh
chmod 700 /home/tuusuario/.ssh
chmod 600 /home/tuusuario/.ssh/authorized_keys
Paso 4: Configura UFW (Firewall)
# Instalar UFW
apt install ufw -y
# Política default: bloquear todo entrante
ufw default deny incoming
ufw default allow outgoing
# Permitir SSH (¡IMPORTANTE ANTES DE ACTIVAR!)
ufw allow ssh
# Permitir HTTP y HTTPS
ufw allow 80/tcp
ufw allow 443/tcp
# Activar
ufw enable
# Verificar
ufw status verbose
Deberías ver:
Status: active
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
443/tcp ALLOW Anywhere
Paso 5: Instala fail2ban
apt install fail2ban -y
# Crear config local
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Edita /etc/fail2ban/jail.local:
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
banaction = ufw
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 24h
# Reiniciar
systemctl restart fail2ban
systemctl enable fail2ban
# Verificar
fail2ban-client status sshd
Paso 6: Hardening SSH
Edita /etc/ssh/sshd_config:
# Deshabilitar password
PasswordAuthentication no
# Deshabilitar root login
PermitRootLogin no
# Solo tu usuario
AllowUsers tuusuario
# Límites
MaxAuthTries 3
# Reiniciar SSH
systemctl restart sshd
Paso 7: Updates automáticos
apt install unattended-upgrades -y
dpkg-reconfigure -plow unattended-upgrades
Selecciona "Yes" para habilitar updates automáticos.
Paso 8: Verifica todo
# Firewall activo
ufw status
# fail2ban corriendo
fail2ban-client status sshd
# SSH configurado
grep -E "PasswordAuth|PermitRoot" /etc/ssh/sshd_config
# Probar login desde otra terminal ANTES de cerrar esta
ssh tuusuario@TU_IP
Troubleshooting
| Problema | Solución |
|---|---|
| No puedo conectar SSH | Usa consola web del VPS provider |
| Me baneó fail2ban | fail2ban-client set sshd unbanip TU_IP |
| UFW bloqueó todo | Consola web → ufw disable |
Checklist final
- UFW activo con solo 22, 80, 443
- fail2ban protegiendo SSH
- Login solo con llave SSH
- Root login deshabilitado
- Updates automáticos