SSH Authentifizierung
Debian SSH: Passwort für normale User, SSH-Key für root
Diese Anleitung zeigt, wie du OpenSSH so konfigurierst, dass sich normale Benutzer mit Passwort anmelden können, während sich root ausschließlich mit SSH-Schlüssel (Zertifikat/Key) anmelden darf.
Voraussetzungen
- Debian/Ubuntu mit
openssh-server
- Zugriff als root (oder per
sudo
) - Ein funktionierender SSH-Key für root (z. B. Ed25519)
1) SSH-Key für root vorbereiten
- Auf deinem Admin-Client (nicht auf dem Server) einen Key erzeugen (falls noch nicht vorhanden):
ssh-keygen -t ed25519 -a 100 -f ~/.ssh/id_ed25519
ssh-keygen -t ed25519 -a 256 -f ~/.ssh/id_ed25519 -C "jj@little-snitch"
- Öffentlichen Schlüssel auf den Server in die root-Authorized Keys kopieren:
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@server.example.com
Alternativ manuell:
cat ~/.ssh/id_ed25519.pub | ssh root@server.example.com 'mkdir -p /root/.ssh && chmod 700 /root/.ssh && cat >> /root/.ssh/authorized_keys && chmod 600 /root/.ssh/authorized_keys'
2) Bestehende SSH-Konfiguration sichern
cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.backup.$(date +%F-%H%M%S)
3) Kerneinstellungen
Die zentrale Idee:
PermitRootLogin prohibit-password
→ verbietet root-Passwortlogin, erlaubt aber Schlüssel.PasswordAuthentication yes
→ Passwortlogin global erlaubt (wird in Match-Blöcken eingeschränkt).Match User root
→ für root nur SSH-Key.Match User <username>
→ optional: für bestimmte User Verhalten festlegen.
4) Beispiel: vollständige /etc/ssh/sshd_config
Folgende Konfiguration entspricht deinem Beispiel und erzwingt für root
Key-Login, lässt für master771
sowohl Passwort als auch Key zu. Andere Benutzer folgen den globalen Einstellungen (Passwort erlaubt, Key erlaubt):
root@mqtt:~# cat /etc/ssh/sshd_config
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin:/bin:/usr/games
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
Include /etc/ssh/sshd_config.d/*.conf
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin prohibit-password
PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
#PubkeyAuthentication yes
PubkeyAuthentication yes
# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to "no" here!
#PasswordAuthentication yes
PasswordAuthentication yes
#PermitEmptyPasswords no
# Change to "yes" to enable keyboard-interactive authentication. Depending on
# the system's configuration, this may involve passwords, challenge-response,
# one-time passwords or some combination of these and other methods.
# Beware issues with some PAM modules and threads.
KbdInteractiveAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the KbdInteractiveAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via KbdInteractiveAuthentication may bypass
# the setting of "PermitRootLogin prohibit-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and KbdInteractiveAuthentication to 'no'.
UsePAM yes
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none
# Allow client to pass locale and color environment variables
AcceptEnv LANG LC_* COLORTERM NO_COLOR
# override default of no subsystems
Subsystem sftp /usr/lib/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
Match User root
PasswordAuthentication no
PubkeyAuthentication yes
Match User master771
PasswordAuthentication yes
PubkeyAuthentication yes
5) Syntax prüfen und SSH neu laden
- Syntaxcheck:
sshd -t
Wenn keine Ausgabe erfolgt, ist die Syntax OK.
- Dienst neu laden (ohne laufende Sessions zu kappen):
systemctl reload ssh
Falls
reload
nicht verfügbar, dann:systemctl restart ssh
6) Sicher testen (zweite Session!)
- Neue Terminal-Session öffnen, bestehende Sitzung nicht schließen.
- Test root per Key:
ssh -i ~/.ssh/id_ed25519 root@server.example.com
- Test root per Passwort (soll scheitern):
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no root@server.example.com
- Test normaler Benutzer per Passwort (soll funktionieren):
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no master771@server.example.com
Optionen & Varianten
- Nur Passwort für bestimmte Benutzer erzwingen:
Match User alice,bob PasswordAuthentication yes PubkeyAuthentication no
- Key-Login global erlauben, aber root-Passwort strikt verbieten: Bereits abgedeckt durch
PermitRootLogin prohibit-password
und denMatch User root
-Block. - Brute-Force eindämmen:
fail2ban
verwenden undMaxAuthTries
senken (z. B.MaxAuthTries 3
).
Rollback
Falls etwas schiefgeht, via Konsole (z. B. VM-Konsole) die Sicherung zurückspielen:
mv /etc/ssh/sshd_config.backup.YYYY-MM-DD-HHMMSS /etc/ssh/sshd_config
systemctl restart ssh
Hinweise zur Sicherheit
- Lege für alle Benutzer starke Passwörter fest oder nutze ggf. 2FA (z. B.
google-authenticator
) zusätzlich. - Bevorzuge Ed25519-Schlüssel. RSA nur mit ausreichend großer Länge (min. 3072 Bit).
- Überlege, den SSH-Port zu ändern und/oder Port-Knocking bzw. VPN vorzuschalten.
- Halte
openssh-server
aktuell (apt update && apt upgrade
).
Stand: aktualisiere diese Seite, wenn sich deine Policy ändert oder neue Benutzer hinzukommen.
No Comments