Securing SSH (Port 22) with Multi-Factor Authentication (MFA)
This article explains how to secure SSH (Port 22) with Multi-Factor Authentication (MFA), enhancing the security of remote access and preventing brute-force attacks.
SSH (Secure Shell) access is one of the most important methods for managing remote servers, but leaving Port 22 open to the internet can make your servers vulnerable to brute-force attacks. Fortunately, by implementing Multi-Factor Authentication (MFA), you can significantly enhance the security of your SSH login process.
In this guide, I’ll show you how to secure your SSH access by adding an extra layer of protection using Google Authenticator and Time-based One-Time Passwords (TOTP). These steps will ensure that even if your private SSH key is compromised, unauthorized access to your server will remain highly unlikely.
Why Use MFA for SSH?
-
Extra Layer of Protection: MFA adds a second form of authentication (a dynamic OTP from your phone) to the SSH login process. Even if your SSH key is stolen, attackers still need the OTP.
-
Brute-Force Attack Protection: MFA makes brute-force attacks ineffective because attackers can't simply guess your SSH credentials—they also need access to your authenticator app.
-
Improved Security: MFA combines something you know (SSH key) with something you have (an authenticator app), creating stronger protection for your server.
Steps to Secure SSH with MFA:
Step 1: Install Google Authenticator PAM Module
The first step is to install the Google Authenticator PAM (Pluggable Authentication Module) on your server, which will enable the TOTP functionality.
Commands for installation:
- Install the PAM module:
For Debian/Ubuntu systems:
Commands:-
sudo apt-get update
sudo apt-get install libpam-google-authenticator
For CentOS/RHEL systems:
Command:- sudo yum install google-authenticator
- Install Google Authenticator app: On your phone, install the Google Authenticator app (available on both Android and iOS) or use any TOTP-compatible authenticator app like Authy.
Step 2: Configure Google Authenticator for Each User
After installing the PAM module, you'll need to configure Google Authenticator for the user accounts that require MFA.
Commands for user setup:
- Run Google Authenticator for the user account:
Commands:- google-authenticator
-
This command will prompt you with several questions. Here’s a breakdown of the important steps:
-
Do you want to update your "/home/user/.google_authenticator" file?
- Answer yes to generate a configuration file.
-
Do you want me to disallow multiple uses of the same authentication token?
- Answer yes for better security.
-
Do you want to enable rate-limiting?
- Answer yes to prevent brute-force attacks.
-
-
After answering these questions, Google Authenticator will generate a QR code. Use your Google Authenticator app (or another TOTP app) to scan this QR code. This will sync your phone with your server for TOTP generation.
-
The app will generate a unique, time-based code every 30 seconds that will be used during the SSH login.
Step 3: Configure PAM for MFA
Next, modify the PAM configuration file for SSH to enforce MFA.
- Edit the PAM SSH configuration:
What's Your Reaction?