How to Setup Two-Factor Authentication For SSH In Linux
Jun 30, 2025 am 09:50 AMBy default, SSH already uses secure data communication between remote machines, but if you want to add some extra security layer to your SSH connections, you can add a Google Authenticator (two-factor authentication) module that allows you to enter a random one-time password (TOTP) verification code while connecting to SSH servers. You’ll have to enter the verification code from your smartphone or PC when you connect.
The Google Authenticator is an open-source module that includes implementations of one-time passcodes (TOTP) verification tokens developed by Google.
It supports several mobile platforms, as well as PAM (Pluggable Authentication Module). These one-time passcodes are generated using open standards created by the OATH Initiative for Open Authentication).
In this article, I will show you how to set up and configure SSH for two-factor authentication under RedHat-based and Debian-based Linux distributions such as Fedora, CentOS Stream, Rocky Linux, and AlmaLinux, Ubuntu, Debian, and Mint.
Installing Google Authenticator in Linux
Open the machine that you want to set up two-factor authentication and install the following PAM libraries along with development libraries that are needed for the PAM module to work correctly with the Google authenticator module.
On RedHat-based systems install the ‘pam-devel‘ package using the following yum command.
# yum install google-authenticator -y
On Debian-based systems install the ‘libpam0g-dev‘ package using the following apt command.
$ sudo apt install libpam-google-authenticator -y
Generate Google Authentication Tokens
Once you run the ‘google-authenticator‘ command, it will prompt you with a series of questions.
# google-authenticator
Simply type “y” (yes) as the answer in most situations. If something goes wrong, you can type again the ‘google-authenticator‘ command to reset the settings.
- Do you want authentication tokens to be time-based (y/n) y
After this question, you will get your ‘secret key‘ and ‘emergency codes‘. Write down these details somewhere, we will need the ‘secret key‘ later on to set up the Google Authenticator app.
# google-authenticator Do you want authentication tokens to be time-based (y/n) y Warning: pasting the following URL into your browser exposes the OTP secret to Google: https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@tecmint?secret=CYZF2YF7HFGX55ZEPQYLHOO5JM&issuer=tecmint Failed to use libqrencode to show QR code visually for scanning. Consider typing the OTP secret into your app manually. Your new secret key is: <strong>CYZF2YF7HFGX55ZEPQYLHOM</strong> Enter code from app (-1 to skip): <code>-1</code> Code confirmation skipped Your emergency scratch codes are: <strong>83714291 53083200 80975623 57217008 77496339</strong>
Next, follow the setup wizard and in most cases type the answer as “y” (yes) as shown below.
Do you want me to update your "/root/.google_authenticator" file (y/n) <code>y</code> Do you want to disallow multiple uses of the same authentication token? This restricts you to one login about every 30s, but it increases your chances to notice or even prevent man-in-the-middle attacks (y/n) <code>y</code> By default, tokens are good for 30 seconds and in order to compensate for possible time-skew between the client and the server, we allow an extra token before and after the current time. If you experience problems with poor time synchronization, you can increase the window from its default size of 1:30min to about 4min. Do you want to do so (y/n) <code>y</code> If the computer that you are logging into isn't hardened against brute-force login attempts, you can enable rate-limiting for the authentication module. By default, this limits attackers to no more than 3 login attempts every 30s. Do you want to enable rate-limiting (y/n) <code>y</code>
Configuring SSH to Use Google Authenticator in Linux
Open the PAM configuration file ‘/etc/pam.d/sshd‘ and add the following line to the bottom of the file.
auth required pam_google_authenticator.so nullok auth required pam_permit.so
Next, open the SSH configuration file ‘/etc/ssh/sshd_config‘ and scroll down to find the line that says.
ChallengeResponseAuthentication no
Change it to “yes“. So, it becomes like this.
ChallengeResponseAuthentication yes
Finally, restart the SSH service to take new changes.
# systemctl restart sshd Or $ sudo systemctl restart sshd
Configuring Google Authenticator App
Launch the Google Authenticator app on your smartphone. Press and choose “Enter a setup key“. If you don’t have this app, you can download and install the Google Authenticator app on your Android/iPhone/Blackberry devices.
Add your account ‘Name‘ and enter the ‘secret key‘ generated earlier.
It will generate a one-time password (verification code) that will constantly change every 30sec on your phone.
Now try to log in via SSH, you will be prompted with a Google Authenticator code (Verification code) and Password whenever you attempt to log in via SSH. You have only 30 seconds to enter this verification code, if you miss it will regenerate a new verification code.
login as: tecmint Access denied Using keyboard-interactive authentication. <strong>Verification code</strong>: Using keyboard-interactive authentication. Password: Last login: Tue Apr 23 13:58:29 2022 from 172.16.25.125 [root@tecmint ~]#
If you don’t have a smartphone, you can also use a Firefox add-on called Authenticator to do two-factor authentication.
Important: The two-factor authentication works with password-based SSH login. If you are using any private/public key SSH session, it will ignore two-factor authentication and log you in directly.
The above is the detailed content of How to Setup Two-Factor Authentication For SSH In Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Are you looking for good software to write mathematical equations? If so, this article provides the top 5 equation editors that you can easily install on your favorite Linux distribution.In addition to being compatible with different types of mathema

Linux administrators should be familiar with the command-line environment. Since GUI (Graphical User Interface) mode in Linux servers is not commonly installed.SSH may be the most popular protocol to enable Linux administrators to manage the servers

PPA is an important tool for Ubuntu users to expand their software sources. 1. When searching for PPA, you should visit Launchpad.net, confirm the official PPA in the project official website or document, and read the description and user comments to ensure its security and maintenance status; 2. Add PPA to use the terminal command sudoadd-apt-repositoryppa:/, and then run sudoaptupdate to update the package list; 3. Manage PPAs to view the added list through the grep command, use the --remove parameter to remove or manually delete the .list file to avoid problems caused by incompatibility or stopping updates; 4. Use PPA to weigh the necessity and prioritize the situations that the official does not provide or require a new version of the software.

Gogo is a remarkable tool to bookmark directories inside your Linux shell. It helps you create shortcuts for long and complex paths in Linux. This way, you no longer need to type or memorize lengthy paths on Linux.For example, if there's a directory

LXD is described as the next-generation container and virtual machine manager that offers an immersive for Linux systems running inside containers or as virtual machines. It provides images for an inordinate number of Linux distributions with support

Node Version Manager (NVM) is a simple bash script that helps manage multiple Node.js versions on your Linux system. It enables you to install various Node.js versions, view available versions for installation, and check already installed versions.NV

The key to installing dual systems in Linux and Windows is partitioning and boot settings. 1. Preparation includes backing up data and compressing existing partitions to make space; 2. Use Ventoy or Rufus to make Linux boot USB disk, recommend Ubuntu; 3. Select "Coexist with other systems" or manually partition during installation (/at least 20GB, /home remaining space, swap optional); 4. Check the installation of third-party drivers to avoid hardware problems; 5. If you do not enter the Grub boot menu after installation, you can use boot-repair to repair the boot or adjust the BIOS startup sequence. As long as the steps are clear and the operation is done properly, the whole process is not complicated.

R is a widely-used programming language and software environment designed for developing statistical and graphical computing tools within data science. It closely resembles the S programming language and environment, with R serving as an alternative
