1. 系统基础环境
说明:使用云服务器安装与本地系统两种方式安装google-authenticator
基于云服务器centos7
[root@linux-tes ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.6.1810 (Core)
Release: 7.6.1810
Codename: Core
centos8 官网镜像版本 CentOS-8.1.1911-x86_64-dvd.iso,本地Windows系统 Virtualbox安测试
[root@linux-tes ~]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
2. 安装 google-authenticator
2.1. 云服务器安装google-authenticator
直接使用命令
yum install google-authenticator #云服务器安装只有一个命令很简单
2.2. 本地google-authenticator源码安装
2.2.1本地安装直接步骤:
说明: 使用的直接安装,采用报什么错误,解决在继续安装,不想看到以下错误,请直接使用最后面的 本地安装全步骤 ,全步骤不会有错误提示。
下载地址
https://github.com/google/google-authenticator-libpam
下载zip包,并解压目录“google-authenticator-libpam-master”传到centos系统中
如果安装git,可以直接clone到centos系统中
git clone https://github.com/google/google-authenticator-libpam.git
进入google-authenticator目录,源码安装
cd google-authenticator-libpam-master/ #进入目录
chmod +x bootstrap.sh #设置可执行权限
./bootstrap.sh
解决bootstrap.sh错误,如上图
yum install automake #上面图的错误需要安装automake
./bootstrap.sh
解决bootstrap.sh错误,如上图
yum install libtool #上面图的错误需要安装libtool 这个工具会自动安装gcc
./bootstrap.sh
#安装libtool自带其他依赖
#(1/9): isl-0.16.1-6.el8.x86_64.rpm 155 kB/s | 841 kB 00:05
#(2/9): libmpc-1.0.2-9.el8.x86_64.rpm 147 kB/s | 59 kB 00:00
#(3/9): libtool-2.4.6-25.el8.x86_64.rpm 240 kB/s | 709 kB 00:02
#(4/9): glibc-devel-2.28-72.el8.x86_64.rpm 191 kB/s | 1.0 MB 00:05
#(5/9): glibc-headers-2.28-72.el8.x86_64.rpm 263 kB/s | 469 kB 00:01
#(6/9): kernel-headers-4.18.0-147.5.1.el8_1.x86_64.rpm 332 kB/s | 2.7 MB 00:08
#(7/9): libxcrypt-devel-4.1.1-4.el8.x86_64.rpm 77 kB/s | 25 kB 00:00
#(8/9): cpp-8.3.1-4.5.el8.x86_64.rpm 351 kB/s | 10 MB 00:30
#(9/9): gcc-8.3.1-4.5.el8.x86_64.rpm 628 kB/s | 23 MB 00:38
bootstrap.sh脚本执行完成,执行configure命令
./configure #执行会报下面错误 默认安装到当前目录
#./configure --prefix=/opt 指定安装目录
执行configure报错,如上图
yum install pam-devel #上面图的错误需要安装pam-deve
./configure
安装make且完成configure
yum install make #上面图错误与make没有安装相关,
#如果使用提示中的参数 ./configure --disable-dependency-tracking,虽然成功,后面执行make也提示 (-bash: make: command not found)
./configure
#以下是安装成功提示
#google-authenticator version 1.08
# Prefix.........: /usr/local
# Debug Build....:
# C Compiler.....: gcc -g -O2 -Wall
# Linker.........: /usr/bin/ld -m elf_x86_64 -ldl
编译安装google-authenticator
make
make install
2.2.2本地安装全步骤
yum install automake libtool pam-devel qrencode make git
# automake libtool pam-devel 是google-authenticator需要的。libtool中依赖gcc
# qrencode 可以不用安装,如果没有安装,在运行goole-authenticator时候不会显示二维码图
#本地系统没有make命令所有加了make。git使用用于下载google-authenticator源码
git clone https://github.com/google/google-authenticator-libpam.git #下载google-authenticator源码
cd google-authenticator-libpam/ #进入目录
./bootstrap.sh #执行脚本
./configure #执行configure
make #执行
make install #执行安装
3. 运行google-authenticator
google-authenticator # 回车进入相关提示
出现了“Failed to use libqrencode to show QR code visually for scanning”,
该错误是由于没有安装qrencode,如果安装会显示二维图图片,不影响使用
说明:二维码图片只是方便手机app直接扫描添加添加令牌,没有二维码,只能手动输入密钥信息,如Your new secret key is: GAKNYQ6EUL3Y4L46ZLUQDZTGS4,这个GAKNY开头密钥信息输入到手机令牌中。
没有安装qrencode的全部步骤,如下图:
4.修改centos系统配置
vi /etc/ssh/sshd_config #编辑文件sshd_config
ChallengeResponseAuthentication yes #修改成yes
vi /etc/pam.d/sshd #编辑文件sshd
#增加一行
auth required pam_google_authenticator.so #增加
#重启 到此安装结束
service sshd restart
4.1.注意事项:
/etc/pam.d/sshd增加的说明
#官网建议是
auth required pam_google_authenticator.so nullok #增加一行
#翻译不标准,就采用以下官网原话
#During the initial roll-out process, you might find that not all users have created a secret key yet. If you would still like them to be able to log in, you can pass the "nullok" option on the module's command line:
#其他说明参考官网,链接在最后面
注意/etc/pam.d/sshd配置文件增加的顺序
登陆的时候先输入密码,然后再输入验证码。
auth substack password-auth #文件中有的
auth required pam_google_authenticator.so #增加一行
登陆的时候先输入验证码,然后再输入密码
auth required pam_google_authenticator.so #增加一行
auth substack password-auth #文件中有的
严重注意事项:千万不要断开ssh链接,在去尝试是否可以使用手机令牌登陆。如果安装成功,但不能使用,导致无法登陆。这就麻烦了,建议是另开一个链接窗口测试手机令牌是否可以正常使用。本人在云服务器上安装使用没有任何错误,但本地安装一直无法登录,查看日志/var/log/secure才发现问题,问题解决在最后面。
4.手机app添加安全令牌
直接在app应用商城里搜索下载Google Authenticator,大部分都支持
- Google Authenticator
- 阿里云app(虚拟MFA)
- Microsoft Authenticator
- 等等,只要是手机令牌,基本上可以扫码或人工输入添加令牌
建议提前下载安装app,在运行google authenticator可以验证,在以下步骤中
Enter code from app (-1 to skip): #这个提示在上面操作步骤是输入-1跳过,手机app可以添加之后,在这里输入6位验证码使用。
#错误:将一直让你输入。
#成功:将提示,Code confirmed 并显示Your emergency scratch codes are:42176283 ...
4.1.部分app使用截图
Microsoft Authenticator:
Google Authenticator:
阿里云app(虚拟MFA):
5. 使用测试
使用的xsheel链接ssh测试,选择keyboard Interactvie(I) 使用键盘输入用户身份验证
自动会弹出以下两个框,这个两个框的顺序与/etc/pam.d/sshd这个文件配置顺序有关。
6. 可能出现以下错误
云服务安装没有该错误,但本地安装有此错误。
登陆的时候,监控登陆日志 tail -f /var/log/secure 可以看到以下错误
Mar 4 08:14:57 localhost sshd[32346]: 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
Mar 4 08:14:57 localhost sshd[32346]: PAM adding faulty module: /usr/lib64/security/pam_google_authenticator.so
解决办法
find / -name "pma_google*.so" #查找该文件在哪里
ln -s /usr/local/lib/security/pam_google_authenticator.so /usr/lib64/security/pam_google_authenticator.so
#建立软连接,以路径的形式存在。类似于Windows操作系统中的快捷方式
#ln语法: ln [参数][源文件或目录][目标文件或目录]
官网地址
https://github.com/google/google-authenticator-libpam