Home » configure SSH for two factor authentication on linux

configure SSH for two factor authentication on linux

In this article will show you how to setup and configure SSH for two factor authentication under Red Hat, CentOS, Fedora and Ubuntu,
Installing Google Authenticator Module
Open the server/system that you want to setup two factor authentication and install following PAM libraries along with development libraries that are needed for the PAM module to work correctly with Google authenticator module.
On Red Hat, CentOS and Fedora systems install the ‘pam-devel‘ package.

#yum install pam-devel make gcc-c++ wget

On Ubuntu, Linux Mint and Debian systems install ‘libpam0g-dev‘ package.

# apt-get install libpam0g-dev make gcc-c++ wget

Download and extract Google authenticator module under /root directory

# cd /root
# wget https://google-authenticator.googlecode.com/files/libpam-google-authenticator-1.0-source.tar.bz2
# tar -xvf libpam-google-authenticator-1.0-source.tar.bz2

Type the following commands to compile and install Google authenticator module on the system.

#cd libpam-google-authenticator-1.0
# make
# make install
# google-authenticator

Once you run ‘google-authenticator‘ command, it will prompt you with a serious of question. Simply type “y” (yes) as the answer in most situation. If something goes wrong, you can type again ‘google-authenticator‘ command to reset the settings.

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

After this question, you will get your ‘secret key‘ and ‘emergency codes‘. Write down these details somewhere, we will need the ‘secret key‘ later on to setup Google Authenticator app.

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

Your new secret key is: SD14E@4SFGKIU78
Your verification code is 461618
Your emergency scratch codes are:
12587586
48712598
12478425
87497458
35874989

Next, follow the setup wizard and in most cases type answer as “y” (yes) as shown below.

Do you want me to update your “/root/.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, 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

Configuring SSH to use Google Authenticator Module
Open the PAM configuration file ‘/etc/pam.d/sshd‘ and add the following line to the top of the file.

auth required pam_google_authenticator.so

Next, open the SSH configuration file ‘/etc/ssh/sshd_config‘ and scroll for fine the line that says.

ChallengeResponseAuthentication no

Change it to “yes“. So, it becomes like this.

ChallengeResponseAuthentication yes

Finally, restart SSH service to take new changes.

# /etc/init.d/sshd restart

Configuring Google Authenticator App
Launch Google Authenticator app in your smartphone/system. Press Menu and choose “Setup an account“. If you don’t have this app, you can download and install Google Authenticator app on your Android/iPhone/Blackberry devices.
setup account

Google Authenticator Setup Account

Press “Enter key provided”.
key

Enter Google Authenticator Secret Key
Add your account ‘Name‘ and enter the ‘secret key‘ generated earlier

settings11
Google Authenticator Account Name and Secret Key
It will generate one time password (verification code) that will constantly changing every 30sec on your phone.
authication
Google Authenticator One Time Password
Now try to login via SSH, you will be prompted with Google Authenticator code (Verification code) and Password whenever you attempt to log in via SSH. You have only 30 seconds to enter this verification code, if you miss it will regenerate new verification code.

login as: gajendra
Access denied
Using keyboard-interactive authentication.
Verification code:
Using keyboard-interactive authentication.
Password:
Last login: Tue Sep 23 23:58:29 2013 from xx.xx.xx.xx
[gajendra@linuxpcfix ~]#
If you don’t have smartphone, you can also use a Firefox add-on called GAuth Authenticator to do two-factor authentication.
Important: The two-factor authentication works with password based SSH login. If you are using any private/public key SSH session, it will ignore two-factor authentication and log you in directly.

One thought on “configure SSH for two factor authentication on linux

  1. jestinecouvreur says:

    Excellent post. I was checking continuously this blog and I am impressed!
    Very useful information specifically the last part 🙂 I care for such info a lot.
    I was seeking this particular information for a long time.
    Thank you and good luck.

Leave a Reply

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

*
*

Time limit is exhausted. Please reload the CAPTCHA.

Categorized Tag Cloud