Skip to content
-
Subscribe to our Howsnip.com website & never miss our best posts. Subscribe Now!
howsnip_logo How Snip

Tech Tips You Can Trust

  • Home
  • About Us
  • Pages
    • Privacy Policy
    • Write For Us
    • Terms and Conditions
  • Sitemap
  • Contact Us
Advertise
Home ยป How to Enable SSH Two-Factor Authentication (2FA) on Ubuntu with Google Authenticator
SSH Two-Factor Authentication Howsnip
Posted inLinux

How to Enable SSH Two-Factor Authentication (2FA) on Ubuntu with Google Authenticator

Posted by Carly Medina October 22, 2026

Securing SSH access is one of the most important steps in protecting an Ubuntu server. Password-only logins can be vulnerable if credentials are leaked or stolen. By enabling two-factor authentication (2FA), you add an extra layer of security that requires both your password (or SSH key) and a time-based verification code generated on your phone.

In this tutorial, you’ll learn how to configure SSH two-factor authentication on Ubuntu using Google Authenticator and the PAM (Pluggable Authentication Module) framework. The process works for both password-based and SSH key-based authentication.

Why Use SSH Two-Factor Authentication?

With 2FA enabled, logging in through SSH requires:

  • Something you know: your password or SSH key passphrase.
  • Something you have: a one-time verification code from an authenticator app.

Even if an attacker obtains your SSH password, they still won’t be able to access the server without the verification code from your device.

Prerequisites

Before starting, make sure you have:

  • An Ubuntu server (Ubuntu 20.04 or newer is recommended)
  • SSH access with sudo privileges
  • The SSH service installed and running
  • A smartphone with Google Authenticator or Authy installed

If you’re deploying a new Ubuntu VM, using the Ubuntu Server Live ISO is recommended because it includes the SSH daemon during installation.

Verify SSH Is Running

Before making any changes, confirm that the SSH service is active:

sudo systemctl status ssh

This command displays the current status of the SSH service. Make sure the output shows:

active (running)

Step 1: Install the Google Authenticator PAM Module

Ubuntu uses PAM (Pluggable Authentication Modules) to handle authentication. Installing the Google Authenticator PAM package allows SSH to verify time-based one-time passwords (TOTP).

Update the package repository:

sudo apt update

SSH Two Factor Ubuntu Howsnip

Install the Google Authenticator PAM module:

sudo apt install libpam-google-authenticator

SSH Two Factor Ubuntu Howsnip

This package integrates Google Authenticator with the system authentication process.

Step 2: Configure Google Authenticator for Your User Account

Before proceeding, install an authenticator app such as Google Authenticator or Authy on your mobile device.

Run the setup wizard:

google-authenticator

SSH Two Factor Ubuntu Howsnip

The wizard will ask several configuration questions.

Configure Time-Based Tokens

When prompted:

Do you want authentication tokens to be time-based?

Enter:

y

Time-based tokens are the standard and recommended option for SSH authentication.

SSH Two Factor Ubuntu Howsnip

Scan the QR Code

The setup displays a QR code in the terminal.

  • Open your authenticator app.
  • Choose Add Account.
  • Scan the displayed QR code.

A new account entry will appear in your authenticator app and begin generating verification codes.

Save the Emergency Scratch Codes

The wizard generates several emergency recovery codes.

Store these codes in a safe offline location. They can be used if you lose access to your authenticator app or mobile device.

Answer the Remaining Prompts

Use the following recommended responses:

  • Prompt Recommended AnswerUpdate ~/.google_authenticator file? Yes
  • Disallow multiple uses of the same token? Yes
  • Increase the time window? No
  • Enable rate limiting? Yes

These settings help prevent token reuse and limit brute-force authentication attempts.

Step 3: Configure PAM for SSH Authentication

Now configure PAM to require Google Authenticator verification during SSH login.

Open the SSH PAM configuration file:

sudo nano /etc/pam.d/sshd

SSH Two Factor Ubuntu Howsnip

Add the following line near the top of the file, before other authentication directives:

auth required pam_google_authenticator.so

SSH Two Factor Ubuntu Howsnip

This tells PAM to request a TOTP code during authentication.

