Skip to content

SSH Hardening

Durcissement configuration SSH pour accès sécurisé au serveur Proxmox.

Configuration Actuelle

Host : Proxmox VE 9.1 (192.168.1.21)
Port : 22 (standard)
Auth : Password (root)
Fichier : /etc/ssh/sshd_config

# Status SSH
systemctl status sshd

# Vérifier config
cat /etc/ssh/sshd_config | grep -v "^#" | grep -v "^$"

Paramètres Par Défaut

Port 22
PermitRootLogin yes
PasswordAuthentication yes
PubkeyAuthentication yes

⚠️ Risques :

  • Port standard 22 (scanné massivement)
  • Root login autorisé (cible privilégiée)
  • Password authentication (brute-force possible)

Étape 1 : SSH Key Authentication

Génération Clé (depuis workstation)

# Sur machine cliente
ssh-keygen -t ed25519 -C "admin@homelab"

# Résultat
~/.ssh/id_ed25519 (private key - JAMAIS partager)
~/.ssh/id_ed25519.pub (public key)

Pourquoi ed25519 ?

  • Plus sécurisé que RSA 2048
  • Plus rapide
  • Clés plus courtes

Copie Clé sur Serveur

# Méthode 1 : ssh-copy-id
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@192.168.1.21

# Méthode 2 : Manuelle
cat ~/.ssh/id_ed25519.pub | ssh root@192.168.1.21 \
  "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

# Permissions correctes sur serveur
ssh root@192.168.1.21 "chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"

Test Connexion

# Connexion avec clé (doit fonctionner sans password)
ssh -i ~/.ssh/id_ed25519 root@192.168.1.21

# Si OK → Étape suivante

Étape 2 : Durcir /etc/ssh/sshd_config

Backup Config

cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup.$(date +%Y%m%d)

Modifications Recommandées

nano /etc/ssh/sshd_config

Changements :

# Port non-standard (réduire scans automatiques)
Port 2222

# Disable root login avec password (key-only)
PermitRootLogin prohibit-password

# Disable password authentication entièrement
PasswordAuthentication no

# Pubkey seulement
PubkeyAuthentication yes

# Disable empty passwords
PermitEmptyPasswords no

# Limite tentatives
MaxAuthTries 3

# Timeout connexion
ClientAliveInterval 300
ClientAliveCountMax 2

# Disable X11 forwarding (non nécessaire)
X11Forwarding no

# Protocol version 2 seulement
Protocol 2

# Logging
SyslogFacility AUTH
LogLevel VERBOSE

# Allow specific users seulement
AllowUsers root

# Disable challenge response
ChallengeResponseAuthentication no

# Disable tunnel device forwarding (si non utilisé)
PermitTunnel no

# Maximum sessions simultanées
MaxSessions 3

Appliquer Changements

# Test configuration (IMPORTANT - évite lockout)
sshd -t

# Si OK (aucune erreur)
systemctl restart sshd

# Vérifier service actif
systemctl status sshd

⚠️ CRITIQUE : Garder session SSH active pendant test. Ouvrir nouvelle session dans autre terminal pour tester.

Test Nouvelle Config

# Depuis workstation - nouveau terminal
ssh -p 2222 -i ~/.ssh/id_ed25519 root@192.168.1.21

# Doit fonctionner avec clé
# Doit échouer avec password

Étape 3 : fail2ban

Protection anti-brute-force.

Installation

apt update
apt install fail2ban -y

Configuration

# Créer jail local
nano /etc/fail2ban/jail.local

Contenu :

[DEFAULT]
# Ban time 1 heure
bantime = 3600

# Find time 10 minutes
findtime = 600

# Max 3 tentatives
maxretry = 3

# Email alerts (optionnel)
destemail = admin@domain.com
sendername = Fail2Ban-Proxmox
action = %(action_mwl)s

[sshd]
enabled = true
port = 2222
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600

Démarrage

systemctl enable fail2ban
systemctl start fail2ban

# Status
fail2ban-client status
fail2ban-client status sshd

