本文安装环境: CentOS 7 64位

安装所需的支持环境

1
2
3
$ yum groupinstall "Development Tools" -y
$ yum install pam-devel -y
$ yum install chrony -y //这步Centos7是已经默认安装的

【重要】开启时间同步

由于GOOGLE的两步验证服务器端每次密钥生成依赖时间,所以务必执行本步骤,以确保时间是正确时间

编辑时间同步的配置文件:

1
$ vim /etc/chrony.conf

在头部时间服务器列表中增加:server 2.cn.pool.ntp.org iburst

1
2
$ systemctl restart chronyd //重启时间服务
$ chronyc sources //手动同步一下时间

编译安装谷歌两步验证

先从远端仓库拉回来代码:

1
2
$ git clone https://github.com/google/google-authenticator-libpam.git
$ cd google-authenticator-libpam/

编译、安装:

1
2
3
$ ./bootstrap.sh
$ ./configure
$ make && make install

重要步骤

将谷歌两步验证加入到ssh服务中:

1
$ vim /etc/pam.d/sshd

在文件中头部增加:

1
auth required pam_google_authenticator.so

配置ssh服务,开启两步验证:

1
$ vim /etc/ssh/sshd_config

找到ChallengeResponseAuthentication nono修改为yes

重要

按照默认安装,会报错,请将这个文件cp或者ln到目录,推荐使用ln方法。

1
$ cp pam_google_authenticator.so /usr/lib64/security/

重启ssh

1
$ systemctl restart sshd

开始配置谷歌两步验证

1
$ google-authenticator

这里剩下一路回车即可,系统会生成密钥,可以用谷歌验证器扫描增加。

这里要注意的是在二维码生成后会现实几串应急密码,可以记录下来,在验证器失败的时候可以用临时应急密码登陆,但每使用一个失效一个。