Here at Liquidstate, we have a few Linux systems that have their SSH daemons exposed to the Internet. In this post, I'll show you how we've used the Google Authenticator App to enable Two Factor Authentication for remote (SSH) logins.

Once enabled, your Linux system will require a six-digit security code in addition to your password whenever you SSH in.  That might sound annoying, but its not actually that bad and might save you a similar fate to Mat Honan who was recently hacked good and proper.

So, before we really get started, lets just get the Google Authenticator smartphone app installed.  In this example, we'll be using it on an iPhone 4S,  which is freely available in the App Store as you would expect.

In this example, we'll be using the Google Authenticator PAM module with an Ubuntu 12.04 LTS system.  The good news is that the libpam-google-authenticator module has been included in the Ubuntu software repository since 11.10.  So, first things first, lets get the module installed:

[email protected]:~$ sudo apt-get install libpam-google-authenticator

After installing, run the "google-authenticator" program as the user you will access remotely. This will create a private key and display a QR code which you can scan using the Google Authenticator smartphone app.  In this example, we'll be using the iPhone app.  You will also get some emergency codes for when you don’t have your phone.

[email protected]:~$ google-authenticator

Before answering any of the configuration option, lets use that QR Code to configure the Google Authenticator app.  Fire up the Google Authenticator app on your smartphone and hit the "+" button and you'll be presented with a rather basic screen:

Arrange your terminal window so that the QR Code is completely displayed on the screen.  Hit the Scan Barcode button on your iPhone and then hold  it up to the screen.  The app will automatically recognise the barcode and present you with a basic and rather cryptic screen with your SSH login and a 6-digit code.  This the 6-digit code that will change regularly and you will use to complete SSH authentication in the future.  The little pie-chart in the top left corner counts down how long the current code will last before you are presented with the next one.

Now, back to your terminal and those configuration questions.  In this example, we've chosen to enable multiple logins and enable rate-limiting:

Do you want me to update your "~/.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) n
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) 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

Now that's all configured, we need to globally enable Two Factor Authentication.  This is done in PAM, which is the central place for all thing authentication based.

Open the file /etc/pam.d/sshd and in the Standard Un*x authentication section and add a new line to enable google-authenticator-pam PAM module.

# Standard Un*x authorization.
@include common-account
auth required pam_google_authenticator.so
# Standard Un*x session setup and teardown.
@include common-session

Next, open /etc/ssh/sshd_config, and change or add the ChallengeResponseAuthentication line so it reads:

ChallengeResponseAuthentication yes

Restart SSH to make the configuration change take effect:

sudo service ssh restart

If you’re setting this up on a remote server, keep your current SSH session open so you can revert the changes if needed. Try to SSH to the system, and you should be prompted for the password and a verification code from the Google Authenticator App before you are admitted.

Congratulations, your Linux box is now much more secure than it was 10 minutes ago!