Server härten
Server härten
Section titled “Server härten”Reihenfolge beim ersten Mal: SSH härten → Updates → Firewall → Fail2ban → Backups einrichten & testen → dann erst Dienste draufpacken.
Diese Seite ist die Kurzfassung. Für ausführliche Schritt-für-Schritt-Walkthroughs siehe:
- Server erstinstallation — kompletter Ablauf vom nackten Debian zum einsatzbereiten Server
- Server Backups — verschlüsselte Off-Site-Backups mit
restic
1. Zugang härten
Section titled “1. Zugang härten”- SSH-Keys only, root-Login aus, Passwort-Auth aus
- Non-root User mit
sudoanlegen - Optional: Port ändern (nur Security through Obscurity)
Befehle:
# Neuen User anlegenadduser deployusermod -aG sudo deploy
# SSH-Config härtensudo nano /etc/ssh/sshd_config# PasswordAuthentication no# PermitRootLogin no# PermitEmptyPasswords no# X11Forwarding nosudo systemctl restart sshd2. System aktuell halten
Section titled “2. System aktuell halten”apt update && apt full-upgradeapt install unattended-upgradesdpkg-reconfigure -plow unattended-upgrades # Security-Updates automatischAutomatische Reboots für Kernel-Updates (Debian 12+/Ubuntu 22.04+):
apt install unattended-upgrades# In /etc/apt/apt.conf.d/50unattended-upgrades:# Unattended-Upgrade::Automatic-Reboot "true";3. Firewall (Standard: nur SSH + 80/443)
Section titled “3. Firewall (Standard: nur SSH + 80/443)”apt install ufwufw default deny incomingufw default allow outgoingufw allow 22/tcpufw allow 80,443/tcpufw enableufw status verbose4. Fail2ban (gegen SSH-Brute-Force)
Section titled “4. Fail2ban (gegen SSH-Brute-Force)”apt install fail2bansystemctl enable --now fail2ban
# Status & Bans anschauenfail2ban-client status sshdfail2ban-client status sshd | grep "Banned IP list"5. Zeitsync + Locale
Section titled “5. Zeitsync + Locale”timedatectl set-timezone Europe/Berlinapt install systemd-timesyncd && systemctl enable --now systemd-timesyncdtimedatectl status6. Backups (verschlüsselt, off-site)
Section titled “6. Backups (verschlüsselt, off-site)”- Cron +
restic/borg→ S3 oder Hetzner Storage Box - Test: Restore üben, nicht nur Backup laufen lassen
apt install resticrestic -r sftp:backup@backup.example.com:/srv/backups initAusführlich: Server Backups
Bonus-Härtung (nicht Pflicht, aber sinnvoll)
Section titled “Bonus-Härtung (nicht Pflicht, aber sinnvoll)”SSH: ed25519-Keys statt RSA
Section titled “SSH: ed25519-Keys statt RSA”# Auf dem Clientssh-keygen -t ed25519 -a 100 -C "deploy@workstation"ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@server
# Auf dem Server: alte RSA-Keys der authorized_keys löschenSSH: 2-Faktor mit TOTP (Google Authenticator)
Section titled “SSH: 2-Faktor mit TOTP (Google Authenticator)”apt install libpam-google-authenticatorgoogle-authenticator# Scannen mit Authenticator-App, Recovery-Codes notieren!
# /etc/ssh/sshd_config:# ChallengeResponseAuthentication yes# AuthenticationMethods publickey,keyboard-interactive# UsePAM yes
sudo systemctl restart sshdSSH: WireGuard statt offener Port 22
Section titled “SSH: WireGuard statt offener Port 22”Wenn der Server nur aus bekannten Netzen erreichbar sein muss:
apt install wireguard
# [Interface]# Address = 10.0.0.1/24# ListenPort = 51820# PrivateKey = <server-key># [Peer]# PublicKey = <client-key># AllowedIPs = 10.0.0.2/32
systemctl enable --now wg-quick@wg0
# ufw: SSH-Port dichtmachen, nur WireGuard erlaubenufw delete allow 22/tcpufw allow 51820/udpKernel-Härtung: sysctl
Section titled “Kernel-Härtung: sysctl”# IP-Spoofing-Schutznet.ipv4.conf.all.rp_filter = 1net.ipv4.conf.default.rp_filter = 1
# ICMP-Redirects ignorierennet.ipv4.conf.all.accept_redirects = 0net.ipv4.conf.default.accept_redirects = 0net.ipv4.conf.all.secure_redirects = 0net.ipv6.conf.all.accept_redirects = 0
# Source-Routing deaktivierennet.ipv4.conf.all.accept_source_route = 0net.ipv6.conf.all.accept_source_route = 0
# SYN-Flood-Schutznet.ipv4.tcp_syncookies = 1
sudo sysctl --systemLogin-Banner & MOTD
Section titled “Login-Banner & MOTD”Rechtlicher Hinweis: Unbefugte Zugriffe werden strafrechtlich verfolgt.
# /etc/issue.net (vor SSH-Login)echo "Unauthorized access prohibited. All activities are logged." | sudo tee /etc/issue.net
# /etc/motd (nach Login)echo "Welcome. This system is for authorized users only." | sudo tee /etc/motdAudit-Logging (auditd)
Section titled “Audit-Logging (auditd)”apt install auditd audispd-pluginssystemctl enable --now auditd
# Wichtige Regeln in /etc/audit/rules.d/audit.rules:# -w /etc/passwd -p wa -k identity# -w /etc/shadow -p wa -k identity# -w /etc/sudoers -p wa -k sudoers# -w /var/log/auth.log -p wa -k auth_log
auditctl -l # aktive Regelnausearch -k identity # nach "identity"-Events suchenRootkits & Malware (optional)
Section titled “Rootkits & Malware (optional)”apt install rkhunter chkrootkit clamav-daemonsystemctl enable --now clamav-daemon
# Wöchentlicher Scan via Cron:# 0 3 * * 0 /usr/bin/rkhunter --check --cronjob --report-warnings-onlyCheckliste “Server ist produktionsreif”
Section titled “Checkliste “Server ist produktionsreif””- SSH nur mit Pubkey-Login, root-Login aus
- Non-root User mit
sudo, eigener SSH-Key -
ufwaktiv: nur 22/80/443 offen (oder nur WireGuard) -
fail2banläuft, sshd-Jail aktiv -
unattended-upgradesaktiv - Zeitsync via
systemd-timesyncd - Sysctl-Härtung geladen
- restic-Repo initialisiert + erster Backup + Restore getestet
- auditd oder mindestens
auth.log-Monitoring - Login-Banner gesetzt
- Snapshot/Konfiguration der Firewall ist dokumentiert
Wenn alle Häkchen gesetzt sind: jetzt erst Dienste deployen.