SSH Hardening
SSH Hardening – Cheatsheet
Schnellreferenz zur SSH-Absicherung.
SSH-Key erstellen
# Ed25519 (empfohlen)
ssh-keygen -t ed25519 -C "user@hostname"
# RSA 4096
ssh-keygen -t rsa -b 4096 -C "user@hostname"
# Key kopieren
ssh-copy-id user@server
# Berechtigungen
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 600 ~/.ssh/authorized_keys
Wichtigste sshd_config Einstellungen
# /etc/ssh/sshd_config
# Port ändern
Port 2222
# Root-Login verbieten
PermitRootLogin no
# Passwort-Login deaktivieren
PasswordAuthentication no
PermitEmptyPasswords no
# Nur bestimmte User
AllowUsers admin deploy
# Limits
MaxAuthTries 3
MaxSessions 3
LoginGraceTime 30
# Timeout
ClientAliveInterval 300
ClientAliveCountMax 2
# Features deaktivieren
X11Forwarding no
AllowTcpForwarding no
Nach Änderungen
# Syntax prüfen
sudo sshd -t
# Neustart
sudo systemctl restart sshd
# Testen (in neuem Terminal!)
ssh -v -p 2222 user@server
Fail2Ban
# Installieren
sudo apt install fail2ban
# /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 2222
maxretry = 3
bantime = 3600
# Status
sudo fail2ban-client status sshd
# IP entsperren
sudo fail2ban-client set sshd unbanip 1.2.3.4
Firewall (UFW)
# Neuen Port erlauben
sudo ufw allow 2222/tcp
# Alten Port entfernen
sudo ufw delete allow 22/tcp
# Nur bestimmte IPs
sudo ufw allow from 192.168.1.0/24 to any port 2222
Monitoring
# Erfolgreiche Logins
grep "Accepted" /var/log/auth.log
# Fehlgeschlagene
grep "Failed password" /var/log/auth.log
# Live verfolgen
journalctl -u sshd -f
# Aktuelle Verbindungen
who
ss -tn | grep :22
Sichere Kryptographie
# /etc/ssh/sshd_config
HostKeyAlgorithms ssh-ed25519,rsa-sha2-512
KexAlgorithms curve25519-sha256@libssh.org
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com
MACs hmac-sha2-512-etm@openssh.com
2FA mit Google Authenticator
# Installieren
sudo apt install libpam-google-authenticator
google-authenticator
# /etc/pam.d/sshd
auth required pam_google_authenticator.so
# /etc/ssh/sshd_config
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
Checkliste
- [ ] SSH-Keys statt Passwörter
- [ ] Root-Login deaktiviert
- [ ] Port geändert
- [ ] Fail2Ban aktiv
- [ ] User eingeschränkt
- [ ] Firewall angepasst
- [ ] Timeout konfiguriert
- [ ] Getestet!