一般来说,使用ssh远程登录服务器,只需要输入账号和密码,显然这种方式不是很安全。为了安全着想,可以使用GoogleAuthenticator(谷歌身份验证器),以便在账号和密码之间再增加一个验证码,只有输入正确的验证码之后,再输入密码才能登录。这样就增强了ssh登录的安全性。账号、验证码、密码三者缺一个都不能登录,即使账号和密码正确,验证码错误,同样登录失败。其中,验证码是动态验证码,并且是通过手机客户端自动获取(默认每隔30秒失效一次)

Google Authenticator开源版主页 https://github.com/google/google-authenticator

一、Ubuntu安装

1、安装Google Authenticator

apt-get install libpam-google-authenticator

也可以通过源码编译安装

git clone https://github.com/google/google-authenticator.git
cd google-authenticator/libpam/
./bootstrap.sh
./configure
make
make install

注意:在Debian7中执行./configure时可能存在以下错误提示

configure: error: Unable to find the PAM library or the PAM header files

在此需要安装libpam0g-devlibtool

apt-get -y install libpam0g-dev libtool

2、设置Google Authenticator

在手机端搜索安装 Google Authenticator

google-authenticator
Do you want authentication tokens to be time-based (y/n) y

然后出现二维码,使用手机端Google Authenticator扫码,接下来服务器端继续设置

如果没有出现二维码,可以将提示的以 https://www.google.com/chart开头的连接复制到浏览器中,就可以看到二维码了

Do you want me to update your "/root/.google_authenticator" file (y/n) y

Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y

By default, tokens are good for 30 seconds and in order to compensate for
possible time-skew between the client and the server, we allow an extra
token before and after the current time. If you experience problems with poor
time synchronization, you can increase the window from its default
size of 1:30min to about 4min. Do you want to do so (y/n) y

If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting (y/n) y

修改配置文件

root@test:~# vim /etc/pam.d/sshd        #在第一行(即account    required     pam_nologin.so的下一行)增加以下代码
auth required pam_google_authenticator.so root@test:~# vim /etc/ssh/sshd_config
ChallengeResponseAuthentication yes #修改no为yes root@test:~# service ssh restart

3、手机安装Google身份验证器

见Centos安装步骤

4、登录

再次登录的话输入用户名后就跟着提示两部验证码,然后才输入用户密码,如下:

root@test1:~$ ssh root@120.17.144.148 -p65322
The authenticity of host '[120.17.144.148]:65322 ([120.17.144.148]:65322)' can't be established.
RSA key fingerprint is 6f:d1:19:41:af:25:8d:ee:b9:54:11:c0:cc:c0:6e:cd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[120.17.144.148]:65322' (RSA) to the list of known hosts.
Verification code:
Password:
Last login: Tue Jan 8 16:55:15 2019 from 121.157.126.116 Welcome to aliyun Elastic Compute Service! [root@test2 ~]#  

二、Centos安装

1、关闭SELINUX

[root@test ~]# vim /etc/selinux/config      #永久关闭。需要reboot重启后生效
SELINUX=enforcing 改为 SELINUX=disabled [root@test ~]# setenforce 0 #临时性关闭。不需要reboot重启

2、安装编辑工具包

[root@test ~]# yum install wget gcc make  pam-devel libpng-devel
[root@test ~]# yum -y install google-authenticator qrencode mercurial

3、配置ssh服务调用google authenticator PAM插件

[root@test ~]# vim /etc/pam.d/sshd       #在第一行(即auth       required pam_sepermit.so的下一行)增加以下代码
auth required pam_google_authenticator.so [root@test ~]# vim /etc/ssh/sshd_config
ChallengeResponseAuthentication yes #修改no为yes [root@test ~]# service sshd restart

4、使用google authenticator PAM插件为ssh登录账号生成动态验证码

注意:哪个账号需要动态验证码,请切换到该账号下操作。(可以在不同用户下执行这个命令以生成各自的二次验证码)

[root@test ~]# google-authenticator

https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@iZ23e3f2jdcZ%3Fsecret%3DD76NMSG26PAUJGAR

Your new secret key is: D76NMSG26PAUJGAR
Your verification code is 829676
Your emergency scratch codes are:
51322579
63010483
39007873
32360318
54935046

Do you want me to update your "~/.google_authenticator" file (y/n) y

Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y

By default, tokens are good for 30 seconds and in order to compensate for
possible time-skew between the client and the server, we allow an extra
token before and after the current time. If you experience problems with poor
time synchronization, you can increase the window from its default
size of 1:30min to about 4min. Do you want to do so (y/n) y

If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting (y/n) y

5、手机安装Google身份验证器

通过此工具扫描上一步生成的二维码图形,获取动态验证码。
在App Store里直接可以下载Authenticator

然后扫描上面在服务器上生成的二维码,每个用户都会有一个单独的二维码

接着在ssh的客户端里设置,如下,设置"Keyboard Interactive"方式登录

然后再次连接的时候,就会提示先输入二次身份验证码,再输入用户密码。

在Linux客户机上远程登录,效果一样:

