BLOG POSTS
    MangoHost Blog / How to Set Up Multi-Factor Authentication for SSH on Ubuntu 24
How to Set Up Multi-Factor Authentication for SSH on Ubuntu 24

How to Set Up Multi-Factor Authentication for SSH on Ubuntu 24

Setting up Multi-Factor Authentication (MFA) for SSH on Ubuntu 24 is basically taking your server security from “please don’t hack me” to “good luck getting past this fortress.” While a strong password or SSH key might keep script kiddies at bay, MFA adds that crucial second layer that makes attackers work exponentially harder. You’ll learn how to implement Google Authenticator-based MFA, handle both password and key-based authentication scenarios, troubleshoot common gotchas, and understand why this setup is becoming non-negotiable for production environments.

How Multi-Factor Authentication Works with SSH

SSH MFA operates through the Pluggable Authentication Module (PAM) system, which sits between your SSH daemon and the authentication process. When you enable MFA, Ubuntu’s SSH service requires multiple authentication factors before granting access. The most common implementation uses TOTP (Time-based One-Time Password) tokens generated by apps like Google Authenticator or Authy.

Here’s the authentication flow breakdown:

  • User initiates SSH connection
  • SSH daemon checks PAM configuration
  • First factor: SSH key or password verification
  • Second factor: TOTP token verification
  • Both factors must succeed for access

The beauty of this approach is that even if someone compromises your SSH key or password, they still need access to your phone or authenticator device. The TOTP codes rotate every 30 seconds, making replay attacks virtually impossible.

Step-by-Step Implementation Guide

Let’s walk through setting up MFA using Google Authenticator’s PAM module. First, update your system and install the necessary packages:

sudo apt update
sudo apt install libpam-google-authenticator qrencode

Now configure the authenticator for your user account. Run this as the user who needs MFA (not root initially):

google-authenticator

You’ll see a series of prompts. Here are the recommended answers:

  • “Do you want authentication tokens to be time-based?” → Y
  • “Do you want me to update your ~/.google_authenticator file?” → Y
  • “Do you want to disallow multiple uses of the same authentication token?” → Y
  • “By default, tokens are good for 30 seconds” → N (unless you have clock sync issues)
  • “Do you want to enable rate-limiting?” → Y

The QR code displayed should be scanned with your authenticator app. Save the backup codes somewhere secure – you’ll need them if you lose your phone.

Next, configure PAM to use the authenticator. Edit the SSH PAM configuration:

sudo nano /etc/pam.d/sshd

Add this line at the top, right after the @include common-auth line:

auth required pam_google_authenticator.so

Now modify the SSH daemon configuration:

sudo nano /etc/ssh/sshd_config

Find and modify these lines:

ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
UsePAM yes

If you want to use password + MFA instead of key + MFA, use:

AuthenticationMethods password,keyboard-interactive

Restart SSH service to apply changes:

sudo systemctl restart ssh

Test the configuration with a new SSH session (keep your current session open as backup). You should be prompted for your SSH key passphrase (or password) followed by your verification code.

Real-World Examples and Use Cases

Here are some practical scenarios where SSH MFA proves invaluable:

Development Team Access: A startup with remote developers needs secure access to production servers. Each developer gets SSH key access plus individual MFA tokens, ensuring compromised laptops don’t automatically mean compromised servers.

Compliance Requirements: Financial services companies often mandate MFA for any system handling sensitive data. SSH MFA satisfies SOC 2 and PCI DSS requirements while maintaining developer productivity.

Emergency Access Procedures: Configure a dedicated emergency user with password + MFA authentication alongside your normal key-based access. This provides failsafe access if your SSH keys become unavailable.

# Example emergency user setup
sudo useradd -m -s /bin/bash emergency
sudo passwd emergency
# Configure MFA for emergency user
sudo -u emergency google-authenticator

Selective MFA Implementation: You can configure MFA for specific users or groups only. Create a group for MFA-required users:

sudo groupadd mfa-users
sudo usermod -a -G mfa-users username

Then modify PAM configuration to apply MFA selectively:

auth [success=1 default=ignore] pam_succeed_if.so quiet user notingroup mfa-users
auth required pam_google_authenticator.so

Comparison with Alternative MFA Solutions

Method Setup Complexity Cost Security Level User Experience
Google Authenticator PAM Medium Free High Good
Duo Security Low Paid Very High Excellent
YubiKey PAM High Hardware Cost Very High Excellent
SMS-based Medium SMS fees Medium Poor
RSA SecurID High Very Expensive High Good

Google Authenticator strikes the best balance for most use cases. Duo Security offers superior management features for enterprise environments, while YubiKeys provide the highest security for organizations handling extremely sensitive data.

Best Practices and Common Pitfalls

Always test with a backup session: The number one mistake is configuring MFA without maintaining an active SSH session for troubleshooting. If something breaks, you could lock yourself out completely.

Backup codes are critical: Store your emergency backup codes in a password manager or secure location. Lost phones happen, and backup codes are your lifeline.

Clock synchronization matters: TOTP relies on synchronized clocks. If your server’s time drifts, authentication will fail. Configure NTP:

sudo timedatectl set-ntp true
sudo systemctl enable systemd-timesyncd

Common troubleshooting scenarios:

  • “Permission denied” after entering correct codes: Check PAM configuration order and SSH daemon settings
  • “Invalid verification code”: Usually a time sync issue or typing the previous code
  • “Connection refused”: SSH daemon failed to restart due to configuration errors

To debug authentication issues, check the auth log:

sudo tail -f /var/log/auth.log

Performance considerations: MFA adds approximately 2-3 seconds to SSH connection time, primarily from user input rather than computational overhead. The PAM module itself introduces negligible latency.

Network considerations: MFA works perfectly with SSH tunneling, port forwarding, and SCP/RSYNC operations. However, automated scripts need special handling – consider using dedicated service accounts with restricted key-only access for automation.

Scaling MFA across multiple servers: For managing MFA across many servers, consider centralized solutions like FreeIPA or Active Directory integration. For smaller deployments, you can sync the .google_authenticator file across servers, though this reduces security isolation.

Remember that MFA is just one layer of defense. Combine it with fail2ban, regular security updates, proper firewall configuration, and network-level access controls for comprehensive security. The goal isn’t to make your server impenetrable – it’s to make attacking it more expensive than the potential reward.

For additional reference, check the official PAM documentation at https://www.kernel.org/pub/linux/libs/pam/ and Ubuntu’s SSH server guide at https://ubuntu.com/server/docs/service-openssh.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked