Prerequisites
Before we begin, make sure you have the following:
- A Linux server (Ubuntu, CentOS, or any other distribution)
- Administrative access to the server
- A smartphone (iOS or Android) to install the FreeOTP app
Step 1: Installing FreeOTP
- On your smartphone, open the respective app store (Google Play Store or Apple App Store).
- Search for "FreeOTP" and install the app.
- Once installed, open the FreeOTP app.
Step 2: Configuring SSH for MFA
- Connect to your server using SSH with administrative privileges.
- Open the SSH configuration file using a text editor (e.g., nano or vi).
-
sudo vi /etc/ssh/sshd_config
-
- Look for the
ChallengeResponseAuthentication
line and set it to "yes" if not already enabled. - Add the following line to enable the use of Google Authenticator-compatible TOTP (Time-based One-Time Password) authentication:
AuthenticationMethods publickey,password publickey,keyboard-interactive
- Save and exit the SSH configuration file.
Step 3: Configuring the User for MFA
- In the SSH configuration file, find the
Match User
orAllowUsers
section for the user you want to enable MFA for. - Add the following line below the user entry:
AuthenticationMethods publickey,password publickey,keyboard-interactive
- Save and exit the SSH configuration file.
Step 4: Restarting the SSH Service
- Restart the SSH service to apply the changes.
sudo systemctl restart sshd
Step 5: Enabling MFA for the User
- On your server, generate a secret key for the user using the following command:
google-authenticator
- You will be presented with a series of prompts. Answer "y" for each of them to configure MFA.
- Scan the displayed QR code using the FreeOTP app on your smartphone.
- FreeOTP will add your server as a new account and start generating one-time passwords.
- Complete the setup process by following the on-screen instructions.
Step 6: Testing the MFA Setup
- Attempt to SSH into your server using the user account that has MFA enabled.
- After entering the username and password, you will be prompted for the verification code.
- Open the FreeOTP app on your smartphone and find the account associated with your server.
- Enter the current one-time password generated by FreeOTP.
- If the authentication is successful, you will gain access to your server.
By implementing MFA for SSH using FreeOTP, you have taken a significant step towards bolstering the security of your server. MFA provides an additional layer of protection against unauthorised access and greatly reduces the risk of compromised user credentials. Remember to enforce strong passwords and regularly update your system to maintain a robust security.
In future tutorials (when I get the time) I will show how to further harden your system with fail2ban and geoblocking.