系統(tǒng):CentOS 7.5 X64
#安裝開發(fā)者工具
yum groupinstall “Development Tools” -y
#安裝pam 開發(fā)包
yum install pam-devel -y
#安裝chrony軟件,chrony 是網(wǎng)絡(luò)時間協(xié)議的(NTP)的另一種實現(xiàn),因為動態(tài)口令再驗證時用到了時間,所以要保持時間上的一致性
yum install chrony -y
vi /etc/chrony.conf
…
server 2.cn.pool.ntp.org iburst
systemctl restart chronyd
chronyc sources
#如果時區(qū)不對的話,可以拷貝你當(dāng)前地區(qū)所在地的時區(qū)到系統(tǒng)運行的時區(qū)
#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
#配置系統(tǒng)PAM模塊,修改sshd支持谷歌的認(rèn)證,在sshd文件的第一行
vi /etc/pam.d/sshd
auth required pam_google_authenticator.so
vi /etc/ssh/sshd_config
…
ChallengeResponseAuthentication yes
#安裝二維碼生成工具,這步也可以省略,如果不裝的話,因為下一步生成的二維碼就會成一個鏈接,
#到時將鏈接復(fù)制到你的瀏覽器中,也是可以出現(xiàn)二維碼的,到時利用智能手機打開google author 進(jìn)行掃描。
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
#運行g(shù)oogle-authenticator命令,它將會在當(dāng)前登陸用戶的家目錄中生成一個新的密鑰
cd ~
google-authenticator
#Your emergency scratch codes are:
# 98627355
# 45083255
# 48589468
# 91373042
# 22191496
#上述共需回答5個y
#第1個:問你是否想做一個基于時間的令×××y
#第2個:是否更新你的google認(rèn)證文件,由于第一次設(shè)置,所以一定選y
#第3個:是否禁止口令多用,這里選擇y,禁止它,以防止中間人欺騙。y
#第4個:默認(rèn)情況,1個口令的有效期是30s,這里是為了防止主機時間和口令客戶端時間不一致,設(shè)置的誤差,可以選擇y,也可選n,看要求嚴(yán)謹(jǐn)程度y
#第5個:是否打開嘗試次數(shù)限制,默認(rèn)情況,30s內(nèi)不得超過3次登陸測試,防止別人暴力破解。y
#并且上面這些設(shè)置將被存儲在用戶的/.google_authenticator文件中,emergency scratch codes 中的5個代碼是緊急代碼,務(wù)必牢記,
#這是在你的動態(tài)口令無法使用的情況下使用的,記住,用一個失效一個。后期可以登陸上去后,重新生成!
#此時打開手機上的Google Authenticator應(yīng)用掃描二維碼
#最后重啟sshd服務(wù)
systemctl restart sshd
###— secureCRT設(shè)置keyboard interactive需要放在第一位 —###
### 解決內(nèi)網(wǎng)主機跳過二次認(rèn)證
#編輯pam.d下的sshd 文件,在第一行增加內(nèi)容,主要是指定允許的主機信息文件
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/目錄下創(chuàng)建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服務(wù)
systemctl restart sshd