Optional: Allow Logins Without 2FA Configuration

If you want users without Google Authenticator configured to continue using password-only logins, use:

auth required pam_google_authenticator.so nullok

The nullok option makes two-factor authentication optional rather than mandatory.

Save and exit the file:

  • Ctrl + X
  • Y
  • Enter

Step 4: Configure the SSH Daemon

Next, update the SSH server configuration so SSH can work with PAM-based authentication challenges.

Open the SSH configuration file:

sudo nano /etc/ssh/sshd_config

SSH Two Factor Ubuntu Howsnip

Option 1: Password Authentication + 2FA

If you authenticate using SSH passwords, make sure the following settings are present:

  • ChallengeResponseAuthentication yes
  • UsePAM yes
  • AuthenticationMethods password,keyboard-interactive

Option 2: SSH Key Authentication + 2FA

For stronger security, combine SSH public keys with Google Authenticator.

Use:

  • ChallengeResponseAuthentication yes
  • UsePAM yes
  • AuthenticationMethods publickey,keyboard-interactive

This requires:

  • A valid SSH key.
  • A valid Google Authenticator verification code.

SSH Two Factor Ubuntu Howsnip

Pro Tip: SSH public key authentication combined with 2FA is generally considered more secure than passwords with 2FA.

Save the file and exit the editor.

Step 5: Restart the SSH Service

After modifying the configuration files, restart SSH to apply the changes.

sudo systemctl restart ssh

This reloads the SSH daemon with the new authentication settings.

To verify the service restarted successfully:

sudo systemctl status ssh

Ensure the service status remains:

active (running)

SSH Two Factor Ubuntu Howsnip

Step 6: Test SSH Two-Factor Authentication

Before closing your current SSH session, open a second terminal window and test the new configuration.

Use:

ssh username@your-server-ip

SSH Two Factor Ubuntu Howsnip

Replace:

  • username with your Ubuntu username
  • your-server-ip with your server’s IP address

If everything is configured correctly, SSH will prompt for:

  • Password:

Followed by:

  • Verification code:

Enter the current code from your authenticator app.

If authentication succeeds, you’ll be logged in normally.

Important: Always keep your original SSH session open while testing new SSH authentication settings. This prevents accidental lockouts if a configuration issue occurs.

Troubleshooting Tips

a) SSH Login Stops Working

If you’re unable to log in after enabling 2FA:

  • Keep the existing SSH session open.
  • Review /etc/pam.d/sshd for syntax errors.
  • Verify settings in /etc/ssh/sshd_config.
  • Confirm that your Google Authenticator account is generating valid codes.
  • Ensure the server time is synchronized correctly, as TOTP codes rely on accurate system time.

b) Lost Your Authenticator Device

  • If you lose access to your phone, use one of the emergency scratch codes generated during setup. This is why securely storing the recovery codes is critical.

Conclusion

Adding Google Authenticator to SSH significantly improves the security of your Ubuntu server by requiring a second authentication factor during login. By configuring PAM and SSH correctly, you can protect your server from unauthorized access even if passwords are compromised.

For the strongest security setup, use SSH public key authentication combined with Google Authenticator 2FA, ensuring that both a trusted key and a time-based verification code are required for every login.

Tags:
Google Authenticator UbuntuLinux SSH authenticationPAM Google Authenticatorsecure SSH loginSSH 2FA UbuntuSSH Google Authenticator setupSSH MFA UbuntuSSH two-factor authentication UbuntuUbuntu server securityUbuntu SSH hardening
Carly Medina
Carly Medina is the voice behind howsnip.com, where she shares practical insights, tips, and inspiration to help readers simplify life and achieve more every day.
View All Posts

Post navigation

Previous Post
[PHP2Shell] How to Check PHP Servers for CVE-2026-109618 and CVE-2026-109619 [PHP2Shell] How to Check PHP Servers for CVE-2026-109618 and CVE-2026-109619
Next Post
How to Transfer Files Securely Using SCP on Linux SCP Transfer Files Securely Howsnip
Related posts
SCP Transfer Files Securely Howsnip
Posted inLinux

How to Transfer Files Securely Using SCP on Linux

