{"id":13333,"date":"2019-04-01T20:29:20","date_gmt":"2019-04-01T20:29:20","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13333"},"modified":"2019-04-01T20:30:56","modified_gmt":"2019-04-01T20:30:56","slug":"how-to-setup-two-factor-authentication-google-authenticator-for-ssh-logins","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/how-to-setup-two-factor-authentication-google-authenticator-for-ssh-logins\/","title":{"rendered":"How to Setup Two-Factor Authentication (Google Authenticator) for SSH Logins"},"content":{"rendered":"<p>By default,\u00a0<strong>SSH<\/strong>\u00a0already uses a secure data communication between remote machines, but if you want to add some extra security layer to your SSH connections, you can add a\u00a0<strong>Google Authenticator<\/strong>\u00a0(<strong>two-factor authentication<\/strong>) module that allow you to enter a random one-time password (<strong>TOTP<\/strong>) verification code while connecting to\u00a0<strong>SSH<\/strong>\u00a0servers. You\u2019ll have to enter the verification code from your\u00a0<strong>smartphone<\/strong>\u00a0or\u00a0<strong>PC<\/strong>\u00a0when you connect.<\/p>\n<p>The\u00a0<strong>Google Authenticator<\/strong>\u00a0is an open-source module that includes implementations of one-time passcodes (<strong>TOTP<\/strong>) verification token developed by\u00a0<strong>Google<\/strong>. It supports several mobile platforms, as well as\u00a0<strong>PAM<\/strong>\u00a0(<strong>Pluggable Authentication Module<\/strong>). These one-time passcodes are generated using open standards created by the<strong>\u00a0OATH<\/strong>(<strong>Initiative for Open Authentication<\/strong>).<\/p>\n<div id=\"attachment_2776\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/ssh-two-factor-authentication\/ssh-two-factor-authentication-2\/\" rel=\"attachment wp-att-2776\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-2776\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/04\/SSH-Two-Factor-Authentication-617x450.jpg\" sizes=\"auto, (max-width: 617px) 100vw, 617px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/04\/SSH-Two-Factor-Authentication-617x450.jpg 617w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/04\/SSH-Two-Factor-Authentication.jpg 707w\" alt=\"SSH Two Factor Authentication\" width=\"617\" height=\"450\" aria-describedby=\"caption-attachment-2776\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-2776\" class=\"wp-caption-text\">SSH Two Factor Authentication<\/p>\n<\/div>\n<p>In this article I will show you how to setup and configure SSH for two-factor authentication under\u00a0<strong>Red Hat<\/strong>,\u00a0<strong>CentOS<\/strong>,\u00a0<strong>Fedora<\/strong>\u00a0and\u00a0<strong>Ubuntu<\/strong>,\u00a0<strong>Linux Mint<\/strong>\u00a0and\u00a0<strong>Debian<\/strong>.<\/p>\n<h3>Installing Google Authenticator Module<\/h3>\n<p>Open the machine that you want to setup two factor authentication and install following\u00a0<strong>PAM<\/strong>\u00a0libraries along with development libraries that are needed for the\u00a0<strong>PAM<\/strong>\u00a0module to work correctly with\u00a0<strong>Google authenticator<\/strong>module.<\/p>\n<p>On\u00a0<strong>Red Hat<\/strong>,\u00a0<strong>CentOS<\/strong>\u00a0and\u00a0<strong>Fedora<\/strong>\u00a0systems install the \u2018<strong>pam-devel<\/strong>\u2018 package.<\/p>\n<pre># yum install pam-devel make gcc-c++ wget<\/pre>\n<p>On\u00a0<strong>Ubuntu<\/strong>,\u00a0<strong>Linux Mint<\/strong>\u00a0and\u00a0<strong>Debian<\/strong>\u00a0systems install \u2018<strong>libpam0g-dev<\/strong>\u2018 package.<\/p>\n<pre># apt-get install libpam0g-dev make gcc-c++ wget<\/pre>\n<p>Download and extract\u00a0<strong>Google authenticator<\/strong>\u00a0module under\u00a0<strong>Home<\/strong>\u00a0directory (assume you are already logged in home directory of\u00a0<strong>root<\/strong>).<\/p>\n<pre># cd \/root\r\n# wget https:\/\/google-authenticator.googlecode.com\/files\/libpam-google-authenticator-1.0-source.tar.bz2\r\n# tar -xvf libpam-google-authenticator-1.0-source.tar.bz2<\/pre>\n<p>Type the following commands to compile and install\u00a0<strong>Google authenticator<\/strong>\u00a0module on the system.<\/p>\n<pre># cd libpam-google-authenticator-1.0\r\n# make\r\n# make install\r\n# google-authenticator<\/pre>\n<p>Once you run \u2018<strong>google-authenticator<\/strong>\u2018 command, it will prompt you with a serious of question. Simply type \u201c<strong>y<\/strong>\u201d (<strong>yes<\/strong>) as the answer in most situation. If something goes wrong, you can type again \u2018<strong>google-authenticator<\/strong>\u2018 command to reset the settings.<\/p>\n<ol>\n<li>Do you want authentication tokens to be time-based (y\/n)\u00a0<strong>y<\/strong><\/li>\n<\/ol>\n<p>After this question, you will get your \u2018<strong>secret key<\/strong>\u2018 and \u2018<strong>emergency codes<\/strong>\u2018. Write down these details somewhere, we will need the\u00a0<strong>\u2018secret key<\/strong>\u2018 later on to setup\u00a0<strong>Google Authenticator<\/strong>\u00a0app.<\/p>\n<pre>[root@tecmint libpam-google-authenticator-1.0]# google-authenticator\r\n\r\nDo you want authentication tokens to be time-based (y\/n) <strong>y<\/strong>\r\nhttps:\/\/www.google.com\/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth:\/\/totp\/root@tecmint.com%3Fsecret%3DXEKITDTYCBA2TLPL\r\nYour new <strong>secret key<\/strong> is: <strong>XEKITDTYCBA2TLPL<\/strong>\r\nYour <strong>verification code<\/strong> is 461618\r\nYour <strong>emergency scratch codes<\/strong> are:\r\n <strong> 65083399\r\n  10733609\r\n  47588351\r\n  71111643\r\n  92017550\r\n<\/strong><\/pre>\n<p>Next, follow the setup wizard and in most cases type answer as \u201c<strong>y<\/strong>\u201d (<strong>yes<\/strong>) as shown below.<\/p>\n<pre>Do you want me to update your \"\/root\/.google_authenticator\" file (y\/n) <strong>y<\/strong>\r\n\r\nDo you want to disallow multiple uses of the same authentication\r\ntoken? This restricts you to one login about every 30s, but it increases\r\nyour chances to notice or even prevent man-in-the-middle attacks (y\/n) <strong>y<\/strong>\r\n\r\nBy default, tokens are good for 30 seconds and in order to compensate for\r\npossible time-skew between the client and the server, we allow an extra\r\ntoken before and after the current time. If you experience problems with poor\r\ntime synchronization, you can increase the window from its default\r\nsize of 1:30min to about 4min. Do you want to do so (y\/n) <strong>y<\/strong>\r\n\r\nIf the computer that you are logging into isn't hardened against brute-force\r\nlogin attempts, you can enable rate-limiting for the authentication module.\r\nBy default, this limits attackers to no more than 3 login attempts every 30s.\r\nDo you want to enable rate-limiting (y\/n) <strong>y\r\n<\/strong><\/pre>\n<h3>Configuring SSH to use Google Authenticator Module<\/h3>\n<p>Open the\u00a0<strong>PAM<\/strong>\u00a0configuration file \u2018<strong>\/etc\/pam.d\/sshd<\/strong>\u2018 and add the following line to the top of the file.<\/p>\n<pre>auth       required     pam_google_authenticator.so<\/pre>\n<p>Next, open the\u00a0<strong>SSH<\/strong>\u00a0configuration file \u2018<strong>\/etc\/ssh\/sshd_config<\/strong>\u2018 and scroll for fine the line that says.<\/p>\n<pre>ChallengeResponseAuthentication no<\/pre>\n<p>Change it to \u201c<strong>yes<\/strong>\u201c. So, it becomes like this.<\/p>\n<pre>ChallengeResponseAuthentication yes<\/pre>\n<p>Finally, restart\u00a0<strong>SSH<\/strong>\u00a0service to take new changes.<\/p>\n<pre># \/etc\/init.d\/sshd restart<\/pre>\n<h3>Configuring Google Authenticator App<\/h3>\n<p>Launch\u00a0<strong>Google Authenticator<\/strong>\u00a0app in your smartphone. Press\u00a0<strong>Menu<\/strong>\u00a0and choose \u201c<strong>Setup an account<\/strong>\u201c. If you don\u2019t have this app, you can download and install\u00a0<a href=\"https:\/\/support.google.com\/accounts\/bin\/answer.py?hl=en&amp;answer=1066447\" target=\"_blank\" rel=\"nofollow noopener\">Google Authenticator<\/a>\u00a0app on your\u00a0<strong>Android\/iPhone\/Blackberry<\/strong>devices.<\/p>\n<div id=\"attachment_2772\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/ssh-two-factor-authentication\/google-authenticator-app-1\/\" rel=\"attachment wp-att-2772\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2772\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/04\/Google-Authenticator-App-1.png\" alt=\"Google Authenticator Setup Account\" width=\"320\" height=\"220\" aria-describedby=\"caption-attachment-2772\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-2772\" class=\"wp-caption-text\">Google Authenticator Setup Account<\/p>\n<\/div>\n<p>Press \u201c<strong>Enter key provided<\/strong>\u201d.<\/p>\n<div id=\"attachment_2773\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/ssh-two-factor-authentication\/google-authenticator-app-2\/\" rel=\"attachment wp-att-2773\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2773\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/04\/Google-Authenticator-App-2.png\" alt=\"Google Authenticator Secret Key\" width=\"320\" height=\"303\" aria-describedby=\"caption-attachment-2773\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-2773\" class=\"wp-caption-text\">Enter Google Authenticator Secret Key<\/p>\n<\/div>\n<p>Add your account \u2018<strong>Name<\/strong>\u2018 and enter the \u2018<strong>secret key<\/strong>\u2018 generated earlier.<\/p>\n<div id=\"attachment_2774\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/ssh-two-factor-authentication\/google-authenticator-app-3\/\" rel=\"attachment wp-att-2774\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2774\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/04\/Google-Authenticator-App-3.jpg\" alt=\"Google Authenticator Account Name\" width=\"498\" height=\"296\" aria-describedby=\"caption-attachment-2774\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-2774\" class=\"wp-caption-text\">Google Authenticator Account Name and Secret Key<\/p>\n<\/div>\n<p>It will generate one time password (<strong>verification code<\/strong>) that will constantly changing every\u00a0<strong>30sec<\/strong>\u00a0on your phone.<\/p>\n<div id=\"attachment_2775\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/ssh-two-factor-authentication\/google-authenticator-app-4\/\" rel=\"attachment wp-att-2775\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2775\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/04\/Google-Authenticator-App-4.jpg\" alt=\"Google Authenticator One Time Password\" width=\"522\" height=\"333\" aria-describedby=\"caption-attachment-2775\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-2775\" class=\"wp-caption-text\">Google Authenticator One Time Password<\/p>\n<\/div>\n<p>Now try to login via\u00a0<strong>SSH<\/strong>, you will be prompted with\u00a0<strong>Google Authenticator code<\/strong>\u00a0(<strong>Verification code<\/strong>) and\u00a0<strong>Password<\/strong>\u00a0whenever you attempt to log in via\u00a0<strong>SSH<\/strong>. You have only\u00a0<strong>30 seconds<\/strong>\u00a0to enter this verification code, if you miss it will regenerate new verification code.<\/p>\n<pre>login as: tecmint\r\nAccess denied\r\nUsing keyboard-interactive authentication.\r\n<strong>Verification code<\/strong>:\r\nUsing keyboard-interactive authentication.\r\nPassword:\r\nLast login: Tue Apr 23 13:58:29 2013 from 172.16.25.125\r\n[root@tecmint ~]#<\/pre>\n<p>If you don\u2019t have smartphone, you can also use a\u00a0<strong>Firefox<\/strong>\u00a0add-on called\u00a0<a href=\"https:\/\/marketplace.firefox.com\/app\/gauth-authenticator\/\" target=\"_blank\" rel=\"nofollow noopener\">GAuth Authenticator<\/a>\u00a0to do two-factor authentication.<\/p>\n<p><strong>Important<\/strong>: The two-factor authentication works with password based SSH login. If you are using any\u00a0<a href=\"https:\/\/www.tecmint.com\/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps\/\" target=\"_blank\" rel=\"noopener\">private\/public key SSH session<\/a>, it will ignore two-factor authentication and log you in directly.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/ssh-two-factor-authentication\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>By default,\u00a0SSH\u00a0already uses a secure data communication between remote machines, but if you want to add some extra security layer to your SSH connections, you can add a\u00a0Google Authenticator\u00a0(two-factor authentication) module that allow you to enter a random one-time password (TOTP) verification code while connecting to\u00a0SSH\u00a0servers. You\u2019ll have to enter the verification code from your\u00a0smartphone\u00a0or\u00a0PC\u00a0when &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/how-to-setup-two-factor-authentication-google-authenticator-for-ssh-logins\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Setup Two-Factor Authentication (Google Authenticator) for SSH Logins&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13333","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13333"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13333\/revisions"}],"predecessor-version":[{"id":13335,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13333\/revisions\/13335"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}