Ubuntu/Centos – Install 2-factor authentication for ssh login

0

PCI-DSS 3.2 has one major new requirement which is 2-factor authentication. There are many ways to get that done in an Enterprise environment. But how about a standalone webserver for E-commerce or DMZ without central auth?

When a ssh-key authentication is not enough, the quickest solution for a server could be to make ssh to ask for 2-factor token when logging in.

2-factor authentication is based on one part where you need to know the password plus something you need to have additionally. You can create a separate key on an external device like a tablet or mobile phone. There are also devices like RSA token but that’s not part of this.

Here I plan to use Android tools like FreeOTP, Authy or Google Authenticator for the passkey.
They usually produce every 30 seconds a new key based on OATH.

Let’s secure the ssh access now.

Setup:

Firstly we need to add the google authenticator PAM library.

Ubuntu:

sudo apt-get install libpam-google-authenticator

Centos 6:

sudo yum install google-authenticator

Centos 7:

sudo dnf install google-authenticator

Change settings in sshd_config

sudo vi /etc/ssh/sshd_config

change

ChallengeResponseAuthentication no

to

ChallengeResponseAuthentication yes

and

#PasswordAuthentication yes

to

PasswordAuthentication no

ensure that UsePAM is set to yes

UsePAM yes

Change pam.d ssh

sudo vi /etc/pam.d/sshd

find at the top of the file @include common-auth and insert straight below it:

auth required pam_google_authenticator.so

restart ssh

Ubuntu:

sudo service ssh restart

Centos:

sudo service sshd restart

Configure the user account:

This has to be done as the user / for each user:

Start google-authenticator to setup the authentication and follow the following:

google-authenticator

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

You will then get a URL and a QR Code for the phone to setup, a secret key, a verification code and 5 emergency scratch code that you can use without the authenticator.

Save the codes and key.

Add the QR code to a tool of your choice. I have tested the QR code with the following Android apps (the order does not reflect any judment or preference):

Authy
FreeOTP
Google Authenticator

To add them, use the camera and add a new account by using the QR code. Alternatively you can add the code by using the URL or the secret key.

Continue with the following options:

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

Answer the following as per your desire.

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, 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) y

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

How to use:

ssh user@system
Password:
Authentication:

Leave a Reply