Possible Disable Password Authentication with Google Authenticator in Linux SSH Logins?

Abstract: Disable SSH password authentication to enforce Google Authenticator two-factor login on Linux.

Created: 2026-08-02 by
You can disable password authentication for SSH logins while keeping Google Authenticator enabled by modifying the /etc/ssh/sshd_config file on your Linux server. This configuration forces users to authenticate using a time-based one-time password (TOTP) instead of traditional passwords, significantly enhancing security against brute-force attacks and credential stuffing attempts.

Prerequisites and Installation

Before altering authentication methods, ensure that the Google Authenticator PAM module is installed and functional on your system. Most modern Linux distributions provide this package in their default repositories. For Debian-based systems like Ubuntu, you can install it using sudo apt-get install libpam-google-authenticator. On Red Hat-based systems such as CentOS or Fedora, use sudo yum install google-authenticator or dnf. Once installed, each user who needs to use two-factor authentication must run the google-authenticator command. This interactive tool generates a QR code that the user scans with their mobile authenticator app (such as Google Authenticator, Authy, or Microsoft Authenticator). It also provides emergency scratch codes which should be saved securely in case the user loses access to their phone. Without this initial setup, disabling password authentication will lock users out immediately because they will have no valid second factor configured.

Configuring SSH Daemon Settings

The core of this configuration lies in editing the SSH daemon configuration file located at /etc/ssh/sshd_config. You must open this file with root privileges using a text editor like nano or vim. Look for the line that specifies ChallengeResponseAuthentication or UsePAM. In many modern configurations, especially those using systemd, UsePAM yes is already present and required for PAM modules to function correctly. You need to set ChallengeResponseAuthentication no if it is not already commented out, but more importantly, you must ensure that PasswordAuthentication no is explicitly defined. This directive tells the SSH server to reject any login attempts that rely solely on a username and password combination. If you are using key-based authentication alongside Google Authenticator, you might also want to keep PubkeyAuthentication yes enabled, allowing users to log in with keys first and then provide the TOTP code, or you can force TOTP-only access depending on your security policy. After making these changes, save the file. It is critical to test the configuration syntax before restarting the service to avoid locking yourself out. Run sudo sshd -t to check for errors. If the output is empty, the configuration is syntactically correct. If errors appear, correct them before proceeding.

Integrating Google Authenticator with PAM

To make the SSH daemon recognize the Google Authenticator module, you must edit the PAM configuration for SSH. Locate the file /etc/pam.d/sshd on most distributions. Add the following line to the end of the file: auth required pam_google_authenticator.so. This line instructs the Pluggable Authentication Modules system to require a valid TOTP code for every SSH login attempt. Depending on your specific Linux distribution and version, you may also need to modify the /etc/ssh/sshd_config to include AuthenticationMethods keyboard-interactive,pubkey or similar directives if you are combining multiple methods. However, for pure password-to-TOTP replacement, ensuring PasswordAuthentication no and adding the PAM line is usually sufficient. The keyboard-interactive method is often used in conjunction with PAM to handle the prompt for the verification code. Restart the SSH service to apply the changes. Use sudo systemctl restart sshd on systemd-based systems. Before disconnecting your current session, verify that you can still log in with the new configuration from a different terminal or device. This step prevents accidental lockouts due to misconfiguration.

Security Considerations and Best Practices

Disabling password authentication reduces the attack surface significantly, as attackers cannot guess passwords. However, you must ensure that all users have properly configured their Google Authenticator apps. Provide clear documentation on how to scan the QR code and store emergency codes. Additionally, consider implementing IP whitelisting for administrative accounts if possible, adding another layer of defense. For environments requiring high availability, monitor the SSH logs closely after implementation. Logs in /var/log/auth.log or /var/log/secure will show successful authentications and failures. If you notice unexpected denials, it may indicate that the PAM module is not loading correctly or that users have not completed the initial setup. Regularly update the libpam-google-authenticator package to benefit from security patches. Avoiding Google Authenticator Re-Authentication in Existing SSH Sessions on EC2 discusses how to manage session persistence, which complements this hardening strategy by reducing friction for legitimate users after the initial login.

Frequently Asked Questions

What happens if I lose my phone?

You can use the emergency scratch codes generated during the initial google-authenticator setup to log in. These codes are single-use and should be stored securely offline. If you lose both your phone and the scratch codes, you will need physical or console access to the server to reset the Google Authenticator configuration for your user account.

Can I use key-based authentication with Google Authenticator?

Yes, you can combine public key authentication with Google Authenticator. By setting AuthenticationMethods publickey,keyboard-interactive in sshd_config, users must first provide their SSH key and then enter the TOTP code. This two-step process offers robust security while maintaining convenience for users who manage their keys securely.

How do I troubleshoot if I get locked out?

If you are locked out, access the server via a console interface such as AWS EC2 Instance Connect, DigitalOcean Rescue Mode, or physical console access. Once logged in, temporarily comment out the auth required pam_google_authenticator.so line in /etc/pam.d/sshd and set PasswordAuthentication yes in /etc/ssh/sshd_config. Restart the SSH service to regain access, then reconfigure the settings carefully.

Does this work on all Linux distributions?

This method works on most major Linux distributions including Ubuntu, Debian, CentOS, RHEL, and Fedora, as they all support PAM and OpenSSH. Minor variations may exist in the location of configuration files or package names, but the underlying mechanism remains consistent. Always consult your distribution's documentation for specific paths if the standard locations differ.

Is Google Authenticator secure enough for production?

Google Authenticator uses Time-based One-Time Passwords (TOTP), which is an industry-standard method for two-factor authentication. When combined with strong SSH keys or complex passwords, it provides a high level of security. However, security also depends on proper implementation, such as protecting the private keys and ensuring the server itself is hardened against other vectors of attack.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More