安裝依賴環(huán)境
yum install libpng-devel libtool lrzsz pam-devel
安裝google-authenticator
git clone https://github.com/google/google-authenticator-libpam.git
cd google-authenticator-libpam
./bootstrap.sh
./configure
make && make install
cp /usr/local/lib/security/pam_google_authenticator.so /lib64/security/
修改PAM,SSH登錄時調(diào)用google-authenticator模塊
編輯/etc/pam.d/sshd文件,在第一行添加如下代碼
auth required pam_google_authenticator.so no_increment_hotp noskewadj
選項說明:
no_increment_hotp -- 確保計數(shù)器不會因失敗嘗試而遞增noskewadj -- 不自動調(diào)整時間偏差nullok -- 如果用戶尚未設(shè)置OTP,則允許用戶在沒有OTP的情況下登錄echo_verification_code -- 默認情況下,PAM模塊在用戶輸入驗證碼時不會回顯驗證碼。在某些情況下,管理員可能更喜歡不同的行為。將echo_verification_code選項傳遞給模塊以啟用回顯。
編輯 /etc/ssh/sshd_config 文件,修改如下內(nèi)容
ChallengeResponseAuthentication yes \啟用其它認證
UsePAM yes \啟用UsePAM模塊
PasswordAuthentication yes
sed -i “s/^#ChallengeResponseAuthentica.*/ChallengeResponseAuthentication yes/g” /etc/ssh/sshd_config
vim /etc/ssh/sshd_config
sed -i “s/^#UsePAM.*/UsePAM yes/g” /etc/ssh/sshd_config
sed -i “s/^#PasswordAuthentication.*/PasswordAuthentication yes/g” /etc/ssh/sshd_config
重啟 sshd
配置google authentication 生成基于計數(shù)的認證token(可以忽略時間錯誤)
/etc/init.d/sshd restart
cd /usr/local/bin/
./google-authenticator (運行后會回答問題,第一個N,其他的Y)
參數(shù):
-c, —counter-based 基于計數(shù)器生成動態(tài)碼(可以忽略時間差錯誤)
-t, —time-based基于時間生成動態(tài)碼
-R, —rate-time=M設(shè)置登錄頻率限制的時間間隔
-w, —window-size=W 設(shè)置時間窗的大小,主要在移動設(shè)備的時間不是準(zhǔn)確同步的情況下比較有用
交互式命令 google-authenticator 生成token,可以一直按y,如果特殊需要可以自行修改
google-authenticator
Do you want authentication tokens to be time-based (y/n): n
是否基于時間生成動態(tài)碼,n是基于計數(shù)器
此時會生成一個很大的二維碼,和手機客戶端登錄的密鑰和5個應(yīng)急碼,手機客戶端掃描二維碼就可以直接自動添加當(dāng)前賬戶。
問題一:用xshell登錄,無法登錄。查看/var/log/secure 看到日志第一條是:invalid authentication code for root 。觀察細節(jié)會發(fā)現(xiàn),剛打開連接服務(wù)器的xshell窗口,日志就已經(jīng)報錯 invalid authentication code for root,然而此時什么都還沒有輸入。似乎可以通過調(diào)整配置解決這個問題。我的解決辦法是停掉 ssh 的密碼驗證。
PasswordAuthentication no