徐亮偉, 江湖人稱標桿徐。多年互聯網運維工作經驗,曾負責過大規模集群架構自動化運維管理工作。擅長Web集群架構與自動化運維,曾負責國內某大型電商運維工作。
個人博客"徐亮偉架構師之路"累計受益數萬人。
筆者Q:552408925
架構師群:471443208
通常我們直接通過ssh輸入密碼連接服務器,但這樣很容易出現暴力破解情況,所以我們可以結合google的動態認證+ssh密碼,這樣能夠大大的提升登陸的安全。
簡單來說,就是當用戶通過ssh登陸系統時,先輸入google的隨機驗證碼,然后在輸入服務器的ssh密碼
1.安裝依賴包,環境屬於centos7,centos6請自行查閱網上資料
[root@bgx ~]# yum -y install wget gcc make pam-devel libpng-devel pam-devel
2.安裝Google Authenticator PAM插件安裝
[root@bgx ~]# wget https://github.com/google/google-authenticator-libpam/archive/1.04.tar.gz
[root@bgx ~]# tar xf 1.04.tar.gz
[root@bgx ~]# cd google-authenticator-libpam-1.04/
[root@bgx google-authenticator-libpam-1.04]# ./bootstrap.sh
[root@bgx google-authenticator-libpam-1.04]# ./configure
[root@bgx google-authenticator-libpam-1.04]# make && make install
[root@bgx google-authenticator-libpam-1.04]# cp /usr/local/lib/security/pam_google_authenticator.so /lib64/security/
3.初始配置 Google Authenticator
[root@bgx google-authenticator-libpam-1.04]# google-authenticator
是否基於時間的認證,為了防止不同跨時區的問題,這里選擇n
Do you want authentication tokens to be time-based (y/n) n
然后會跳出一個google的二維碼
紅色框框是: 生成的密鑰
綠色框框是: 生成的5個一次性緊急驗證碼,用於緊急情況下,使用過一次后該驗證碼即失效了。
是否更新用戶的 Google Authenticator 配置文件,選擇 y 才能使上面操作對當前 root 用戶生效,其實就是在對應用戶的 Home 目錄下生成了一個 .google_authenticator 文件,如果你想停用這個用戶的 Google Authenticator 驗證,只需要刪除這個用戶 Home 目錄下的 .google_authenticator 文件就可以了。
Do you want me to update your "/root/.google_authenticator" file? (y/n) y
每次生成的認證碼是否同時只允許一個人使用?這里選擇 y。
Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y
每次生成的令牌30s生成一次,最高允許存在誤差4分鍾。
By default, a new token is generated every 30 seconds by the mobile app.
In order to compensate for possible time-skew between the client and the server,
we allow an extra token before and after the current time. This allows for a
time skew of up to 30 seconds between authentication server and client. If you
experience problems with poor time synchronization, you can increase the window
from its default size of 3 permitted codes (one previous code, the current
code, the next code) to 17 permitted codes (the 8 previous codes, the current
code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
between client and server.
Do you want to do so? (y/n) y
4.SSH調用及客戶端配置,添加pam認證,在第一行添加
[root@bgx ~]# vim /etc/pam.d/sshd #添加如下行
auth required pam_google_authenticator.so
5.修改sshd配置,關聯google認證
[root@bgx ~]# vim /etc/ssh/sshd_config
ChallengeResponseAuthentication yes #修改為yes
[root@bgx ~]# systemctl restart sshd #重啟sshd服務
6.客戶端通過ssh連接服務器測試
需要輸入動態密碼,動態密碼通過手機獲取如下圖所示
7.查看服務端的安全日志文件,可以看到是先進程google動態密碼認證
8.注意事項:
1.用password + google authenticator,如果使用公鑰登錄的話,會跳過google authenticator驗證直接登錄服務器的。
2.如果是內網測試使用,建議安裝google authenticator 瀏覽器插件實踐。如果是公網服務器建議安裝手機版的Authenticator
9.安裝Google authenticator
Andorid版: “自行百度”
iOS版: 下載 “Authenticator”
chrome瀏覽器有google authenticator的插件