How to implementing 2FA for SSH on Rocky Linux 8
Understanding Two-Factor Authentication for SSH
As security breaches become increasingly sophisticated, implementing robust security measures is crucial. Two-factor authentication (2FA) adds an extra layer of security to
SSH (Secure Shell) sessions, ensuring that only authorized users gain access. Rocky Linux 8, a community enterprise operating system, provides a secure environment for the deployment of services and applications, include secure remote access with SSH.
Setting Up 2FA on Rocky Linux 8
To set up 2FA on Rocky Linux 8 for SSH, we will use Google Authenticator or Microsoft Authenticator as the Time-based One-Time Password (TOTP) provider. The process involves installing the Google Authenticator PAM (Pluggable Authentication Module) and configuring SSH to use this module.
Step 1: Install Google Authenticator PAM
Install
epel-releaseand update your package list:sudo dnf -y install epel-release sudo dnf -y updateInstall
libqrencodepackagesudo dnf -y install qrencode-libsInstall Google Authenticator
sudo dnf -y install google-authenticator
Step 2: Disable the SELinux
Check if
SELinuxis enabled or disabled. In a default RL8 installation, it will be Enforcing. Commenting outSELINUX=enforcingorSELINUX=permissive, and adding the lineSELINUX=diabled. Then reboot the server after saving the file.... #SELINUX=enforcing SELINUX=disabled ...Reboot Rocky Linux 8.
Step 3: Run the Google Authenticator
Run the following command to configure 2FA for your user:
Switch to the PAM User (e.g. admin):
su - adminRun Google Authenticator
google-authenticatorYou will prompted with a series of questions:
Do you want authentication tokens to be time-based (y/n)? → Type
yand press Enter.You will see a QR code. Scan it with your Google Authenticator or Microsoft Authenticator app (or any TOTP-based app).
Copy and save the backup codes displayed.
Do you want me to update your "
~/.google_authenticator" file? (y/n) → Typeyand press Enter.Do you want to disallow multiple uses of the same authentication token? (y/n) → Type
yand press Enter.Do you want to increase the time window? (y/n) → Type
nand press Enter.Do you want to enable rate-limiting? (y/n) → Type
yand press Enter.
Ensure
Step 4: Configure PAM to Use Google Authenticator
Open the PAM configuration file:
sudo vi /etc/pam.d/sshdAdd the following line at the top of the file:
auth required pam_google_authenticator.so auth required pam_unix.soe.g.
#%PAM-1.0 auth required pam_google_authenticator.so auth required pam_unix.so auth substack password-auth auth include postlogin account required pam_sepermit.so account required pam_nologin.so account include password-auth password include password-auth # pam_selinux.so close should be the first session rule session required pam_selinux.so close session required pam_loginuid.so # pam_selinux.so open should only be followed by sessions to be executed in the user context session required pam_selinux.so open env_params session required pam_namespace.so session optional pam_keyinit.so force revoke session optional pam_motd.so session include password-auth session include postloginSave and exit the file.
Step 5: Enable 2FA for SSH
Open the SSH daemon configuration file:
sudo vi /etc/ssh/sshd_configFind the line:
ChallengeResponseAuthentication noChange it to:
ChallengeResponseAuthentication yesFind the line:
UsePAM noChange it to:
UsePAM yesSave and exit.
Step 6 Restart SSH Service
Restart SSH to apply the changes:
sudo systemctl restart sshdStep 7: Test 2FA Authentication
Open a new terminal and try to SSH into the server:
ssh admin@your-server-ipYou should be prompted for:
Your password
A verification code from the Google Authenticator app.
Step 8: (Optional) Require 2FA for root & Specific Users
Open
/etc/security/access.confsudo vi /etc/security/access.confAdd the following lines to restrict access to only users with 2FA:
-:ALL EXCEPT username root:ALLReplace
usernamewith the actual username you want to allow.Save and exit.
Step 9: Ensure You have Backup Access
Add a secondary SSH key-based authentication in case you loss access.
Test the configuration before logging out.
Keep your energency backup codes safe.
Step 10: Disable Password Authentication (After Successful Testing)
To enforce SSH key + 2FA only, disable password login:
Open /etc/ssh/sshd_configsudo vi /etc/ssh/sshd_configSet:PasswordAuthentication noRestart SSH:sudo systemctl restart sshd
Conclusion
Adding two-factor authentication to your SSH sessions on Rocky Linux 8 is straightforward and significantly improves security. By following the steps outlined in this guide, you can protect your remote connections with an additional authentication factor, making it much harder for malicious actors to compromise your systems.
© DataCore Software Corporation. · https://www.datacore.com · All rights reserved.