How to Setup 2FA on an SSH Server

How to Setup 2FA on an SSH Server

In this tutorial we will install Google Authenticator to provide time-based 2FA as a second layer of protection on our server. I will mention that if you are using SSH keys then you are most likely fine to treat this as optional. In fact I have a whole post on how to recover your server in case you get locked out of it due to 2FA issues. I guess I am saying you might not want to do this. 🙂

Installing Time Based 2FA for our SSH Server

First off we will install the Google Authenticator PAM module via: 

							
							
					sudo apt install libpam-google-authenticator				
			

Next we need to add the following line to the PAM ssh config file located at /etc/pam.d/sshd 

							
							
					auth required pam_google_authenticator.so nullok
#nullok ensures that if problems arise due to a misconfiguration that you are still able to login
#be sure to delete nullok for production				
			

And in the same file we need to comment out the line: @include common-auth, it should look like: #@include common-auth

Now we will restart the ssh server via: 

							
							
					sudo systemctl restart ssh				
			

Next we need to adjust the following settings in the /etc/ssh/sshd_config file:

  • “KbdInteractiveAuthentication” set to “yes”
  • “ChallengeResponseAuthentication” set to “yes”
  • “AuthenticationMethods” set to “publickey,keyboard-interactive”
  • “UsePAM” set to “yes”
  • “PasswordAuthentication” set to “no”

Once we have finished configuring the settings in the sshd_config file, exit the editor and we will configure the Google Authenticator settings. Run: 

							
							
					google-authenticator
				
			

Then it will prompt you with various settings and options, adjust the configuration as needed, I have provided a sample configuration below. Be sure to copy the emergency scratch codes somewhere safe.

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

#You will see a GIANT QR code here

Your new secret key is: XXXXXXXXXXXXXXXXXXX
Enter code from app (-1 to skip): XXXXXXXX
Code confirmed
Your emergency scratch codes are:
  XXXXXX
  XXXXXX
  XXXXXX
  XXXXXX
  XXXXXX
Do you want me to update your "/home/<username>/.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
				 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				
			

Once you have finished the Google Authenticator configuration restart the ssh server via:

							
							
					sudo systemctl restart ssh				
			

Test logging into the server in a new terminal before logging out of the current session to ensure things are working properly.

That’s a wrap! 

Walter Miely is a tech entrepreneur and CEO of Phoenix Ignited Tech You can find him on Linkedin. This material is licensed under the CC BY 4.0 License LEGAL DISCLAIMER: The content provided here is provided AS IS, and part of, or the entirety of this content may be incorrect. Please read the entireLegal Disclaimer here.