94 lines
2.6 KiB
Bash
94 lines
2.6 KiB
Bash
#!/bin/bash
|
|
|
|
LOG_DIR="./log"
|
|
CONFIG_DIR="./config"
|
|
mkdir -p "$LOG_DIR"
|
|
mkdir -p "$CONFIG_DIR"
|
|
|
|
# === Redirection de toute la sortie standard et des erreurs vers install.log ===
|
|
INSTALL_LOG="$LOG_DIR/install.log"
|
|
exec > >(tee -a "$INSTALL_LOG") 2>&1
|
|
|
|
echo "[INFO] ===== Début du script à $(date) ====="
|
|
|
|
# === Vérification droits root ===
|
|
if [ "$EUID" -ne 0 ]; then
|
|
echo "[ERREUR] Ce script doit être lancé en tant que root (sudo)."
|
|
exit 1
|
|
fi
|
|
|
|
# === Création de l'utilisateur devops ===
|
|
echo "[INFO] Création de l'utilisateur devops..."
|
|
if id "devops" &>/dev/null; then
|
|
echo "[INFO] L'utilisateur devops existe déjà."
|
|
else
|
|
useradd -m -s /bin/bash devops
|
|
echo "devops:devops" | chpasswd
|
|
fi
|
|
|
|
# === Mise à jour des dépôts ===
|
|
echo "[INFO] Mise à jour des paquets..."
|
|
apt update && apt upgrade -y
|
|
|
|
# === Installation des paquets nécessaires ===
|
|
echo "[INFO] Installation des paquets : openssh-server, ufw, fail2ban, clamav, rsyslog"
|
|
apt install -y openssh-server ufw fail2ban clamav rsyslog
|
|
|
|
# === Configuration SSH ===
|
|
echo "[INFO] Configuration SSH..."
|
|
mkdir -p /home/devops/.ssh
|
|
cp "$CONFIG_DIR/authorized_keys" /home/devops/.ssh/authorized_keys
|
|
chown -R devops:devops /home/devops/.ssh
|
|
chmod 700 /home/devops/.ssh
|
|
chmod 600 /home/devops/.ssh/authorized_keys
|
|
|
|
# Vérification de la configuration SSH
|
|
echo "[INFO] Vérification de la configuration SSH..."
|
|
mkdir -p /run/sshd
|
|
sshd -t
|
|
if [ $? -ne 0 ]; then
|
|
echo "[ERREUR] La configuration SSH contient des erreurs."
|
|
exit 1
|
|
fi
|
|
|
|
# Redémarrage du service SSH
|
|
echo "[INFO] Redémarrage du service SSH..."
|
|
systemctl restart ssh
|
|
if [ $? -ne 0 ]; then
|
|
echo "[ERREUR] Échec du redémarrage du service SSH."
|
|
exit 1
|
|
fi
|
|
|
|
# === Configuration UFW ===
|
|
echo "[INFO] Configuration UFW..."
|
|
ufw default deny incoming
|
|
ufw default allow outgoing
|
|
ufw allow 80
|
|
ufw allow 443
|
|
ufw allow 7575
|
|
ufw --force enable
|
|
ufw status > "$CONFIG_DIR/ufw-status.txt"
|
|
|
|
# === Configuration Fail2Ban ===
|
|
echo "[INFO] Configuration Fail2Ban..."
|
|
cp "$CONFIG_DIR/jail.local" /etc/fail2ban/jail.local
|
|
systemctl restart fail2ban
|
|
if [ $? -ne 0 ]; then
|
|
echo "[ERREUR] Échec du redémarrage du service Fail2Ban."
|
|
exit 1
|
|
fi
|
|
|
|
# === Configuration ClamAV ===
|
|
echo "[INFO] Mise à jour et configuration ClamAV..."
|
|
freshclam
|
|
if [ $? -ne 0 ]; then
|
|
echo "[ERREUR] Échec de la mise à jour de ClamAV."
|
|
exit 1
|
|
fi
|
|
|
|
# === Ajout d'une tâche CRON pour ClamAV tous les lundis à 4h ===
|
|
echo "[INFO] Planification CRON ClamAV..."
|
|
(crontab -l 2>/dev/null; echo "0 4 * * 1 /usr/bin/clamscan -r --exclude-dir='^/dev' / > /var/log/clam-scan.log") | crontab -
|
|
|
|
echo "[INFO] ===== Fin du script à $(date) ====="
|