Skip to content

TechDirectArchive

Hands-on IT, Cloud, Security, Veeam & DevOps

  • Home
  • About
  • Advertise With US
  • Reviews
  • Tech News
  • Contact
  • Toggle search form

Set Two-Factor Authentication for SSH in Linux

Posted on 07/02/202314/03/2023 Imoh Etuk By Imoh Etuk No Comments on Set Two-Factor Authentication for SSH in Linux
  1. Home
  2. Linux
  3. Set Two-Factor Authentication for SSH in Linux
2FA-For-SSH-in-Linux

The digital revolution, remote work, and the cloud have advanced the evolution of two-factor authentication and multi-factor authentication, and single sign-on (SSO) solutions. SSO is all about providing users with access to their resources through a single sign-on authentication. 2FA and Multi-Factor Authentication (MFA) are similar in that they both add a second layer of security to a system. While MFA uses two or more of these checkpoints, two-factor authentication uses only two of these methods to verify and authorize a user’s login attempts. In this guide, you will learn how to set Two-Factor Authentication for SSH in Linux.

Why add an extra layer of Security?

Adding an extra layer of security to remote systems has become very necessary because there has been a significant increase in cyberattacks and credential theft attacks. In a Linux system environment, SSH, or secure shell, is frequently used to establish connections to remote Linux systems. Because we frequently use it to connect to computers containing sensitive data, it is recommended that another security layer be added. Two-factor authentication (2FA) is a perfect second security layer that works well in this condition. In this article, you will learn how to Enable Two-Factor Authentication for SSH in Linux.

Two-factor authentication adds an extra layer of security because users must provide additional information in addition to their username and password. This data is a temporary, numeric password that is generated independently on the server. In this article, you will learn how to enable Two-Factor Authentication for SSH in Linux.

You may be interested in reading about the following related posts; Rolling out Multi-factor Authentication – Recommendations, Microsoft Azure Multi-Factor Authentication (MFA) Errors associated with Pleasant Password RDP SSO and SSH SSO, How to configure and use Pleasant Password RDP SSO, and How to fix Sign-in issues, non-routable domain, invalid username, and password for Single Sign On.

What is 2FA?

Two-factor authentication (2FA) is the most basic and effective method of adding a secure layer of authentication to the login credentials. After entering their credentials, users must verify their identity using an independent factor (email, SMS, security questions, social profile, etc.). The use of these protocols limits suspicious login attempts to the system, even if the consumer’s password is compromised maliciously.

What is Secure Socket Shell (SSH)?

Secure Socket Shell (SSH), also simply known as Secure Shell, is a cryptographic protocol that allows secure access to remote servers and devices over the internet. It is based on public key cryptography and provides a mechanism for mutual authentication between the server and the client as well as an encrypted channel of communication over an unsecured network.

Prerequisite for setting up Two-Factor Authentication for SSH in Linux

To get along with me in this post, you need to have the following tools available:

  • A computer running Ubuntu 20.04 LTS or above,
  • A phone running Android or iOS,
  • A configured SSH connection.

Enabling Two-Authentication for SSH

In order to set Two-Factor Authentication for SSH in Linux, do the following:

Step 1 – Install and configure the required packages. Do this by starting a terminal session and running the below command to install the Google Authenticator PAM module:

sudo apt install libpam-google-authenticator
Installing-Google-Authenticator-App
Installing Google Authenticator App

After installing it, stay logged in as the root user, and then switch user command (su) to switch, log in to your server as a normal (that is, non-root) user and type:

google-authenticator

When the prompt, “Do you want authentication tokens to be time-based?”  displays, type y and then press Enter.

Type-Yes-
Setting the Time-based Authentication Tokens

The system creates a QR code image as well as emergency scratch codes. Scan the QR code image with your mobile device to set up the account.

Emergency-Scratch-Codes
Scanning the QR Code Image
Keep the secret key and emergency codes in a secure location. If you ever lose your mobile device or it becomes inoperable for any reason, you must have one of these emergency codes. You will be unable to access your account otherwise.

On the system, you will receive the "Do you want me to update your "/home/<username>/.google_authenticator?" file on the server? prompt, enter y, and then click OK. You will also receive the prompt, "Do you want to disallow multiple users from using the same authentication token? prompt, enter y, and then click OK. Repeat the same steps for all other prompts by typing y and then pressing the Enter key.

Configuring-the-Google-App
Configuring the Authentication Prompts

Configuring SSH on Linux

Next, switch the user to the root user, and open the /etc/pam.d/sshd file in your preferred text editor and add the following line to the top of the file

auth required pam_google_authenticator.so nullok
Adding-the-auth-athentication-file
Adding a file to /etc/pam.d/sshd file

Save and exit the editor.

The nullok command as added to the command above allows users who have not yet enabled two-factor authentication to log in with just a username and password. When configuring two-factor authentication for the first time, it is a good idea to use this setting. Once you’ve configured two-factor authentication for all of your users, you can remove the nullok setting to make it mandatory.

The next thing in configuring the SSH is to restart the sshd daemon by running the below command:

