Wir möchten unsere SSH Zugang mit einer Zwei Faktor Authentifizierung absichern.
Zur Erzeugung des zweiten Faktor verwenden wir ein Smartphone mit der App FreeOTP oder Google Authenticator. Diese Apps generieren ein Token, welcher nur für eine bestimmte Zeit gültig ist. Damit wir der SSH Zugang zusätzlich abgesichert.
Zuerst müssen wir das SSH Modul für Google Authenticator installieren.
sudo apt install libpam-google-authenticator
Jetzt müssen wir den Server mit der App verbinden. Dazu rufen wir die Anwendung als Benutzer auf.
google-authenticator
Den QS Code können wir mit dem Smartphone in der App Google Authenticator einlesen. Die Emergency Codes sollten wir uns an einem sicheren Ort speichern.
Die Frage können wir mit Yes, Yes, Yes, No, Yes beantworten.
Do you want authentication tokens to be time-based (y/n) y
Do you want me to update your "/home/user/.google_authenticator" file (y/n)? y
The app provides you with a secret key, verification code, and recovery codes. Keep these in a secure, safe location. The recovery codes are the only way to access your server if you lose your mobile phone.
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) y
By default, tokens are good for 30 seconds. 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. If you experience problems with poor time synchronization, you can increase the window from its default size of +-1min (window size of 3) to about +-4min (window size of 17 acceptable tokens).
Do you want to do so? (y/n) n
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) y |
Do you want authentication tokens to be time-based (y/n) y
Do you want me to update your "/home/user/.google_authenticator" file (y/n)? y
The app provides you with a secret key, verification code, and recovery codes. Keep these in a secure, safe location. The recovery codes are the only way to access your server if you lose your mobile phone.
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) y
By default, tokens are good for 30 seconds. 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. If you experience problems with poor time synchronization, you can increase the window from its default size of +-1min (window size of 3) to about +-4min (window size of 17 acceptable tokens).
Do you want to do so? (y/n) n
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) y
Das PAM Modul muss noch konfiguriert werden.
sudo vim /etc/pam.d/sshd
Ende der Datei fügen wir eine Zeile hinzu.
auth required pam_google_authenticator.so |
auth required pam_google_authenticator.so
In den Einstellungen vom SSH-Server müssen ggf. noch Einstellunge geändert werden.
sudo vim /etc/ssh/sshd_config
Prüfen Sie ob diese Werde gesetzt sind.
ChallengeResponseAuthentication yes
PasswordAuthentication no |
ChallengeResponseAuthentication yes
PasswordAuthentication no
Nach dem Neustart vom SSH-Server sollte die Zwei Faktor Authentifizierung arbeiten.
sudo systemctl restart sshd.service