Ubuntu - SSH Two Factor Authentication
Let me start by saying that security is a mindset, not an application. The best way to secure one's systems is to configure as many layers of security as possible and be careful with one's credentials. No single system or application will guarantee complete protection, but setting up many barriers may prevent an attack or severly limit it's capabilities. Here we will demonstrate how to setup two factor authentication for logging into an Ubuntu server through SSH. This will act as another layer of security in addition to the use of passwords or SSH keys.
What Is Two Factor Authentication?
Linus from Linus Tech Tips can explain this in a far better manner and detail than I. Watch the video below.
Installation Steps
Install dependencies
sudo apt-get install libpam-google-authenticator -y
Edit the Config Files
Execute the following bash script to update the necessary config files.
#!/bin/bash
# update the /etc/pam.d/sshd
echo "auth required pam_google_authenticator.so"|cat - /etc/pam.d/sshd > /tmp/out
cat /tmp/out | sudo tee /etc/pam.d/sshd
# Update the /etc/ssh/sshd_config file
SEARCH="ChallengeResponseAuthentication no"
REPLACE="ChallengeResponseAuthentication yes"
FILEPATH="/etc/ssh/sshd_config"
sudo sed -i "s;$SEARCH;$REPLACE;" $FILEPATH
Configure A User
The previous steps were to setup the server. This set of steps should be used for each user on the server that you wish to apply two factor authentication to.
Run the following command:
google-authenticator
Answer y
to the first question.
Do you want authentication tokens to be time-based (y/n)
Add the Account to Google Authenticator
A barcode will be shown that one can scan with the Google authenticator application. After scanning it, a new account will be setup with the format user@hostname so it is important to have configured the server's hostname correctly beforehand.
Alternatively, one could add the account using the secret key and the verification code.
Copy The Scratch Codes
At this point it is a good idea for one to copy the "scratch codes" (found below the barcode) onto a USB stick that is hopefully kept encrypted and hidden away from the computer when not in use.
Answer the Remaining Questions
Answer y to the next question.
Do you want me to update your "/home/USERNAME/.google_authenticator" file (y/n)
For the next question, I recommend answering y
for production servers or anything else that is vitally important.
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)
For the next question I would answer n
in all cases.
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)
The next question asks about rate-limiting, for which I recommend answering 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)
Restart the SSH Service
sudo service ssh restart
Testing
Now try to SSH into your server. You should be asked for the Google authentication code first and then your password if you aren't using ssh keys.
Conclusion
You should now have two-factor authentication configured for SSH. If you have any problems or feel that I have missed something, please post a comment. One can always use the references below for additional information.
References
- Digital Ocean - How To Protect SSH With Two-Factor Authentication
- Nixcraft - Secure Your Linux Desktop and SSH Login Using Two Factor Google Authenticator
- How-to-geek - How to Secure SSH with Google Authenticator’s Two-Factor Authentication
- Linux.com - Securing SSH with two factor authentication using Google Authenticator
First published: 16th August 2018