ssh密码登录+google认证实现双因子登录认证

1、前言

通常来说使用ssh远程登录到服务器,只需要输入账号和密码即可,但是这种方式显然不是很安全。而Linux系统实现SSH安全登录服务器的方法有两种,一种是结合证书做SSH认证,第二种就是结合google的Authenticator 做双因子认证。下面是搭建ssh+google Authenticator 双因子认证的相关流程和步骤。

2、安装相关工具包

首先安装相关的依赖包:


[root@localhost ~]# yum install wget gcc make pam-devel libpng-devel -y

接着在下述链接中分别下载相应的工具包:qrencode-4.0.2.tar、libpam-google-authenticator-1.0-source.tar和rpmforge-release-0.5.3-1.el7.rf.x86_64 (下述链接可能需要FQ)

https://fukuchi.org/works/qrencode/

https://github.com/yangcvo/Google-Authenticator

https://www.rpmfind.net/linux/rpm2html/search.php?query=rpmforge-release

下载放置在相关的目录下并安装:


[root@localhost ~]# cd /software/

#安装repoforge第三方yum源

[root@localhost software]# rpm -ivh rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm 

警告:rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 6b8d79e6: NOKEY

准备中... ################################# [100%]

正在升级/安装...

   1:rpmforge-release-0.5.3-1.el7.rf ################################# [100%]

[root@localhost software]# yum install mercurial -y        #安装轻量级的资源管理控制系统进行版本管理

#安装google authenticator 插件

[root@localhost software]# tar -jxvf libpam-google-authenticator-1.0-source.tar.bz2

[root@localhost software]# cd libpam-google-authenticator-1.0

[root@localhost libpam-google-authenticator-1.0]# make && make install

#安装QrenCode工具,用于在Linux命令行中生成二维码

[root@localhost software]# tar -xvf qrencode-4.0.2.tar.gz

[root@localhost software]# cd qrencode-4.0.2

[root@localhost qrencode-4.0.2]# ./configure --prefix=/usr/

[root@localhost qrencode-4.0.2]# make && make install

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


[root@localhost ~]# vim /etc/pam.d/sshd

auth [success=1 default=ignore] pam_succeed_if.so user != charles        #当登录用户不是charles时,忽略下一个语句,防止因为启用了google认证导致其他用户无法登录

auth required pam_google_authenticator.so        #在第一行(即auth required pam_sepermit.so的下一行)添加该语句

[root@localhost ~]# vim /etc/ssh/sshd_config

ChallengeResponseAuthentication yes        #找到相应的参数,修改其选项为yes

#重启sshd服务

[root@localhost ~]# systemctl restart sshd

4、利用google-authenticator为ssh登录账号生成相应的动态验证码


[root@localhost ~]# su - charlie        #切换到需要生成动态验证码的账号下操作

[charlie@localhost ~]$ google-authenticator

Do you want authentication tokens to be time-based (y/n) y        #设置认证tokens为基于时间

https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/[email protected]%3Fsecret%3D46B225ISWDXY7ZZ3        #此链接为生成的二维码,需要在FQ条件下打开

ssh密码登录+google认证实现双因子登录认证_第1张图片
关联二维码

Your new secret key is: 46B225ISWDXY7ZZ3    #如果在手机的谷歌身份验证器上不想通过"扫描条形码"的方式添加,就输入这个key,通过"手动输入验证码的方式"。账号就是服务器主机名,如:[email protected]

Your verification code is 634528

Your emergency scratch codes are:        #下面会生成5个紧急验证码(当无法获取动态验证码或验证码不能使用使用可以使用这5个),需要注意的是:这5个验证码用一个就会少一个!请保存好!

  66417550

  97607625

  12501023

  35902809

  12853866

Do you want me to update your "/home/charlie/.google_authenticator" file (y/n) y        #提示是否要更新验证文件,选择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) n        #默认动态验证码在30秒内有效,由于客户端和服务器可能会存在时间差,可将时间增加到最长4分钟,是否要这么做:这里选择是n,继续默认30秒

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        #是否限制尝试次数,每30秒只能尝试最多3次,这里选择y进行限制

5、在手机安装google的authenticator获取动态验证码

在手机的app store或者应用市场查找安装Authenticator。

APP图标
ssh密码登录+google认证实现双因子登录认证_第2张图片
APP界面

安装完成后点击开始设置,扫描此前显示的二维码,获取相应的验证码

ssh密码登录+google认证实现双因子登录认证_第3张图片
认证信息

接着在ssh的客户端中,设置“keyboard Interactive”方式登录,如:

ssh密码登录+google认证实现双因子登录认证_第4张图片
登录设置

再次登录连接时,会先提示输入验证码后,再输入密码:


ssh密码登录+google认证实现双因子登录认证_第5张图片
提示输入验证码
ssh密码登录+google认证实现双因子登录认证_第6张图片
提示输入密码

确认输入无误后,就能使用指定的用户登录到系统中。

在Linux 客户机上远程登录的流程和效果也是一样的,如:


[root@localhost ~]# ssh [email protected]

The authenticity of host '192.168.0.91 (192.168.0.91)' can't be established.

ECDSA key fingerprint is f8:12:d7:52:64:22:6a:14:29:d2:82:4d:e5:8b:20:9a.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.0.91' (ECDSA) to the list of known hosts.

Verification code: 

Password: 

Last login: Wed Jul 11 22:45:58 2018 from 192.168.0.38

[charlie@localhost ~]$ 

你可能感兴趣的:(ssh密码登录+google认证实现双因子登录认证)