How to enable two-factor authentication for SSH (2024)

This article describes how to enable and disable two-factor authentication for SSH on an unmanaged server.

This article only applies to products listed in the Article Details sidebar. You must have root access to the server to follow these procedures.

Table of Contents
  • About two-factor authentication
  • Enabling two-factor authentication for SSH
    • Step 1: Disable root logins for SSH
    • Step 2: Install an authenticator app on a mobile device
    • Step 3: Enable two-factor authentication on the server
  • Disabling two-factor authentication for SSH

About two-factor authentication

By default, when users access your unmanaged VPSor dedicated server using SSH, they type a username and password to log in.

Two-factor authentication provides an extra layer of security because, in addition to knowing the correct username and password, users must provide another piece of information. This piece of information is a temporary, numeric password generated independently on the server and on a mobile device, such as a smartphone or tablet.

As a result, in order for a potential attacker to access your server, he or she would not only need to obtain something you know (your username and password), but also something you have (your mobile device). This two-factor approach to logins significantly enhances your server's security and makes brute-force attacks much more difficult.

Enabling two-factor authentication for SSH

To enable two-factor authentication with SSH on your server, do the following steps in the order in which they are presented.

Step 1: Disable root logins for SSH

If you haven't done so already, you should disable root SSH logins on your server and create a normal user account. For information about how to do this, please see this article.

Step 2: Install an authenticator app on a mobile device

You use an authenticator app on your smartphone or tablet to generate a one-time password that you use to log in (this is the “second factor” for authentication, in addition to your username and password).

Step 3: Enable two-factor authentication on the server

To enable two-factor authentication on the server, follow these steps:

  1. Log in to your server using SSH.
  2. As the root user, install the Google Authenticator package:
    • For Debian and Ubuntu, type the following command:
      apt-get install libpam-google-authenticator
    • For AlmaLinux and Fedora, type the following command:

      yum install google-authenticator

    Google develops and maintains the Google Authenticator code, but it does not collect any information from your server.

  3. Stay logged in as the root user, and then in a separate window, log in to your server as a normal (that is, non-root) user.
  4. At the command prompt, type the following command:

    google-authenticator
  5. At the Do you want authentication tokens to be time-based? prompt, type y and then press Enter.
  6. The server generates a QR code image and emergency codes. On your mobile device, scan the QR code image and configure the account.

    Alternatively, if your device does not support reading QR codes, you can manually enter the alphanumeric secret key.

    Make sure you store the secret key and emergency codes in a safe place. You must have one of these emergency codes if you ever lose your mobile device, or if it is inaccessible for some reason. Otherwise, you will be unable to access your account.

  7. On the server, at the Do you want me to update your “/home/username/.google_authenticator” file? prompt, type y and then press Enter.
  8. At the Do you want to disallow multiple uses of the same authentication token? prompt, type y and then press Enter.
  9. At the By default, tokens are good for 30 seconds… prompt, type n and then press Enter.

    As the full prompt states, if you experience time synchronization problems (that is, you enter a code from your mobile device, but the server rejects it), you can increase the token validity interval from its default length of 90 seconds to around four minutes. The default value should work fine in most cases however, and is more secure.

  10. At the Do you want to enable rate-limiting? prompt, type y and then press Enter.
  11. As the root user, open the /etc/pam.d/sshd file in your preferred text editor.

    • Add the following line to the top of the file:
      auth required pam_google_authenticator.so nullok
    • Save your changes to the sshd file.

    The nullok setting enables users that have not configured two-factor authentication yet to still log in using only a username and password. It is a good idea to use this setting when you configure two-factor authentication for the first time. After you configure two-factor authentication for all of your users, you can remove the nullok setting to make two-factor authentication mandatory.

  12. As the root user, open the/etc/ssh/sshd_config file in your preferred text editor.

    • Locate the following line:
      ChallengeResponseAuthentication no
    • Modify the line as follows:

      ChallengeResponseAuthentication yes
    • Save your changes to the sshd_config file.
  13. As the root user, restart the SSH service:

    • For Debian and Ubuntu, type the following command:
      service ssh restart
    • For AlmaLinux and Fedora, type the following command:

      service sshd restart
  14. While still logged in as the root user, in a separate window log in as the normal user and test the new configuration:

    • At the Verification code prompt, type the numeric password displayed by the authenticator app on your mobile device.
    • At the Password prompt, type the password for the user.

    If authentication fails, verify that you followed the previous steps correctly. Do not log out as the root user until you are sure the new authentication configuration is working correctly.

