Score:1

Need help on google authenticator on ubuntu

cn flag

I have Ubuntu 20.04.3 LTS where I have successfully installed Google authenticator for MFA authentication, now I need a help on the steps of authentication, my goal is this:

  1. If a user has no ssh-key then on SSH connection the user must first enter their password and then enter the Google verification key to get system access.

  2. If a user has an ssh-key then there is no need to enter a password, but they should need to enter the Google verification key.

Now does it possible, if yes then what kind of settings I need to do in /etc/ssh/sshd_config and /etc/pam.d/sshd

Here is my existing /etc/pam.d/sshd configuration

# Standard Un*x password updating.
@include common-password

# Standard Un*x authentication.
@include common-auth

# Standard Un*x authentication.
auth required pam_google_authenticator.so nullok user=root secret=/root/totp/${USER}
auth required pam_permit.so

And here is /etc/ssh/sshd_config file

ChallengeResponseAuthentication yes
UsePAM yes
PubkeyAuthentication yes
AuthorizedKeysFile     .ssh/authorized_keys
PermitRootLogin yes
PasswordAuthentication no
AuthenticationMethods publickey,keyboard-interactive
Score:0
cn flag
Tom

Presuming you have everything installed correctly i.e. Installed with:

sudo apt-get install libpam-google-authenticator

And got a code with

google-authenticator

And there is a code in your home folder at

ls /home/$USER/.google_authenticator

Then you should add the following two lines to your /etc/pam.d/sshd

...

# Standard Un*x password updating.
@include common-password
auth required pam_google_authenticator.so
auth required pam_permit.so

...

Or if you want MFA to be optional for some users

...

# Standard Un*x password updating.
@include common-password
auth required pam_google_authenticator.so nullok
auth required pam_permit.so

...

In your /etc/ssh/sshd_config file change:

ChallengeResponseAuthentication yes

Then:

sudo systemctl restart sshd.service

When testing don't kill your ssh connection start a new one, if you config is wrong you can be locked out

cn flag
Hi Tom, did you completely read my question?, already have the same settings, then what is different in your answer?
Tom avatar
cn flag
Tom
Hi Umar, Was just trying to make the question generic to help many people. I don't have AuthenticationMethods set. It's not required really. But I would say it should look more like AuthenticationMethods publickey,password publickey,keyboard-interactive
cn flag
This is still not that what I want, I think I had clearly mentioned that if user has ssh key in his authorized_keys files then Ubuntu first check the key and then simply ask him to enter Google verification code, and if user has no ssh key then ubuntu should first ask him to enter password and after password verification simply ask hm to enter google verification code...
Tom avatar
cn flag
Tom
This cannot be done with the pam module. The ssh key overrides everything and so will never call the pam module. To use the pam module and an ssh key you must turn off password-auth
cn flag
This is, that what I want to confirm from someone, so what I want is not possible
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.