Multi-Factor Authentication in Linux using Google Authenticator

I would setting up a multifactor authentication in my Centos 7 for a user Kevin for ssh connectivity.

1) Add the EPEL (Extra Packages for Enterprise Linux) repo.

sudo yum install

2) Install the google-autheticator

sudo yum install google-authenticator

3) Run the google autheticator app for the user for which we want to setup the MFA. Note secret key and Verification code will be shown which would be needed for setting up google Autheticator on Android/Iphone


It will prompt you certain questions as below , Provide the mentioned values

[kevin@lcfs ~]$ 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:

Do you want me to update your "/home/kevin/.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

4) Edit the PAM authetication moddule to use google authetication

sudo vi /etc/pam.d/sshd
Add the following line to the bottom of the file.

. . .
# Used with polkit to reauthorize users in remote sessions
-session   optional prepare
auth required nullok

5) Edit the ssh configuration file to accept Authetication using MFA.

sudo vi /etc/ssh/sshd_config
Look for ChallengeResponseAuthentication lines. Comment out the no line and uncomment the no line.

. . .
# Change to no to disable s/key passwords
ChallengeResponseAuthentication yes
#ChallengeResponseAuthentication no

6) Restart sshd service
service sshd restart

7) Install the Google Autheticator App and configured using secrete key received from step 3.

8) Trying logging using user Kevin and verify.

himanshu@himanshu-ThinkPad-T430 ~ $ ssh kevin@lfcs.lab
Verification code: 
Last login: Sat Apr 11 12:37:19 2020 from
[kevin@lcfs ~]$ exit