{"id":2204,"date":"2024-01-04T14:02:40","date_gmt":"2024-01-04T08:32:40","guid":{"rendered":"https:\/\/www.veeble.org\/kb\/?p=2204"},"modified":"2025-03-10T10:48:33","modified_gmt":"2025-03-10T05:18:33","slug":"setting-up-google-2fa-for-openssh","status":"publish","type":"post","link":"https:\/\/www.veeble.com\/kb\/setting-up-google-2fa-for-openssh\/","title":{"rendered":"Setting up Google 2 Factor Authentication for\u00a0OpenSSH"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full is-resized\"><a href=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Setting-up-Google-2-Factor-Authentication-for-OpenSSH.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1366\" height=\"768\" src=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Setting-up-Google-2-Factor-Authentication-for-OpenSSH.jpg\" alt=\"Setting up Google 2 Factor Authentication for OpenSSH\" class=\"wp-image-7964\" style=\"width:749px;height:auto\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Setting-up-Google-2-Factor-Authentication-for-OpenSSH.jpg 1366w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Setting-up-Google-2-Factor-Authentication-for-OpenSSH-300x169.jpg 300w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Setting-up-Google-2-Factor-Authentication-for-OpenSSH-1024x576.jpg 1024w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Setting-up-Google-2-Factor-Authentication-for-OpenSSH-768x432.jpg 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/figure>\n\n\n\n<p>In the ever-evolving landscape of <a href=\"https:\/\/www.cisco.com\/c\/en_in\/products\/security\/what-is-cybersecurity.html#:~:text=Cybersecurity%20is%20the%20practice%20of,or%20interrupting%20normal%20business%20processes.\" target=\"_blank\" rel=\"noopener\">cybersecurity<\/a>, we face a constant challenge to fortify systems against potential threats. One pivotal aspect of this defense is the implementation of <a href=\"https:\/\/duo.com\/product\/multi-factor-authentication-mfa\/two-factor-authentication-2fa\" target=\"_blank\" rel=\"noopener\">Two-Factor Authentication (2FA)<\/a> for OpenSSH, an essential tool for securing remote access to <a href=\"https:\/\/www.veeble.org\/vps-hosting\/\" target=\"_blank\" rel=\"noopener\">servers<\/a>. In this article, I will share my journey of setting up Google 2FA for <a href=\"https:\/\/www.openssh.com\/\" target=\"_blank\" rel=\"noopener\">OpenSSH<\/a>, detailing the steps and considerations involved. By incorporating this additional layer of authentication, we can significantly enhance the security posture of our server infrastructure.<\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"1-install-the-google-authenticator\">1. Install the Google authenticator<\/h4>\n\n\n<p>The very first step is installing the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Google_Authenticator\" target=\"_blank\" rel=\"noopener\">Google Authenticator<\/a> on the server.  For this install the &#8216;libpam-google-authenticator&#8217; package to enable the Google Authenticator PAM module.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@ervintest:~# apt-get install openssh-server libpam-google-authenticator<\/code><\/pre>\n\n\n<h4 class=\"wp-block-heading\" id=\"2-configure-openssh\">2: Configure OpenSSH<\/h4>\n\n\n<p>Edit the SSH daemon configuration file to enable <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pluggable_authentication_module\" target=\"_blank\" rel=\"noopener\">PAM <\/a>authentication. Open the &#8216;\/etc\/ssh\/sshd_config&#8217; file with your preferred text editor.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@ervintest:~# vi \/etc\/ssh\/sshd_config<\/code><\/pre>\n\n\n\n<p>Ensure the following lines are uncommented:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ChallengeResponseAuthentication yes<\/code><\/pre>\n\n\n\n<p>Save and close the file, then restart the SSH service:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@ervintest:~# service ssh restart<\/code><\/pre>\n\n\n<h4 class=\"wp-block-heading\" id=\"3-configure-google-authenticator\">3: Configure Google Authenticator<\/h4>\n\n\n<p>Run the following command to configure the Google Authenticator for your user account:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@ervintest:~# google-authenticator<\/code><\/pre>\n\n\n\n<p>Follow the on-screen instructions to set up 2FA. This process includes scanning a QR code with the Google Authenticator app on your mobile device.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\" id=\"googleauthenticator\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-03-115414.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"563\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-03-115414-1024x563.png\" alt=\"\" class=\"wp-image-2227\" style=\"width:652px;height:auto\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-03-115414-1024x563.png 1024w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-03-115414-300x165.png 300w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-03-115414-768x422.png 768w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-03-115414.png 1202w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>You will see a secret key and numerous &#8220;emergency scratch codes&#8221; when you use Google Authenticator. If your secret key is lost, you can only use emergency codes once, so make sure you write them down and save them somewhere safe.<\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"4-adjust-pam-configuration\">4: Adjust PAM Configuration<\/h4>\n\n\n<p>Edit the &#8216;\/etc\/pam.d\/sshd&#8217; file to include Google Authenticator. Open the file with your text editor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@ervintest:~# vi \/etc\/pam.d\/sshd<\/code><\/pre>\n\n\n\n<p>Add the following line at the top:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>auth required <a href=\"https:\/\/www.mankier.com\/8\/pam_google_authenticator\" target=\"_blank\" rel=\"noopener\">pam_google_authenticator.so<\/a><\/code><\/pre>\n\n\n\n<p>Save and close the file.<\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"5-restart-ssh-service\">5: Restart SSH Service<\/h4>\n\n\n<p> Restart the SSH service once again to apply the changes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@ervintest:~# service ssh restart<\/code><\/pre>\n\n\n<h4 class=\"wp-block-heading\" id=\"6-using-google-authenticator-app\">6.  Using Google Authenticator app<\/h4>\n\n\n<p>Utilize the <a href=\"https:\/\/play.google.com\/store\/apps\/details?id=com.google.android.apps.authenticator2&amp;hl=en&amp;pli=1\" target=\"_blank\" rel=\"noopener\">Google Authenticator app<\/a> on your iPhone, Android, or Blackberry device to enter a secret key and generate a verification code. Also to complete it, in the Google Authenticator app, you can scan the bar code  found on the url in the above <a href=\"#googleauthenticator\">image<\/a><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"7-testing-twofactor-authentication\">7. Testing Two-Factor Authentication<\/h4>\n\n\n<p>After setting up two-factor authentication for OpenSSH, you can test it by trying to access your server from a remote computer. You will also be asked for a verification token from the Google Authenticator app on your mobile device along with your password.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/01\/testing-2FA.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"946\" height=\"474\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/01\/testing-2FA.png\" alt=\"\" class=\"wp-image-2228\" style=\"width:652px\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/testing-2FA.png 946w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/testing-2FA-300x150.png 300w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/testing-2FA-768x385.png 768w\" sizes=\"auto, (max-width: 946px) 100vw, 946px\" \/><\/a><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><\/p>\n<cite>Congratulations! You have successfully configured Google 2FA for OpenSSH on your server. This additional layer of security significantly enhances the protection of your system, ensuring that only authorized users with both a password and a time-based one-time password (TOTP) from the Google Authenticator app can access the server. Stay secure!<\/cite><\/blockquote>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-c25b8b87 wp-block-button\"><div class=\"uagb-cta__wrap\"><h3 class=\"uagb-cta__title\">Deploy Faster, Optimize Costs \u2013 AWS Hosting Simplified by Veeble!<\/h3><p class=\"uagb-cta__desc\">Maximize ROI with hassle-free AWS hosting. We optimize costs, ensure compliance, and provide round-the-clock support\u2014so you can scale confidently without technical headaches.<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/www.veeble.com\/in\/managed-aws-hosting\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_blank\" rel=\"noopener noreferrer\">Choose Your Plan<svg xmlns=\"https:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\"><path d=\"M504.3 273.6l-112.1 104c-6.992 6.484-17.18 8.218-25.94 4.406c-8.758-3.812-14.42-12.45-14.42-21.1L351.9 288H32C14.33 288 .0002 273.7 .0002 255.1S14.33 224 32 224h319.9l0-72c0-9.547 5.66-18.19 14.42-22c8.754-3.809 18.95-2.075 25.94 4.41l112.1 104C514.6 247.9 514.6 264.1 504.3 273.6z\"><\/path><\/svg><\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the ever-evolving landscape of cybersecurity, we face a constant challenge to fortify systems against potential threats. One pivotal aspect of this defense is [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":7964,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-2204","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-firewallsecurity"],"uagb_featured_image_src":{"full":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Setting-up-Google-2-Factor-Authentication-for-OpenSSH.jpg",1366,768,false],"thumbnail":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Setting-up-Google-2-Factor-Authentication-for-OpenSSH-150x150.jpg",150,150,true],"medium":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Setting-up-Google-2-Factor-Authentication-for-OpenSSH-300x169.jpg",300,169,true],"medium_large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Setting-up-Google-2-Factor-Authentication-for-OpenSSH-768x432.jpg",768,432,true],"large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Setting-up-Google-2-Factor-Authentication-for-OpenSSH-1024x576.jpg",1024,576,true],"1536x1536":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Setting-up-Google-2-Factor-Authentication-for-OpenSSH.jpg",1366,768,false],"2048x2048":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Setting-up-Google-2-Factor-Authentication-for-OpenSSH.jpg",1366,768,false]},"uagb_author_info":{"display_name":"Nayana Nair","author_link":"https:\/\/www.veeble.com\/kb\/author\/nayana\/"},"uagb_comment_info":0,"uagb_excerpt":"In the ever-evolving landscape of cybersecurity, we face a constant challenge to fortify systems against potential threats. One pivotal aspect of this defense is [&hellip;]","_links":{"self":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/comments?post=2204"}],"version-history":[{"count":10,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2204\/revisions"}],"predecessor-version":[{"id":7965,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2204\/revisions\/7965"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media\/7964"}],"wp:attachment":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media?parent=2204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/categories?post=2204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/tags?post=2204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}