sudo systemctl restart sshd.service

Modify /etc/ssh/sshd_config – change ChallengeResponseAuthentication from no to yes, so this part of the file looks like this:

Modifying-the-Challenge-Response-Auth
Modifying the Challenge Response Authentication File

Save the above file and exit your editor. As the root user, restart the SSH service by running:

service ssh restart

Test the two-factor authentication for SSH

Now, log in as a normal user by switching users. Enter the numeric password displayed by the authenticator app on your mobile device at the Verification code prompt.

Finally, to log in, enter the user’s password at the Password prompt.

Disabling two-factor authentication for SSH

You can disable two-factor authentication with SSH if you no longer want to use it. Follow these steps to accomplish this:

Step 1 – Log in to your server using SSH. After you have logged in, pen the /etc/pam.d/sshd file in your preferred text edito

Passing-Comments
Disabling the 2FA

Save and exit the editor.

Step 2 – Open the /etc/ssh/sshd_config file in your preferred text editor and change the ChallengeResponseAuthentication yes to no:

Modifying-the-Challenge-Response-Auth.2jpg
Disabling 2FA

Lastly, run service sshd restart with the sudo privileges. This disables 2FA from your computer completely.

I hope you found this blog post helpful. In this article, you have learned how to Set Two-Factor Authentication for SSH in Linux. If you have any questions, please let me know in the comment session.

Rate this post

Thank you for reading this post. Kindly share it with others.

  • Share on X (Opens in new window) X
  • Share on Reddit (Opens in new window) Reddit
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Facebook (Opens in new window) Facebook
  • Share on Pinterest (Opens in new window) Pinterest
  • Share on Tumblr (Opens in new window) Tumblr
  • Share on Telegram (Opens in new window) Telegram
  • Share on WhatsApp (Opens in new window) WhatsApp
  • Share on Mastodon (Opens in new window) Mastodon
  • Share on Bluesky (Opens in new window) Bluesky
  • Share on Threads (Opens in new window) Threads
  • Share on Nextdoor (Opens in new window) Nextdoor
Linux Tags:login, SSH, Ubunt 20.04 LTS, Ubuntu, Ubuntu 20.04 LTS

Post navigation

Previous Post: Get a list of installed programs locally or remotely in Windows
Next Post: Run Python Script via Windows Task Scheduler

Related Posts

  • How to Set Up and Configure a Squid Proxy Server Linux
  • Feature image nagios
    How to Install and Configure Nagios on Ubuntu Linux
  • mailx
    [MAILX ERROR: STATUS=BOUNCED] Fixing Mailx error when sending emails from Command line Linux
  • integrate VHR with VBR
    How to deploy and integrate VHR with VBR Backup
  • Jenkins
    How to Install Jenkins Automation Server on Ubuntu 20.04 LTS Linux
  • screenshot 2020 04 23 at 00.30.09
    How to mount a USB Drive in Linux Linux

More Related Articles

How to Set Up and Configure a Squid Proxy Server Linux
Feature image nagios How to Install and Configure Nagios on Ubuntu Linux
mailx [MAILX ERROR: STATUS=BOUNCED] Fixing Mailx error when sending emails from Command line Linux
integrate VHR with VBR How to deploy and integrate VHR with VBR Backup
Jenkins How to Install Jenkins Automation Server on Ubuntu 20.04 LTS Linux
screenshot 2020 04 23 at 00.30.09 How to mount a USB Drive in Linux Linux

Leave a Reply Cancel reply

You must be logged in to post a comment.

Microsoft MVP

VEEAMLEGEND

vexpert-badge-stars-5

Virtual Background

GoogleNews

Categories

veeaam100

Veeam Vanguard

  • sid5
    How to Find Security Identifier on Windows [Part 2] Windows
  • banner 1
    Deploying Next.Js App Using Heroku Cloud Application Platform Version Control System
  • FEATURE IMAGE
    SSH into a VM created using Azure CLI or GUI Linux
  • Database Connection Stuck on Working on it
    How to fix TeamPass stuck on working on it Network | Monitoring
  • image 63
    Protect Remote Desktop credentials with Windows Defender Remote Credential Guard or Restricted Admin Mode Windows
  • download
    How to import Users and Groups into Pleasant Password Manager Password Manager
  • How to Enable High Dynamic Range on Windows 11
    How to Enable High Dynamic Range on Windows 11 Windows
  • macos app switcher shortcuts
    How to enable or disable automatic software updates on Mac Mac

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,779 other subscribers
  • RSS - Posts
  • RSS - Comments
  • About
  • Authors
  • Write for us
  • Advertise with us
  • General Terms and Conditions
  • Privacy policy
  • Feedly
  • Telegram
  • Youtube
  • Facebook
  • Instagram
  • LinkedIn
  • Tumblr
  • Pinterest
  • Twitter
  • mastodon

Tags

Active Directory Azure Bitlocker Microsoft Windows PowerShell WDS Windows 10 Windows 11 Windows Deployment Services Windows Server 2016

Copyright © 2026 TechDirectArchive

Loading Comments...

You must be logged in to post a comment.