Posted by Carly Medina
Install Secure MySQL Howsnip
Posted inLinux

How to Install and Secure MySQL on Ubuntu

Posted by Carly Medina
MongoDB Howsnip
Posted inLinux

How to Install MongoDB and Create Your First Database and Collection

Posted by Carly Medina
Search
Categories
  • AI/ML 5
  • Cryptocurrency 1
  • Linux 79
  • Programming 11
  • Tech 35
  • Windows 11
Topattop_Banner
Recent Articles
  • How to Transfer Files Securely Using SCP on Linux
  • How to Enable SSH Two-Factor Authentication (2FA) on Ubuntu with Google Authenticator
  • [PHP2Shell] How to Check PHP Servers for CVE-2026-109618 and CVE-2026-109619
  • How to Install and Secure MySQL on Ubuntu
  • Major Cybersecurity Incidents: eMudhra, SBI Data Breach, PHP2Shell, Node.js and LiteLLM Security Risks
  • eMudhra Certificate Compromise Triggers Global Security Response After Eight CA Keys Exposed
  • How to Install MongoDB and Create Your First Database and Collection
  • Linux Head Command Tutorial – How to View the First Lines and Bytes of Files
  • How to Install and Configure Elasticsearch 8 on Linux
  • Wireshark Investigation Guide – Essential Filters for Incident Response and Malware Analysis
Useful Links
  • About Us
  • Advertise With Us
  • Contact Us
  • Privacy Policy
  • Sitemap
  • Terms and Conditions
  • Write For Us
Tech
  • [PHP2Shell] How to Check PHP Servers for CVE-2026-109618 and CVE-2026-109619
  • Major Cybersecurity Incidents: eMudhra, SBI Data Breach, PHP2Shell, Node.js and LiteLLM Security Risks
  • eMudhra Certificate Compromise Triggers Global Security Response After Eight CA Keys Exposed
  • Simple SIEM Home Lab with Elastic Cloud – Setup and Monitoring Guide
  • Apple iOS 27 Is Here – 9 Exciting New Features Every iPhone User Should Try
  • How to Detect CVE-2026-89049 Attacks Using AWS CloudTrail
  • 25 Commonly Asked AppSec & InfoSec MCQs with Answers and Explanations
  • 97 Essential JSON Test Cases for Authentication Endpoint Verification
  • 20+ SSL Certificate Decoder Websites to Check Certificate Details
  • Outbid.lol Alternatives – The Viral Pay-to-Rank Trend Taking Over the Internet
Linux
  • How to Transfer Files Securely Using SCP on Linux
  • How to Enable SSH Two-Factor Authentication (2FA) on Ubuntu with Google Authenticator
  • How to Install and Secure MySQL on Ubuntu
  • How to Install MongoDB and Create Your First Database and Collection
  • Linux Head Command Tutorial – How to View the First Lines and Bytes of Files
  • How to Install and Configure Elasticsearch 8 on Linux
  • Wireshark Investigation Guide – Essential Filters for Incident Response and Malware Analysis
  • Linux SED Command Tutorial – A Beginner-Friendly Guide with Practical Examples
  • Netstat Command in Linux – Installation, Syntax, and Practical Examples
  • How to Use the curl Command in Linux – Installation, Examples, API Requests, and Troubleshooting
Programming
  • Learn CRUD Operations in PHP Using PDO and MySQL in 15 Practical Steps
  • 25 Essential JavaScript Acronyms Every Developer Should Know
  • Top 100 JavaScript MCQs Every Full Stack Developer Should Know
  • 30-Day MERN Stack Challenge Roadmap to Become a Full-Stack Developer
  • Top 20 Advanced SQL Commands You Need To Know
  • 6 Essential SQL Operations Every Cybersecurity Professional Must Know
  • Build a Simple AJAX-Based Comment System in PHP and MySQL (With CAPTCHA Verification)
  • Top 10 Tricky SQL Interview Questions (With Examples and Explanations)
  • 100 HTML/CSS/JavaScript Interview Questions with Answers
  • 50+ Essential Web Development Abbreviations You Need to Know in 2025
Copyright 2026 - How Snip. All rights reserved.
Scroll to Top