How to setup two factor authentication in Ubuntu for Ubuntu users using Google authenticator (It can also be setup for non Google accounts)?
How to setup two factor authentication in Ubuntu for Ubuntu users using Google authenticator (It can also be setup for non Google accounts)?
Note: Once you have activated the 2-factor authentication for a user and haven't set the same for root, you will never be able to Login as root directly. In such a case a way around is to use any other sudo user for whom we have it setup and then use
sudo su -
to switch to the root user.Use below steps to set it up.
Install below given package to install Google authenticator which we will use as an add-on with PAM authentication:
Now edit
/etc/pam.d/sshd
this file and add Google Authenticator as given below:enter below at the top this file-
Here we have to make changes in
/etc/ssh/sshd_config
to ensure ssh uses the Google Authenticator, this way we ensure ssh is using the two factor authentication.In this file we have to find
ChallengeResponseAuthentication
and uncomment and/or modify it to look like below (in short set it to yes :P):Extra or GUI 2-factor authentication else skip this and go to step 4: To enable it for GUI login, edit
/etc/pam.d/common-auth
:and now add this
auth required pam_google_authenticator.so
above the lineauth [success=1 default=ignore] pam_unix.so nullok_secure
then save the file.Now change to an account on which you want to set it up.
(Note: I would suggest to create at least two super user accounts on the system apart than the root account and configure it at least, for one of them first but not the root account.)
Now we will use below command to setup the two-factor authentication for this
testuser1
:Running this command will ask you below question. (recommended answer is Yes)
After that it will show you the QR code and Emergency Scratch Codes and few other details. Out put should look like below given image:
Now you need to use your Android / Apple / Blackberry phone to download & install the Google Authenticator Application from the respective market places for example Google play store. which will generate code for you to login.
Below are the screenshot of the application Icon and application taken from application Android phone.
Start the application on your phone and scan the QR Code or else use the secret key and the verification code given below the QR code on the system, which you can also see in the first screenshot above.
Once all of this is done it is very important to note down and save your emergency scratch codes on a safe place, as those are the codes which can help you in case you get locked out somehow.
At this point in time you should take a look at the bottom of the screen where it is asking you a below question. (recommended answer is Yes):
Again it will ask you one more question and the recommended answer for below question is also Yes:
Next question would be as given below and the recommended answer for it is No:
And the last question would be as given below and recommended answer for it is Yes:
Now switch exit from this account to go back to root account:
Now restart the ssh service
Now just take a ssh session for the user you have set it up for and it will first ask you for a verification code which you can enter from your mobile and then it will ask for a user password.
That is all what is required to setup the two factor authentication. Please feel free to improve the answer where required and please excuse me for the not so good language.