Ubuntu | Google Authenticator not working with SSH keys

Author - Navneet Kashyap

Purpose is to get MFA/ 2-step verification working i.e. google-authenticator + ssh Keys

First of all, install google authenticator on your server with following steps:

1. Yum install google-authenticator.x86_64 (Redhat/Centos)

sudo apt-get install libpam-google-authenticator (ubuntu OS)

2. Now vi /etc/pam.d/sshd (add following line at the top)

auth required pam_google_authenticator.so (both centos/ubuntu)

3. vi /etc/ssh/sshd_config (both centos/ubuntu)

ChallengeResponseAuthentication
yes

UsePAM
yes

AuthenticationMethods publickey,keyboard-interactive
PasswordAuthentication
no

4. in /etc/pam.d/sshd

auth substack password-auth (Redhat/Centos)

@include common-auth (Ubuntu OS)

5. Service sshD restart

6. Login with the user you want to use google authenticator

7. Run # google-authenticator

Let’s have a quick look at those questions and the outputs:

(i) . Do you want me to update your “/root/.google_authenticator” file (y/n)
y

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

(iii). By default, tokens are good for 30 seconds and 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 1:30min to about 4min. Do you want to do so (y/n)
n

(iv). 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)
n

Note: Copy all information that will be displayed after executing above command i.e. google-authenticator.

Output: 

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

https://www.google.com/chart?chs=200×200&chld=M|0&cht=qr&chl=otpauth://totp/root@hostname%3Fsecret%3DWKHM6UVJNTPYSPTQ
Your new secret key is: WKHM6UVJNTPYSPTQ
Your verification code is 434260

Your emergency scratch codes are:
30287010
70585905
68748337
15176712
38041521

8. Now you can install google-authenticator on any Android/windows/Apple mobile.

9. Remember the secret key you got when you initialized Google-Authenticator PAM module on your computer. Select Enter provided key.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *