Enable Two Factor Authentication on Raspberry Pi

Raspberry Pi Logo

Prerequsites

SSH is enabled

sudo systemctl enable ssh
sudo systemctl start ssh

Setup

Update

sudo apt update && sudo apt full-upgrade -y

Enabling Challenge Response

sudo nano /etc/ssh/sshd_config

You can press [Ctrl + W] to enter into search mode. Then type “Challenge” [Enter]

Change
From:
ChallengeResponseAuthentication no
To:
ChallengeResponseAuthentication yes

[Ctrl + X] [y]

sudo systemctl restart ssh

Install Two Factor App on Phone

andOPT is recommended. It is Open Source. There is no phone number requirement.

Install Google Authenticator PAM module on Raspberry Pi

sudo apt install libpam-google-authenticator -y

Set up 2FA

On the raspberry Pi run the command below as the NON sudo user

google-authenticator
Do you want authentication tokens to be time-based (y/n)
y
Warning: pasting the following URL into your browser exposes the OTP secret to Google:
https://www.google.com/chart?chsREMOVED
Your new secret key is: 99999999999999999999999999
Your verification code is 999999
Your emergency scratch codes are:
99999999
99999999
99999999
99999999
99999999
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)
y
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.
y

Configure PAM

sudo nano /etc/pam.d/sshd

add “auth required pam_google_authenticator.so”

sudo systemctl restart ssh

All done now each time you try and login you will be presented with

Verification code:

,