Skip to main content

Absichern des SSH-Logins mit 2-Faktor-Authentifizierung (TOTP)

Installation

Installieren des libpam-google-authenticator Pakets:

apt install libpam-google-authenticator
Erstellen des Geheimschlüssels und der Notfallscodes

Die Erstellung des Geheimschlüssels und der Notfallscodes erfolgt immer für den aktuell angemeldeten Benutzer, in dieser Anleitung für den Benutzer root.

Starten der Einrichtung über den google-authenticator:

google-authenticator

Auf die Frage ob mit Zeitbasierten Token (TOTP) gearbeitet werden soll mit y beantworten:

Do you want authentication tokens to be time-based (y/n)

Die angezeigete URL NICHT per Browser aufrufen, da sonst der Geheimschlüssel an Google übertragen wird!

Den QR-Code mit den gewünschten Geräten scannen und den Geiheimschlüssel im Passwortsafe hinterlegen.

Auf die Frage, ob die Datei /root/.google_authenticator aktualisiert werden soll, mit y beantworten:

Do you want me to update your "/root/.google_authenticator" file? (y/n)

Auf die Frage, ob dieLogins auf einen alle 30 Sekunden beschränkt werden sollen, mit y antworten:

Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n)

Auf die Frage, ob von 3 auf 17 erlaubte Codes (max. 4 Minuten Zeitdifferenz) erweitert werden soll, mit n beantworten:

By default, a new token is generated every 30 seconds by the mobile app.
In order to compensate for possible time-skew between the client and the server,
we allow an extra token before and after the current time. This allows for a
time skew of up to 30 seconds between authentication server and client. If you
experience problems with poor time synchronization, you can increase the window
from its default size of 3 permitted codes (one previous code, the current
code, the next code) to 17 permitted codes (the 8 previous codes, the current
code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
between client and server.
Do you want to do so? (y/n)

Auf die Frage, ob auf maximal 3 Loginversuche pro 30 Sekunden beschränkt werden soll, mit y antworten:

If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting? (y/n)
Änderungen an der /etc/pam.d/sshd

Am Beginn der Datei /etc/pam.d/sshd die Zeile auth required pam_google_authenticator.so hinzufügen:

# PAM configuration for the Secure Shell service
auth required pam_google_authenticator.so

# Standard Unix authentication.
@include common-auth 
Änderungen an der /etc/ssh/sshd_config

In der /etc/ssh/sshd_config den Schlüssel auf KbdInteractiveAuthentication yes ändern:

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
KbdInteractiveAuthentication yes

Den ssh-Server neu starten:

systemctl restart sshd