Disabling two-factor authentication for SSH

If you decide that you no longer want to use two-factor authentication with SSH, you can disable it. To do this, follow these steps:

  1. Log in to your server using SSH.
  2. As the root user, open the /etc/pam.d/sshd file in your preferred text editor.
    • Delete or comment out the following line at the top of the file:
      auth required pam_google_authenticator.so nullok
    • Save your changes to the sshd file.
  3. Open the/etc/ssh/sshd_config file in your preferred text editor.

    • Locate the following line:
      ChallengeResponseAuthentication yes
    • Modify the line as follows:

      ChallengeResponseAuthentication no
    • Save your changes to the sshd_config file.
  4. Restart the SSH service:

    • For Debian and Ubuntu, type the following command:
      service ssh restart
    • For AlmaLinux and Fedora, type the following command:

      service sshd restart

    Two-factor authentication is now disabled for SSH.

How to enable two-factor authentication for SSH (2024)
Top Articles
What are the Different Types of Shares
Best VPN for Traveling in 2024 (Really Works Abroad) | ExpressVPN
Joi Databas
<i>1883</i>'s Isabel May Opens Up About the <i>Yellowstone</i> Prequel
Vanadium Conan Exiles
Strange World Showtimes Near Amc Braintree 10
Remnant Graveyard Elf
104 Presidential Ct Lafayette La 70503
The Shoppes At Zion Directory
Cbs Trade Value Chart Fantasy Football
The ULTIMATE 2023 Sedona Vortex Guide
111 Cubic Inch To Cc
Sound Of Freedom Showtimes Near Cinelux Almaden Cafe & Lounge
Craigslist Toy Hauler For Sale By Owner
Craigslist Portland Oregon Motorcycles
Watch The Lovely Bones Online Free 123Movies
How your diet could help combat climate change in 2019 | CNN
Ivegore Machete Mutolation
Ford F-350 Models Trim Levels and Packages
Menus - Sea Level Oyster Bar - NBPT
Hood County Buy Sell And Trade
Wnem Tv5 Obituaries
Avatar: The Way Of Water Showtimes Near Maya Pittsburg Cinemas
2021 MTV Video Music Awards: See the Complete List of Nominees - E! Online
Meet the Characters of Disney’s ‘Moana’
10 Best Places to Go and Things to Know for a Trip to the Hickory M...
Jailfunds Send Message
Core Relief Texas
Craftsman Yt3000 Oil Capacity
Google Flights To Orlando
Publix Daily Soup Menu
Kattis-Solutions
Junior / medior handhaver openbare ruimte (BOA) - Gemeente Leiden
THE 10 BEST Yoga Retreats in Konstanz for September 2024
Etowah County Sheriff Dept
Foolproof Module 6 Test Answers
World History Kazwire
7543460065
Heelyqutii
Tiny Pains When Giving Blood Nyt Crossword
Top 25 E-Commerce Companies Using FedEx
Scarlet Maiden F95Zone
Joey Gentile Lpsg
Todd Gutner Salary
Payrollservers.us Webclock
Love Words Starting with P (With Definition)
Oklahoma City Farm & Garden Craigslist
Terrell Buckley Net Worth
New Zero Turn Mowers For Sale Near Me
1Tamilmv.kids
Powah: Automating the Energizing Orb - EnigmaticaModpacks/Enigmatica6 GitHub Wiki
Elizabethtown Mesothelioma Legal Question
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 5879

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.