公司線上的使用機器不能讓用戶隨意的登陸,所以就不能讓開發隨意的登陸到生產的機器的。于是就打算使用google-auth的驗證方式呢。
如果google-auth的方式。
搭建google-authenticator:
搭建這個很簡單,如下:
git clone ?下載最新的google auth 最新版。
cd google-authenticator-libpam/
./bootstrap.sh
./configure && make && make install
ln -s /usr/local/lib/security/pam_google_authenticator.so /lib64/security/pam_google_authenticator.so
修改/etc/pam.d/sshd,
#最上方加一行 “auth required pam_google_authenticator.so”
? ?#這個配置可以更復雜一些,加上一些參數,詳見 libpam/README
? #注:如果遇到仍然需要輸入密碼的情況,改成 “auth sufficient pam_google_authenticator.so” 試試。
修改/etc/ssh/sshd_config
將 ChallengeResponseAuthentication 選項的 no 改成 yes
將 UsePAM yes
service sshd restart
? 生成密鑰
$ google-authenticator? ? #注:運行這個命令的是需要登錄的用戶,不是root用戶
Do you want authentication tokens to be time-based (y/n) y? ?(確認:基于時間的認證token)
【這里會顯示生成二維碼的地址、二維碼、密鑰明文、應急碼】
Do you want me to update your “/var/www/.google_authenticator” file (y/n) y (確認:更新配置文件)
……
size of 1:30min to about 4min. Do you want to do so (y/n) n (token有效期是1.5min,選y就是4min)
……
Do you want to enable rate-limiting (y/n) y (30s內只允許嘗試三次)
在app里掃二維碼,或者手動輸入密鑰,即可看到token每隔30s更新一次了
嘗試登錄
$ ssh localhost
verification code: 【輸入驗證碼】
password: 【輸入密碼】
?
?
補:
但當時只是簡單加上了Google Authenticator,實際使用中既要輸入驗證又要輸入密碼,太繁瑣了,所以在搭建我司跳板機的時候,選擇了用 publickey + authenticator 的方案,只需要輸入一次驗證碼即可。但是這里要求很多。如openssh的版本大于6.2,如果不是的話,就無法使用AuthenticationMethods,最好的方式是使用centos7的版本(已驗證過可以使用)centos6.5測試無法使用(應該是我技術不行)。
具體的配置方案變化不大,主要是用上了 SSH 6.2+ 新增的 AuthenticationMethods 參數,可以指定一系列驗證方法,具體配置如下:
AuthenticationMethods publickey,keyboard-interactive
#對于指定的IP,只需要publickey驗證
Match Address 10.0.0.4
? ? AuthenticationMethods publickey
#也可以指定用戶只需要publickey驗證
#Match User XXX
? ? #AuthenticationMethods publickey
順便吐槽一下,Linux這套東西折騰起來真是要命,今天配置跳板機備份機的時候,完全相同的配置,復制一份就是不對,雖然配置里只指定了publickey,keyboard-interactive,但是每次輸完驗證碼以后還是要求輸入密碼才行,折騰了幾個小時才發現,不知道從什么時候開始,”auth required pam_google_authenticator.so” 已經不合適了,需要改成 “auth sufficient pam_google_authenticator.so”,這樣才會在輸入驗證碼以后就結束認證過程(sufficient的實現里加了一個break?什么鬼。)(感謝 @ )
最后,提醒一下使用SecureCRT的同學,你需要在Session Options -> Connection -> SSH2,將Authentication中只選用 “Keyboard Interactive” ,否則沒法正常登錄。
錯誤:configure: error: Unable to find the PAM library or the PAM header files
方法:yum install -y pam-devel
引用:
?