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

  • 2019 年 10 月 4 日
  • 筆記

原文地址 https://www.cnblogs.com/tiannan/p/6238832.html

1.介绍

双因素认证:双因素身份认证就是通过你所知道再加上你所能拥有的这二个要素组合到一起才能发挥作用的身份认证系统。双因素认证是一种采用时间同步技术的系统,采用了基于时间、事件和密钥三变量而产生的一次性密码来代替传统的静态密码。每个动态密码卡都有一个唯一的密钥,该密钥同时存放在服务器端,每次认证时动态密码卡与服务器分别根据同样的密钥,同样的随机参数(时间、事件)和同样的算法计算了认证的动态密码,从而确保密码的一致性,从而实现了用户的认证。因每次认证时的随机参数不同,所以每次产生的动态密码也不同。由于每次计算时参数的随机性保证了每次密码的不可预测性,从而在最基本的密码认证这一环节保证了系统的安全性。

说白了,就像我们几年前去银行办卡送的口令牌,以及网易游戏中的将军令,在你使用网银或登陆游戏时会再让你输入动态口令的。

2.目的

实现登录Linux 服务器时,除了输入用户名密码外,需要输入一次性的动态口令才能验证成功。

3.安装过程

3.1安装chrony

生成动态口令的其中一个因素是时间,需要保持终端设备和服务器的系统时间一致,才能生成同一的动态口令

简单说下chrony:chrony 是网络时间协议的(NTP)的另一种实现,与网络时间协议后台程序(ntpd)不同,它可以更快地更准确地同步系统始终。

国内比较好用的ntp服务器:官网

注,同步时间时请准确设置系统时区

[root@localhost ~]# yum install -y chrony  [root@localhost ~]# vim /etc/chrony.conf    server 0.cn.pool.ntp.org iburst    [root@localhost ~]# systemctl restart chronyd  [root@localhost ~]# chronyc sources210 Number of sources = 4MS Name/IP address Stratum Poll Reach LastRx Last sample===============================================================================  ^? 223.112.179.133 0 6 0 10y +0ns[ +0ns] +/- 0ns^* dns1.synet.edu.cn 2 6 33 0 +113us[ +284us] +/- 28ms^? 2001:da8:202:10::61 0 6 0 10y +0ns[ +0ns] +/- 0ns^? 42.96.167.209 2 6 10 8 +2011us[ +949us] +/- 127ms    [root@localhost ~]# date2016年 12月 31日 星期六 09:30:24 CST

3.2安装依赖组件

[root@localhost ~]# yum install -y git automake libtool pam-devel

3.3下载谷歌认证模块

[root@localhost ~]# git clone https:~-xr-x  root root  12月  : google-authenticator-~]# cd google-authenticator-libpam/-authenticator-libpam]# ./bootstrap.-authenticator-libpam]# ./-authenticator-libpam]#  &&  -authenticator-libpam]# google--authenticator-libpam]# cd ~~]# vim /etc/pam.d/~]# vim /etc//~~]# google--based (y/
   ? (y/? This restricts you to one --the-middle attacks (y/ possible -. This allows  skew of up to  permitted codes (the  next codes). This will permit  a  skew of up to  so? (y/ attempts, you can enable rate-limiting  than  -limiting? (y/n) y

3.4 手机安装身份验证器

app下载地址:http://www.coolapk.com/apk/com.google.android.apps.authenticator2

4.登录验证

注意,第一次登录可能会出现登录失败的情况,查看日志信息显示错误如下:

[root@localhost ~]# tail -n10 /var/log/secure    ...  Dec 31 09:42:46 localhost sshd[2393]: PAM unable to dlopen(/usr/lib64/security/pam_google_authenticator.so): /usr/lib64/security/pam_google_authenticator.so: cannot open shared object file: No such file or directory  Dec 31 09:42:46 localhost sshd[2393]: PAM adding faulty module: /usr/lib64/security/pam_google_authenticator.so  ...    [root@localhost ~]# ln -sv /usr/local/lib/security/pam_google_authenticator.so /usr/lib64/security/pam_google_authenticator.so"/usr/lib64/security/pam_google_authenticator.so" -> "/usr/local/lib/security/pam_google_authenticator.so"

开始再次登录

iterm2下面,登录。先要输入ssh密码,然后还需要输入一个生成器的随机密码。2个密码都正确才能登录到服务器。