Commandes Utiles

# Liste IPs bannies
fail2ban-client status sshd

# Unban IP
fail2ban-client set sshd unbanip 192.168.1.100

# Logs
tail -f /var/log/fail2ban.log

Étape 4 : Monitoring SSH

Connexions Actives

# Users connectés
who
w

# Sessions SSH
ss -tnp | grep sshd

Historique Connexions

# Dernières connexions réussies
last | head -20

# Tentatives échouées
grep "Failed password" /var/log/auth.log | tail -20

# Tentatives connexion
grep "sshd" /var/log/auth.log | tail -50

Log Monitoring Script

#!/bin/bash
# /usr/local/bin/ssh-monitor.sh

LOG="/var/log/auth.log"
FAILED_ATTEMPTS=$(grep "Failed password" $LOG | tail -100 | wc -l)

if [ $FAILED_ATTEMPTS -gt 10 ]; then
    echo "ALERT: $FAILED_ATTEMPTS failed SSH attempts detected"
    # Send notification (mail, telegram, etc.)
fi

Configuration Client SSH

~/.ssh/config

Simplifier connexions depuis workstation :

# Sur machine cliente
nano ~/.ssh/config

Contenu :

Host proxmox
    HostName 192.168.1.21
    Port 2222
    User root
    IdentityFile ~/.ssh/id_ed25519
    ServerAliveInterval 60

Host lxc100
    HostName 192.168.1.21
    Port 2222
    User root
    IdentityFile ~/.ssh/id_ed25519
    RemoteCommand pct enter 100
    RequestTTY yes

Host lxc101
    HostName 192.168.1.21
    Port 2222
    User root
    IdentityFile ~/.ssh/id_ed25519
    RemoteCommand pct enter 101
    RequestTTY yes

Usage :

# Connexion Proxmox host
ssh proxmox

# Connexion directe LXC 100
ssh lxc100

# Plus besoin de taper "ssh root@192.168.1.21 -p 2222"

Firewall (Optionnel)

UFW Simple

# Installation
apt install ufw

# Allow SSH nouveau port
ufw allow 2222/tcp

# Allow Proxmox Web
ufw allow 8006/tcp

# Default deny
ufw default deny incoming
ufw default allow outgoing

# Enable
ufw enable

# Status
ufw status verbose

Proxmox Firewall

Alternative : utiliser firewall intégré Proxmox.

# Datacenter → Firewall → Enable

# Rules example:
IN SSH(ACCEPT) -source +lan -dport 2222
IN Proxmox(ACCEPT) -source +lan

Checklist Hardening

Minimum Viable

  • SSH key authentication configuré
  • PasswordAuthentication disabled
  • Port changé (22 → 2222)
  • fail2ban installé et actif
  • Test connexion avec clé fonctionnel

Recommandé

  • Root login disabled (créer user admin)
  • Firewall activé (UFW ou Proxmox)
  • Monitoring tentatives connexion
  • Backup /etc/ssh/sshd_config
  • Log rotation configuré

Advanced

  • 2FA authentication (Google Authenticator)
  • Port knocking
  • SSH bastions/jump hosts
  • Certificate-based authentication
  • Geo-blocking IPs externes

Troubleshooting

Locked Out

Symptôme : Impossible de se connecter après changements

Solution :

  1. Accès console Proxmox (physique ou VNC)
  2. Restore backup config :
    cp /etc/ssh/sshd_config.backup /etc/ssh/sshd_config
    systemctl restart sshd
    

fail2ban Ban Propre IP

# Unban
fail2ban-client set sshd unbanip YOUR_IP

# Whitelist permanent
nano /etc/fail2ban/jail.local
# Ajouter sous [DEFAULT]:
ignoreip = 127.0.0.1/8 192.168.1.0/24

Clé Rejetée

# Vérifier permissions
ls -la ~/.ssh/
# authorized_keys doit être 600
# .ssh doit être 700

# Debug connexion
ssh -vvv -p 2222 root@192.168.1.21

Ressources