{"id":1535,"date":"2026-01-01T21:02:15","date_gmt":"2026-01-01T21:02:15","guid":{"rendered":"https:\/\/b2bhostingclub.com\/blog\/?p=1535"},"modified":"2026-02-04T14:14:37","modified_gmt":"2026-02-04T14:14:37","slug":"how-to-set-up-two-factor-authentication-for-linux-server","status":"publish","type":"post","link":"https:\/\/b2bhostingclub.com\/blog\/how-to-set-up-two-factor-authentication-for-linux-server\/","title":{"rendered":"How to Set up Two-Factor Authentication for Linux Server"},"content":{"rendered":"<div class=\"d-title pt-10 pb-0\">\n<div class=\"container\">\n<div class=\"row row-cols-1 justify-content-left d-text-left d-title-inner\">\n<h2 id=\"introduction\" class=\"col col-md-10 d-h2 d-color-black core-title\">Introduction<\/h2>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"d-paragraph-card\">\n<div class=\"container\">\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters pt-4 pb-1\">\n<div class=\"col\">\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">Setting up two-factor authentication (2FA) for a Linux server provides an additional layer of security and helps protect against unauthorized access. With 2FA, users are required to provide two forms of identification to access the server. This combination makes it significantly more difficult for attackers to gain access to the server, even if they manage to obtain or crack a user&#8217;s password.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">When implementing 2FA for a Linux server, there are various methods available, such as using time-based one-time password (TOTP) apps like Google Authenticator or hardware tokens. In this guide, we will show you how to set up two-factor authencation SSH login using Google Authenticator.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"d-title pt-10 pb-0\">\n<div class=\"container\">\n<div class=\"row row-cols-1 justify-content-left d-text-left d-title-inner\">\n<h2 id=\"prerequisites\" class=\"col col-md-10 d-h2 d-color-black core-title\">Prerequisites<\/h2>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"d-paragraph-card\">\n<div class=\"container\">\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters pt-4 pb-1\">\n<div class=\"col\">\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col col-md-12 d-color-black d-fs-normal d-fw-500 core-title\"><b>Verify Your Remote Linux Server is Ready<\/b><\/div>\n<div class=\"col col-md-12 d-color-black d-fs-normal d-fw-500\">If you have ordered one from B2BHOSTINGCLUB, you will receive an email containing the information of your VPS, including server IP and login credentials.<\/div>\n<div class=\"col\"><\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col col-md-12 d-color-black d-fs-normal d-fw-500 core-title\"><b>Install an Authentication App on Your Mobile Phone<\/b><\/div>\n<div class=\"col col-md-12 d-color-black d-fs-normal d-fw-500\">Authentication apps are used to enhance the security of online accounts by providing a second factor of authentication in addition to the traditional username and password combination. They work by generating time-based one-time passwords (TOTP) that serve as the second factor of authentication in a two-factor authentication (2FA) setup. Specifically, these apps generate a new six-digit random number every 30 seconds that applies only to a specific host entry previously set up in the app. There are several authentication apps available, such as Google Authenticator and Microsoft Authenticator. They are typically available as mobile applications for Android and iOS devices, which can be downloaded and installaed from the respective app stores.<\/div>\n<div class=\"col\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"d-title pt-10 pb-0\">\n<div class=\"container\">\n<div class=\"row row-cols-1 justify-content-left d-text-left d-title-inner\">\n<h2 id=\"set-up-two-factor-authentication-2fa-for-linux-servers\" class=\"col col-md-10 d-h2 d-color-black core-title\">Set up Two-Factor Authentication (2FA) for Linux Servers<\/h2>\n<div class=\"col col-md-10 d-color-black d-fs-normal d-fw-500\">In the example, we will install Google Authenticator for 2FA Linux. And we use Ubuntu operating system.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"d-paragraph-card\">\n<div class=\"container\">\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters pt-4 pb-1\">\n<h3 class=\"col col-md-12 d-h3 d-color-black d-fs-20 d-fw-600 d-lineheight-24 core-title\">1. Install Google Authenticator<\/h3>\n<div class=\"col\">\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">First, Log in to your Linux server. In this example, we use Ubuntu 20.04. Then, install the Google Authenticator on your Linux server using the following command.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<pre class=\"d-fs-normal d-lineheight-26 mb-0\">sudo apt install libpam-google-authenticator<\/pre>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">Next, enter y to continue the installation of the package.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<div class=\"d-text-start\"><img decoding=\"async\" class=\"d-img d-lazy-img\" src=\"https:\/\/images.cloudclusters.io\/3d59a27894bc4862963f3ccbba56c324\/install-google-authenticator.png\" alt=\"Install Google authenticator\" width=\"1300\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"d-paragraph-card\">\n<div class=\"container\">\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters pt-4 pb-1\">\n<h3 class=\"col col-md-12 d-h3 d-color-black d-fs-20 d-fw-600 d-lineheight-24 core-title\">2. Run Google Authenticator<\/h3>\n<div class=\"col\">\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">After the authenticator is installed, start it by entering the following command.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<pre class=\"d-fs-normal d-lineheight-26 mb-0\">google-authenticator<\/pre>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">You will then be prompted to decide if you want authentication tokens to be time-based. Enter &#8220;y&#8221; to accept, and you will get a QR code. Now, open the authenticator app on your mobile phone and scan this QR code to add the account to the list of computers.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<div class=\"d-text-start\"><img decoding=\"async\" class=\"d-img d-lazy-img\" src=\"https:\/\/images.cloudclusters.io\/ac2b7e5d9c8f4274a46ab59b4b654bf2\/launch-google-authenticator.png\" alt=\"Launch Google Authenticator\" width=\"1300\" \/><\/div>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<div class=\"d-text-start\"><img decoding=\"async\" class=\"d-img d-lazy-img\" src=\"https:\/\/images.cloudclusters.io\/f3bfcfad00f0436382d7cfafcf5f7956\/qrcode.png\" alt=\"Get a QR code and emergency codes\" width=\"1300\" \/><\/div>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">Below the QR code, there are emergency codes that you can use when you lose your authenticator. Please make sure to store those codes in a secure place.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">After that, you are prompted to decide on the following 4 questions. For security reasons, it&#8217;s recommended to comfirm those with yes.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<div class=\"d-text-start\"><img decoding=\"async\" class=\"d-img d-lazy-img\" src=\"https:\/\/images.cloudclusters.io\/64ad3e166c2344c586cbabcfa4fe6004\/setup-authenticator.png\" alt=\"Set up Google Authenticator\" width=\"1300\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"d-paragraph-card\">\n<div class=\"container\">\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters pt-4 pb-1\">\n<h3 class=\"col col-md-12 d-h3 d-color-black d-fs-20 d-fw-600 d-lineheight-24 core-title\">3. Configure Google Authenticator<\/h3>\n<div class=\"col\">\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">We will need to make adjustments in two confiugration files so that the two-factor authentication ssh login can be well set up. The files are &#8220;\/etc\/ssh\/sshd_config&#8221; and &#8220;\/etc\/pam.d\/sshd&#8221;<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\"><b>Update \/etc\/ssh\/sshd_config<\/b><\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">Open the file by using the following command.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<pre class=\"d-fs-normal d-lineheight-26 mb-0\">sudo nano \/etc\/ssh\/sshd_config<\/pre>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<div class=\"d-text-start\"><img decoding=\"async\" class=\"d-img d-lazy-img\" src=\"https:\/\/images.cloudclusters.io\/586ef326188247699cf5a48fdf329141\/nano.png\" alt=\"Open the sshd configure file\" width=\"1300\" \/><\/div>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">Find the fields of UsePAM and ChallengeResponseAuthentication and change their values to yes as the screenshot shows. After that, press &#8220;CTRL + X&#8221; then &#8220;Y&#8221; and followed by &#8220;Enter&#8221; to save the file.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<div class=\"d-text-start\"><img decoding=\"async\" class=\"d-img d-lazy-img\" src=\"https:\/\/images.cloudclusters.io\/8355212304b746a9ba6ae266f90530a1\/configure%20authentication.png\" alt=\"Edit the sshd configure file\" width=\"1300\" \/><\/div>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">Find the fields of UsePAM and ChallengeResponseAuthentication and change their values to yes as the screenshot shows. After that, press &#8220;CTRL + X&#8221; then &#8220;Y&#8221; and followed by &#8220;Enter&#8221; to save the file.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">Finally, we should restart the ssh service so the changes can take effect. restart the service using the command below.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<pre class=\"d-fs-normal d-lineheight-26 mb-0\">sudo systemctl restart ssh<\/pre>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\"><b>Update \/etc\/pam.d\/sshd<\/b><\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">The next step is to add the Google Authenticator to the login in the &#8220;\/etc\/pam.d\/sshd&#8221; file. Open the file using the command below.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<pre class=\"d-fs-normal d-lineheight-26 mb-0\">sudo nano \/etc\/pam.d\/sshd<\/pre>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<div class=\"d-text-start\"><img decoding=\"async\" class=\"d-img d-lazy-img\" src=\"https:\/\/images.cloudclusters.io\/c35e13e9b5f0491eaccde774950e4e29\/edit-sshd.png\" alt=\"Open the sshd file\" width=\"1300\" \/><\/div>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">Copy this &#8220;auth required pam_google_authenticator.so&#8221; and add it to the file. Lastly, press &#8220;CTRL + X&#8221; then &#8220;Y&#8221; and followed by &#8220;Enter&#8221; to save the file.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<div class=\"d-text-start\"><img decoding=\"async\" class=\"d-img d-lazy-img\" src=\"https:\/\/images.cloudclusters.io\/e2bbcb185d104be3abe592534d4aee73\/add%20auth%20required%20pam-google-authenticator.png\" alt=\"Edit the sshd file\" width=\"1300\" \/><\/div>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">Now you&#8217;ve successfully set up 2FA.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"d-paragraph-card\">\n<div class=\"container\">\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters pt-4 pb-1\">\n<h3 class=\"col col-md-12 d-h3 d-color-black d-fs-20 d-fw-600 d-lineheight-24 core-title\">4. 2FA with Password and One-Time Code<\/h3>\n<div class=\"col\">\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">Now, you can exit the SSH session and log in in a new session with 2FA.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">You will be prompted to enter both password and a verification code.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<div class=\"d-text-start\"><img decoding=\"async\" class=\"d-img d-lazy-img\" src=\"https:\/\/images.cloudclusters.io\/405cbb97cd2a4b76a9f57fdcc8612002\/2FA%20authentication.png\" alt=\"Re-login with 2FA\" width=\"1300\" \/><\/div>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">Now, open the authentication app on your smartphone and get the 6-character code. With the code input, you should be logged into the session.<\/p>\n<\/div>\n<\/div>\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<div class=\"d-text-start\"><img decoding=\"async\" class=\"d-img d-lazy-img\" src=\"https:\/\/images.cloudclusters.io\/62c2555cc0ce449d82fd8eca5a7ce349\/auth-code.png\" alt=\"A code in the authentication app\" width=\"1300\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"d-title pt-10 pb-0\">\n<div class=\"container\">\n<div class=\"row row-cols-1 justify-content-left d-text-left d-title-inner\">\n<h2 id=\"conclusion\" class=\"col col-md-10 d-h2 d-color-black core-title\">Conclusion<\/h2>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"d-paragraph-card\">\n<div class=\"container\">\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters pt-4 pb-1\">\n<div class=\"col\">\n<div class=\"row row-cols-1 justify-content-start d-text-start d-title-inner no-gutters\">\n<div class=\"col\">\n<p class=\"mb-0\">By following these steps, you establish a strong and effective 2FA setup for your Linux server, leveraging the convenience and security provided by Google Authenticator. Remember to keep your secret key and mobile device secure, as they are vital components of the authentication process.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Setting up two-factor authentication (2FA) for a Linux server provides an additional layer of security and helps protect against unauthorized access. With 2FA, users are required to provide two forms of identification to access the server. This combination makes it significantly more difficult for attackers to gain access to the server, even if they [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":1536,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1535","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-hosting"],"_links":{"self":[{"href":"https:\/\/b2bhostingclub.com\/blog\/wp-json\/wp\/v2\/posts\/1535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/b2bhostingclub.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/b2bhostingclub.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/b2bhostingclub.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/b2bhostingclub.com\/blog\/wp-json\/wp\/v2\/comments?post=1535"}],"version-history":[{"count":2,"href":"https:\/\/b2bhostingclub.com\/blog\/wp-json\/wp\/v2\/posts\/1535\/revisions"}],"predecessor-version":[{"id":1538,"href":"https:\/\/b2bhostingclub.com\/blog\/wp-json\/wp\/v2\/posts\/1535\/revisions\/1538"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/b2bhostingclub.com\/blog\/wp-json\/wp\/v2\/media\/1536"}],"wp:attachment":[{"href":"https:\/\/b2bhostingclub.com\/blog\/wp-json\/wp\/v2\/media?parent=1535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/b2bhostingclub.com\/blog\/wp-json\/wp\/v2\/categories?post=1535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/b2bhostingclub.com\/blog\/wp-json\/wp\/v2\/tags?post=1535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}