公司線上的使用機器不能讓用戶隨意的登錄,因此就不能讓開發隨意的登錄到生產的機器的。因而就打算使用google-auth的驗證方式呢。php
若是google-auth的方式。git
搭建google-authenticator:github
搭建這個很簡單,以下:bootstrap
git clone https://github.com/google/google-authenticator-libpam 下載最新的google auth 最新版。centos
cd google-authenticator-libpam/微信
./bootstrap.shapp
./configure && make && make installssh
ln -s /usr/local/lib/security/pam_google_authenticator.so /lib64/security/pam_google_authenticator.so測試
修改/etc/pam.d/sshd,ui
#最上方加一行 "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 參數,能夠指定一系列驗證方法,具體配置以下:
順便吐槽一下,Linux這套東西折騰起來真是要命,今天配置跳板機備份機的時候,徹底相同的配置,複製一份就是不對,雖然配置裏只指定了publickey,keyboard-interactive,可是每次輸完驗證碼之後仍是要求輸入密碼才行,折騰了幾個小時才發現,不知道從何時開始,"auth required pam_google_authenticator.so" 已經不合適了,須要改爲 "auth sufficient pam_google_authenticator.so",這樣纔會在輸入驗證碼之後就結束認證過程(sufficient的實現里加了一個break?什麼鬼。)(感謝 @ https://serverfault.com/a/740881/343388)
最後,提醒一下使用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
引用:https://www.felix021.com/blog/read.php?2133
https://www.felix021.com/blog/read.php?2172