root@test1:~$ ssh root@120.17.144.148 -p65322
The authenticity of host '[120.17.144.148]:65322 ([120.17.144.148]:65322)' can't be established.
RSA key fingerprint is 6f:d1:19:41:af:25:8d:ee:b9:54:11:c0:cc:c0:6e:cd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[120.17.144.148]:65322' (RSA) to the list of known hosts.
Verification code:
Password:
Last login: Tue Jan 8 16:55:15 2019 from 121.157.126.116 Welcome to aliyun Elastic Compute Service! [root@test2 ~]#

  

【Linux】使用Google Authenticator 实现ssh登录双因素认证的更多相关文章

  1. Linux 利用Google Authenticator实现ssh登录双因素认证

    1.介绍 双因素认证:双因素身份认证就是通过你所知道再加上你所能拥有的这二个要素组合到一起才能发挥作用的身份认证系统.双因素认证是一种采用时间同步技术的系统,采用了基于时间.事件和密钥三变量而产生的一 ...

  2. Linux下使用Google Authenticator配置SSH登录动态验证码

    1.一般ssh登录服务器,只需要输入账号和密码.2.本教程的目的:在账号和密码之间再增加一个验证码,只有输入正确的验证码之后,再输入密码才能登录.这样就增强了ssh登录的安全性.3.账号.验证码.密码 ...

  3. centos6实现基于google authenticator 的ssh登录二次验证

    1.手机安装google身份验证器,在浏览器搜索身份验证器安装即可. centos6安装所需要的软件--- google-authenticator 2.查看这个包生成的所有文件和命令 3.输入goo ...

  4. Linux 之 利用Google Authenticator实现用户双因素认证

    一.介绍:什么是双因素认证 双因素身份认证就是通过你所知道再加上你所能拥有的这二个要素组合到一起才能发挥作用的身份认证系统.双因素认证是一种采用时间同步技术的系统,采用了基于时间.事件和密钥三变量而产 ...

  5. 在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹)

    在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹) 1 列出所有需要copy的sh文件 -bash-4.1$ ll /mysqllog/osw/*.sh ...

  6. 业余草双因素认证(2FA)教程

    所谓认证(authentication)就是确认用户的身份,是网站登录必不可少的步骤.密码是最常见的认证方法,但是不安全,容易泄露和冒充.越来越多的地方,要求启用双因素认证(Two-factor au ...

  7. 双因素认证(2FA)教程

    所谓认证(authentication)就是确认用户的身份,是网站登录必不可少的步骤. 密码是最常见的认证方法,但是不安全,容易泄露和冒充. 越来越多的地方,要求启用 双因素认证(Two-factor ...

  8. 轻松搭建CAS 5.x系列(8)-在CAS Server增加双因素认证(DUO版)

    概述说明 为了让系统更加安全,很多登录会加入双因素认证.何为双因素,如果把登陆作为开一扇门的话,那就是在原来的锁上再加一把锁,第二锁用新的钥匙,这样安全系数就更加高了. CAS是通过账号名和密码来认证 ...

  9. Google Authenticator加强ssh安全

    一.安装依赖包 软件包可以在这个地址下载:https://pan.baidu.com/s/1r0CmwbtCfNiBqU9rh_TxtA yum -y install pam-devel tar jx ...

随机推荐

  1. Aladdin and the Flying Carpet(唯一分解定理)

    题目大意:给两个数a,b,求满足c*d==a且c>=b且d>=b的c,d二元组对数,(c,d)和(d,c)属于同一种情况: 题目分析:根据唯一分解定理,先将a唯一分解,则a的所有正约数的个 ...

  2. Booksort POJ - 3460 (IDA*)

    Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...

  3. 001. Ansible简介

    一 简介 Ansible是一款极其简单的自动化运维工具, 基于Python开发, 集合了众多运维工具(puppet, cfengine, chef, func, fabric)的优点. 实现了批量系统 ...

  4. PXE无人值守安装

    简介 1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作 ...

  5. NOIP2017 题解(给自己看的) --有坑要填

    目录 D1T1精妙证明: D1T3 D2T2 几道水题就不写了.... D1T1精妙证明: 把ax+by = z 的z按照模a剩余系分类 由于\((a,b)=1\)所以对于每个\(k\in[0, a) ...

  6. 关于@RestController注解(转发)

    知识点:@RestController注解相当于@ResponseBody + @Controller合在一起的作用. 1) 如果只是使用@RestController注解Controller,则Co ...

  7. Codeforces Round #541 (Div. 2) (A~F)

    目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路 ...

  8. MySQL 一对多查询

    group_concat简单来说,这个函数的作用就是连接多个字段 数据表首先我们先建立两个表 CREATE TABLE `student` ( `id` ) NOT NULL AUTO_INCREME ...

  9. 利用"SQL"语句自动生成序号的两种方式

    1.首先,我们来介绍第一种方式: ◆查询的SQL语句如下: select row_number() over (order by name) as rowid, sysobjects.[id] fro ...

  10. 潭州课堂25班:Ph201805201 第一课:环境搭建 (课堂笔记)

    安装PyCharm ,Oracle VM VirtualBox,Xshell ,实现仿真远程连接服务器,其中Oracle VM VirtualBox用于那家linux服务器,Xshell 用于连接服务 ...