- Offizieller Beitrag
So für frisch installierten Server ein kleines Script zur Sicherheit.
Alles mit besten Wissen und Gewissen zusammen gebaut.
Verbessert gerne mein Script falls ihr was findet das nicht passt.
Bin offen für alle Positiven wie Negativen punkten.
Code
#!/usr/bin/env bash
#
# Server Hardening & Security Setup Script (mit interaktivem SSH-Key Check)
#
set -euo pipefail
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${GREEN}=== Start: Security & Hardening Setup ===${NC}\n"
# 1. Root-Check
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Fehler: Bitte führen Sie dieses Skript als root oder mit sudo aus.${NC}"
exit 1
fi
# 2. Pakete aktualisieren & Basis-Tools installieren
echo -e "${YELLOW}[1/5] Aktualisiere Paketlisten und installiere Sicherheitstools...${NC}"
apt update
apt install -y ufw fail2ban rkhunter unattended-upgrades curl
# 3. UFW Firewall konfigurieren
echo -e "${YELLOW}[2/5] Konfiguriere UFW Firewall...${NC}"
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp comment 'SSH'
ufw allow 80/tcp comment 'HTTP'
ufw allow 443/tcp comment 'HTTPS'
ufw --force enable
echo -e "${GREEN}✓ UFW Firewall ist aktiv.${NC}"
# 4. Fail2ban konfigurieren
echo -e "${YELLOW}[3/5] Konfiguriere Fail2ban für SSH und GitLab...${NC}"
cat << 'EOF' > /etc/fail2ban/jail.d/gitlab.conf
[sshd]
enabled = true
port = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s
maxretry = 3
findtime = 600
bantime = 3600
[gitlab-auth]
enabled = true
port = http,https
filter = gitlab-auth
logpath = /var/log/gitlab/gitlab-rails/application_json.log
maxretry = 5
findtime = 600
bantime = 3600
EOF
cat << 'EOF' > /etc/fail2ban/filter.d/gitlab-auth.conf
[Definition]
failregex = ^.*"severity":"INFO","time":"[^"]+","key":"authentication","message":"Failed login for.*"remote_ip":"<HOST>"
ignoreregex =
EOF
systemctl restart fail2ban
systemctl enable fail2ban
echo -e "${GREEN}✓ Fail2ban konfiguriert und neu gestartet.${NC}"
# 5. rkhunter & Unattended-Upgrades
echo -e "${YELLOW}[4/5] Konfiguriere rkhunter & Unattended-Upgrades...${NC}"
sed -i 's/^UPDATE_MIRRORS=.*/UPDATE_MIRRORS=1/' /etc/rkhunter.conf || true
sed -i 's/^MIRROR_MODE=.*/MIRROR_MODE=0/' /etc/rkhunter.conf || true
sed -i 's|^#\?WEB_CMD=.*|WEB_CMD="curl -s -S"|' /etc/rkhunter.conf || true
if [ -f /etc/default/rkhunter ]; then
sed -i 's/APT_AUTOGEN="false"/APT_AUTOGEN="true"/' /etc/default/rkhunter
sed -i 's/CRON_DAILY_RUN="false"/CRON_DAILY_RUN="true"/' /etc/default/rkhunter
fi
rkhunter --propupd --quiet || true
cat << 'EOF' > /etc/apt/apt.conf.d/20auto-upgrades
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
EOF
echo -e "${GREEN}✓ rkhunter & Automatische Sicherheitsupdates eingerichtet.${NC}"
# 6. Interaktiver SSH-Key Check & Hardening
echo -e "\n${YELLOW}[5/5] Prüfe SSH-Keys und Konfiguration...${NC}"
AUTH_KEYS="/root/.ssh/authorized_keys"
mkdir -p /root/.ssh
chmod 700 /root/.ssh
touch "$AUTH_KEYS"
chmod 600 "$AUTH_KEYS"
# Prüfen, ob bereits Keys eingetragen sind
KEY_COUNT=$(grep -c -v '^[[:space:]]*$' "$AUTH_KEYS" || true)
if [ "$KEY_COUNT" -gt 0 ]; then
echo -e "${GREEN}Es wurden bereits $KEY_COUNT SSH-Key(s) in $AUTH_KEYS gefunden.${NC}"
else
echo -e "${RED}WARNUNG: Es ist aktuell KEIN SSH-Schlüssel in $AUTH_KEYS eingetragen!${NC}"
fi
echo -e "\nWas möchten Sie tun?"
echo "1) Öffentlichen SSH-Key (Public Key) jetzt einfügen"
echo "2) Vorhandenen Key nutzen / Manuell später hinzufügen"
echo "3) SSH-Passwort-Login vorerst AKTIV lassen (Sicherheitsmodus)"
read -p "Auswahl [1-3]: " SSH_CHOICE
case $SSH_CHOICE in
1)
read -p "Fügen Sie Ihren Public Key ein (ssh-ed25519 / ssh-rsa ...): " USER_KEY
if [ -n "$USER_KEY" ]; then
echo "$USER_KEY" >> "$AUTH_KEYS"
echo -e "${GREEN}✓ SSH-Key erfolgreich zu $AUTH_KEYS hinzugefügt.${NC}"
fi
DISABLE_PASS="yes"
;;
2)
DISABLE_PASS="yes"
;;
3)
DISABLE_PASS="no"
echo -e "${YELLOW}Passwort-Login bleibt vorerst aktiviert.${NC}"
;;
*)
DISABLE_PASS="no"
;;
esac
# SSH-Hardening anwenden, falls gewünscht
SSHD_CONFIG="/etc/ssh/sshd_config"
if [ -f "$SSHD_CONFIG" ]; then
cp "$SSHD_CONFIG" "${SSHD_CONFIG}.bak_$(date +%F)"
if [ "$DISABLE_PASS" = "yes" ]; then
sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication no/' "$SSHD_CONFIG"
sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin prohibit-password/' "$SSHD_CONFIG"
echo -e "${GREEN}✓ Passwort-Login deaktiviert. SSH-Login nur noch mit Key möglich!${NC}"
fi
sed -i 's/^#\?PermitEmptyPasswords.*/PermitEmptyPasswords no/' "$SSHD_CONFIG"
systemctl reload ssh || systemctl reload sshd || true
fi
echo -e "\n${GREEN}=== Setup erfolgreich abgeschlossen! ===${NC}"
Alles anzeigen
![]()