系統: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