Search

How to configure SSH to use two factor authentication

post-title

SSH is the secure shell which is used to access remote Linux systems. By default, when user access your server using SSH, they only type a username and password to log in. We often use it to connect with computers containing important data, it’s recommended to add another security layer.

Here comes the two factor authentication. With two-factor authentication, user have to provides another details with login username and password. This will provide extra layer of security. Follow these steps to configure and use two factor authentication.

Prerequisites

To setup two-factor authentication, you need a non-root user with sudo permission. Server also need SSH key and firewall enabled. You also need phone or tablet with TOTP auth app installed like Google Authenticator or Authy.

Installing and configuring Google Authenticator package

After you logged into server, install Google authenticator package with below command.

sudo apt-get update
sudo apt-get install libpam-google-authenticator

Now to generate time-based one-time passwords on your device, run the below command.

google-authenticator

It will ask few questions. The first question will be:

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

Press y and hit the Enter key. This will output the QR code. Scan this code in your TOTP application in phone or tablet.

Open the TOTP app and scan the QR code.

This will add account in Athentication application which will generate random OTP code every 30 seconds.

In the server, it will ask other few questions. Here is the recommended answers for questions.

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

Configure the SSH to use Google Authenticator

The TOTP auth application will change token every 30 seconds. We will configure SSH to use TOTP auth application. To make SSH use Google Authenticator module, add the below lines in the /etc/pam.d/sshd file.

auth required pam_google_authenticator.so

Now restart the ssh deamon using below command.

sudo systemctl restart sshd.service

Open the /etc/ssh/sshd_config file and change ChallengeResponseAuthentication from no to yes.

. . .
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication yes

That's it! You have configured two-factor authentication for SSH using TOTP auth. Now, every time when you try to log in to your server with SSH, you will be asked for an authentication key in addition to login username and password.