How to set up 2 factor authentication in Ubuntu Server 18.04

In this tutorial we will learn how to enable Multi-factor authentication in Ubuntu Server 18.04, this will increase security by confirming your identity using at least two different ways of authentication instead of the regular single password, thus removing any chance of a dictionary or brute force attack to your server.

After completing this tutorial we will be able to log in to the server using a combination of a password and one time code that can be generated by a mobile app that is synchronized with your server.

We will be using the Google Authenticator app available for Android here from the Google Playstore and for iOS here from iTunes to generate the codes.

Installing and configuring the google authenticator module for Ubuntu.


sudo apt install libpam-google-authenticator

To enable 2 factor authentication we will add the following line to the end of the/etc/pam.d/sshd file

auth required


Make sure to add this at the end of the file so that it will request for the code after the password.

Setting up authentication:

Modify the /etc/ssh/sshd_config
Look for the setting ChallengeResponseAuthentication and change this from no to yes

ChallengeResponseAuthentication yes

Restart SSH to apply the new settings

 sudo systemctl restart sshd.service

Setting up the Google Authenticator App:

Generating th QR Code for the app

To start the setup simply run the google-authenticator command and answer the following questions.

Here are the recommended settings:

Do you want authentication tokens to be time-based (y/n) y
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, 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) 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

Now with the QR code that was generated we can proceed to the next step to set up the Google Authenticator App


Adding the QR Code to the Google Authenticator App

Install the Google Authenticator app from the Android App Store or from Itunes

Launch the App, if it is the first time launching the app you will be greeted with the following, otherwise click on add account


Next you can choose to scan the code with your camera, or you can manually add the codes if you are having issues with it.

Place the barcode within the lines and the account will be added automatically

Once the account is added you will see a code that will change every 30 seconds that will be required together with your password to log into the server

That’s all. Now, we can test logging into the server via ssh to test it out.

SSH to your server and you should receive the normal password promt and an additional code.


Congratulations! You have just configured 2 factor authentication for your Server. Now, every time you try to log in, you will be asked for a password an authentication key that changes every 30 seconds to log in.

Get the latest posts delivered right to your inbox

don’t have your own server visit DigitalOcean servers starting at 5$ a month

10$ off on me with