Google Authenticator实现Linux用户登录双认证


系统:CentOS 7.5 X64

#安装开发者工具

yum groupinstall "Development Tools" -y

#安装pam 开发包

yum install pam-devel -y

#安装chrony软件,chrony 是网络时间协议的(NTP)的另一种实现,因为动态口令再验证时用到了时间,所以要保持时间上的一致性

yum install chrony -y

vi /etc/chrony.conf

server 2.cn.pool.ntp.org iburst

systemctl restart chronyd

chronyc sources

#如果时区不对的话,可以拷贝你当前地区所在地的时区到系统运行的时区

#cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

###安装google-authenticator

cd /opt

git clone https://github.com/google/google-authenticator-libpam.git

cd google-authenticator-libpam/

./bootstrap.sh

./configure

make && make install

ln -s /usr/local/lib/security/pam_google_authenticator.so /usr/lib64/security/pam_google_authenticator.so

#配置系统PAM模块,修改sshd支持谷歌的认证,在sshd文件的第一行

vi /etc/pam.d/sshd

auth      required pam_google_authenticator.so

vi /etc/ssh/sshd_config

...

ChallengeResponseAuthentication yes

#安装二维码生成工具,这步也可以省略,如果不装的话,因为下一步生成的二维码就会成一个链接,

#到时将链接复制到你的浏览器中,也是可以出现二维码的,到时利用智能手机打开google author 进行扫描。

cd /opt

yum -y install libpng libpng-devel

wget -c https://fukuchi.org/works/qrencode/qrencode-4.0.2.tar.bz2

tar jxvf qrencode-4.0.2.tar.bz2

cd qrencode-4.0.2

./configure

make && make install

#运行google-authenticator命令,它将会在当前登陆用户的家目录中生成一个新的密钥

cd ~

google-authenticator

#Your emergency scratch codes are:

#  98627355

#  45083255

#  48589468

#  91373042

#  22191496

#上述共需回答5个y

#第1个:问你是否想做一个基于时间的令×××y

#第2个:是否更新你的google认证文件,由于第一次设置,所以一定选y 

#第3个:是否禁止口令多用,这里选择y,禁止它,以防止中间人欺骗。y

#第4个:默认情况,1个口令的有效期是30s,这里是为了防止主机时间和口令客户端时间不一致,设置的误差,可以选择y,也可选n,看要求严谨程度y

#第5个:是否打开尝试次数限制,默认情况,30s内不得超过3次登陆测试,防止别人暴力破解。y

#并且上面这些设置将被存储在用户的/.google_authenticator文件中,emergency scratch codes 中的5个代码是紧急代码,务必牢记,

#这是在你的动态口令无法使用的情况下使用的,记住,用一个失效一个。后期可以登陆上去后,重新生成!

#此时打开手机上的Google Authenticator应用扫描二维码

#最后重启sshd服务

systemctl restart sshd

###--- secureCRT设置keyboard interactive需要放在第一位 ---###

### 解决内网主机跳过二次认证

#编辑pam.d下的sshd 文件,在第一行增加内容,主要是指定允许的主机信息文件

more -2 /etc/pam.d/sshd

auth [success=1 default=ignore] pam_access.so accessfile=/etc/security/access-localhost.conf

auth      required pam_google_authenticator.so no_increment_hotp

#然后在/etc/security/目录下创建access-localhost.conf文件

cat /etc/security/access-localhost.conf

# skipped local network for google auth...

+ : ALL : 192.168.11.0/24

+ : ALL : LOCAL

- : ALL : ALL

#最后重启sshd服务

systemctl restart sshd

linuxboy的RSS地址:https://www.linuxboy.net/rssFeed.aspx

本文永久更新链接地址:https://www.linuxboy.net/Linux/2018-12/155775.htm

相关内容