Raspberry Pi – setting up two factor authentication (2FA) with the Google Authenticator

   You can enable two factor authentication (2FA) and afterwards you’ll be challenged for a verification code when you access it remotely via Secure Shell (SSH)

root@rpi-iot-jsho-2FA-01:~# apt-get update -y
root@rpi-iot-jsho-2FA-01:~# apt-get full-upgrade -y
root@rpi-iot-jsho-2FA-01:~# apt-get autoremove
root@rpi-iot-jsho-2FA-01:~# systemctl enable ssh
root@rpi-iot-jsho-2FA-01:~# systemctl start ssh
root@rpi-iot-jsho-2FA-01:~# vi /etc/ssh/sshd_config
# Change to yes to enable challenge-response passwords (beware issues with some PAM modules and threads)
ChallengeResponseAuthentication yes
root@rpi-iot-jsho-2FA-01:~# systemctl restart ssh
root@rpi-iot-jsho-2FA-01:~# apt-get install libpam-google-authenticator
pi@rpi-iot-jsho-2FA-01:~ $ google-authenticator
Do you want authentication tokens to be time-based (y/n) y
Your new secret key is: Y…
Enter code from app (-1 to skip): 1…
Code confirmed
Your emergency scratch codes are:
9…
3…
1…
8…
4…
Do you want me to update your „/home/pi/.google_authenticator“ file? (y/n) y
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, 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) 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
pi@rpi-iot-jsho-2FA-01:~ $

Don’t move forward quite yet! Before you do anything else you should copy the emergency codes and put them somewhere safe. These codes will let you access your Raspberry Pi — and turn off 2FA — if you lose your phone without them you won’t be able to SSH into your Raspberry Pi if you lose or break the device you’re using to authenticate next before we continue with Google Authenticator on the Raspberry Pi open the Google Authenticator app on your phone and tap the plus sign (+) at the top right then tap on “scan barcode” your phone will ask you whether you want to allow the app access to your camerayou should say “Yes” the camera view will open position the barcode squarely in the green box on the screen

root@rpi-iot-jsho-2FA-01:~# vi /etc/pam.d/sshd
# 2FA
auth required pam_google_authenticator.so
root@rpi-iot-jsho-2FA-01:~# systemctl restart ssh

login as: pi
Keyboard-interactive authentication prompts from server:
| Password:
| Verification code:
End of keyboard-interactive prompts from server
Linux rpi-iot-jsho-2FA-01 5.10.63-v7+ #1459 SMP Wed Oct 6 16:41:10 BST 2021 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Nov 10 13:23:13 2021 from 192.168.1.3
pi@rpi-iot-jsho-2FA-01:~ $

Leave a Reply

You must be logged in to post a comment.