🛡 Hardening Ubuntu/Debian
Setup initial sécurisé : fail2ban, ufw, sshd, unattended-upgrades
bashScript
Hardening Ubuntu / Debian
Configuration sécurisée post-installation : pare-feu, SSH, fail2ban, mises à jour automatiques.
⚠️ Prérequis
- Exécuter en tant que root ou via
sudo - Avoir une connexion SSH active avec une clé avant de durcir SSH
- Tester sur une VM avant la production
Script
#!/bin/bash
hardening-ubuntu.sh — Sécurisation initiale Ubuntu/Debian
Testé sur : Ubuntu 22.04, 24.04 / Debian 11, 12
set -euo pipefail
── Configuration — ADAPTER AVANT EXÉCUTION ───────────────────
SSH_PORT=22 # Port SSH (changer si souhaité)
SSH_ALLOW_USERS="monuser" # Utilisateurs autorisés SSH
ADMIN_EMAIL="[email protected]"
UFW_ALLOWED_PORTS=(22 80 443) # Ports à ouvrir en plus de SSH
── Fonctions ─────────────────────────────────────────────────
log() { echo -e "\n\033[1;36m▶ $*\033[0m"; }
ok() { echo -e " \033[0;32m✓ $*\033[0m"; }
warn(){ echo -e " \033[0;33m⚠ $*\033[0m"; }
[[ $EUID -ne 0 ]] && { echo "Exécuter en root (sudo $0)"; exit 1; }
── 1. Mise à jour système ────────────────────────────────────
log "Mise à jour du système"
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get upgrade -y -qq
ok "Système mis à jour"
── 2. Installation des outils essentiels ────────────────────
log "Installation outils sécurité"
apt-get install -y -qq \
fail2ban ufw unattended-upgrades apt-listchanges \
auditd libpam-pwquality \
rkhunter chkrootkit \
logwatch \
curl wget git vim htop
ok "Outils installés"
── 3. Configuration SSH ──────────────────────────────────────
log "Durcissement SSH"
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%Y%m%d)
cat > /etc/ssh/sshd_config.d/99-hardening.conf << SSHEOF
Hardening SSH — généré par hardening-ubuntu.sh
Port $SSH_PORT
Protocol 2
Authentification
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PermitEmptyPasswords no
MaxAuthTries 3
LoginGraceTime 30
Restrictions
AllowUsers $SSH_ALLOW_USERS
X11Forwarding no
AllowTcpForwarding no
GatewayPorts no
PermitTunnel no
Session
ClientAliveInterval 300
ClientAliveCountMax 2
Banner /etc/ssh/banner
Logs
LogLevel VERBOSE
SyslogFacility AUTH
SSHEOF
echo "Accès autorisé. Toute activité est journalisée." > /etc/ssh/banner
sshd -t && systemctl reload sshd
ok "SSH durci (port $SSH_PORT, authentification par clé uniquement)"
── 4. Pare-feu UFW ───────────────────────────────────────────
log "Configuration pare-feu UFW"
ufw --force reset
ufw default deny incoming
ufw default allow outgoing
for port in "${UFW_ALLOWED_PORTS[@]}"; do
ufw allow "$port"/tcp
ok "Port $port/tcp ouvert"
done
ufw --force enable
ok "UFW activé"
ufw status verbose
── 5. Fail2ban ───────────────────────────────────────────────
log "Configuration Fail2ban"
cat > /etc/fail2ban/jail.local << F2BEOF
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3
destemail = $ADMIN_EMAIL
action = %(action_mwl)s
[sshd]
enabled = true
port = $SSH_PORT
filter = sshd
logpath = %(sshd_log)s
maxretry = 3
bantime = 86400
[nginx-http-auth]
enabled = false
[nginx-badbots]
enabled = false
F2BEOF
systemctl enable --now fail2ban
ok "Fail2ban configuré et actif"
── 6. Mises à jour automatiques ─────────────────────────────
log "Mises à jour de sécurité automatiques"
cat > /etc/apt/apt.conf.d/50unattended-upgrades << APTEOF
Unattended-Upgrade::Allowed-Origins {
"\${distro_id}:\${distro_codename}-security";
"\${distro_id}ESMApps:\${distro_codename}-apps-security";
"\${distro_id}ESM:\${distro_codename}-infra-security";
};
Unattended-Upgrade::AutoFixInterruptedDpkg "true";
Unattended-Upgrade::MinimalSteps "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "false";
Unattended-Upgrade::Mail "$ADMIN_EMAIL";
APTEOF
cat > /etc/apt/apt.conf.d/20auto-upgrades << AUTOEOF
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "1";
APT::Periodic::AutocleanInterval "7";
APT::Periodic::Unattended-Upgrade "1";
AUTOEOF
systemctl enable --now unattended-upgrades
ok "Mises à jour automatiques activées"
── 7. Paramètres kernel sysctl ──────────────────────────────
log "Paramètres sysctl sécurité"
cat > /etc/sysctl.d/99-hardening.conf << SYSCTL
Réseau
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.tcp_syncookies = 1
Mémoire
kernel.randomize_va_space = 2
kernel.dmesg_restrict = 1
kernel.kptr_restrict = 2
Système de fichiers
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
SYSCTL
sysctl --system &>/dev/null
ok "Paramètres kernel appliqués"
── 8. Résumé ─────────────────────────────────────────────────
echo ""
echo "═══════════════════════════════════════════"
echo " ✅ Hardening terminé"
echo " SSH Port : $SSH_PORT"
echo " Fail2ban : actif"
echo " UFW : actif"
echo " Auto-updates : actif"
echo "═══════════════════════════════════════════"
warn "Vérifier l'accès SSH AVANT de fermer la session !"
🔧 Ouvrir tools.rdr-it.com — application complète →
Plus de 40 outils AdminSys gratuits · SSL · DNS · Docker · Nginx · SSH · Mermaid · et plus