天天看點

ssh密碼登入+ Google Authenticator 實作雙向認證

目錄

徐亮偉, 江湖人稱标杆徐。多年網際網路運維工作經驗,曾負責過大規模叢集架構自動化運維管理工作。擅長Web叢集架構與自動化運維,曾負責國内某大型電商運維工作。

個人部落格"徐亮偉架構師之路"累計受益數萬人。

筆者Q:552408925

架構師群:471443208

通常我們直接通過ssh輸入密碼連接配接伺服器,但這樣很容易出現暴力破解情況,是以我們可以結合google的動态認證+ssh密碼,這樣能夠大大的提升登陸的安全。

簡單來說,就是當使用者通過ssh登陸系統時,先輸入google的随機驗證碼,然後在輸入伺服器的ssh密碼

1.安裝依賴包,環境屬于centos7,centos6請自行查閱網上資料

[[email protected] ~]# yum -y install wget gcc make pam-devel libpng-devel pam-devel
           

2.安裝Google Authenticator PAM插件安裝

[[email protected] ~]# wget https://github.com/google/google-authenticator-libpam/archive/1.04.tar.gz
[[email protected] ~]# tar xf 1.04.tar.gz
[[email protected] ~]# cd google-authenticator-libpam-1.04/
[[email protected] google-authenticator-libpam-1.04]# ./bootstrap.sh
[[email protected] google-authenticator-libpam-1.04]# ./configure
[[email protected] google-authenticator-libpam-1.04]# make && make install
[[email protected] google-authenticator-libpam-1.04]# cp /usr/local/lib/security/pam_google_authenticator.so /lib64/security/
           

3.初始配置 Google Authenticator

[[email protected] google-authenticator-libpam-1.04]# google-authenticator
           

是否基于時間的認證,為了防止不同跨時區的問題,這裡選擇n

Do you want authentication tokens to be time-based (y/n) n
           

然後會跳出一個google的二維碼

紅色框框是: 生成的密鑰

綠色框框是: 生成的5個一次性緊急驗證碼,用于緊急情況下,使用過一次後該驗證碼即失效了。

ssh密碼登入+ Google Authenticator 實作雙向認證

是否更新使用者的 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認證,在第一行添加

[[email protected] ~]# vim  /etc/pam.d/sshd  #添加如下行
auth       required     pam_google_authenticator.so
           

5.修改sshd配置,關聯google認證

[[email protected] ~]# vim /etc/ssh/sshd_config
ChallengeResponseAuthentication yes     #修改為yes
[[email protected] ~]# systemctl restart sshd    #重新開機sshd服務
           

6.用戶端通過ssh連接配接伺服器測試

ssh密碼登入+ Google Authenticator 實作雙向認證

需要輸入動态密碼,動态密碼通過手機擷取如下圖所示

ssh密碼登入+ Google Authenticator 實作雙向認證

7.檢視服務端的安全日志檔案,可以看到是先程序google動态密碼認證

ssh密碼登入+ Google Authenticator 實作雙向認證

8.注意事項:

1.用password + google authenticator,如果使用公鑰登入的話,會跳過google authenticator驗證直接登入伺服器的。

2.如果是内網測試使用,建議安裝google authenticator 浏覽器插件實踐。如果是公網伺服器建議安裝手機版的Authenticator

9.安裝Google authenticator

Andorid版: “自行百度”

iOS版: 下載下傳 “Authenticator”

chrome浏覽器有google authenticator的插件

轉載于:https://www.cnblogs.com/xuliangwei/p/10883623.html

繼續閱讀