如何實現SSH通過掃二維碼登錄Linux服務器

背景

Linux服務器通過ssh遠程連接的時候,如果使用的是用戶名+密碼的驗證方式,萬一密碼泄露或者密碼過於簡單被暴li破解,服務器的安全就得不到保障。由此,可以結合谷歌動態驗證口令來爲服務器再加一道安全防護門。此時,就算是用戶密碼被泄露,但沒有動態驗證口令也是無法登錄服務器的。

環境

OS:CentOS7
軟件包:google-authenticator.x86_64
安卓手機一部(某米)
谷歌動態口令的下載及使用自行百度

操作步驟

1.一鍵安裝腳本

#安裝epel
yum install -y epel-release.noarch &> /dev/null
yum makecache  &> /dev/null
#安裝google authenticator
yum install -y google-authenticator.x86_64 &> /dev/null

echo -e "\033[31mDo you want me to update your "/root/.google_authenticator" file? (y/n) y"
echo -e "\033[31m你希望我更新你的“/root/.google_authenticator”文件嗎(y/n)?\033[0m"
echo -e "\033[31mDo you want to disallow multiple uses of the same authentication"
echo -e "\033[31mtoken? This restricts you to one login about every 30s, but it increases"
echo -e "\033[31myour chances to notice or even prevent man-in-the-middle attacks (y/n) y"
echo -e "\033[31m你希望禁止多次使用同一個驗證令牌嗎?這限制你每次登錄的時間大約是30秒, 但是這加大了發現甚至防止中間人攻ji的可能性(y/n)?\033[0m"
echo -e "\033[31mBy default, a new token is generated every 30 seconds by the mobile app."
echo -e "\033[31mIn order to compensate for possible time-skew between the client and the server,"
echo -e "\033[31mwe allow an extra token before and after the current time. This allows for a"
echo -e "\033[31mtime skew of up to 30 seconds between authentication server and client. If you"
echo -e "\033[31mexperience problems with poor time synchronization, you can increase the window"
echo -e "\033[31mfrom its default size of 3 permitted codes (one previous code, the current"
echo -e "\033[31mcode, the next code) to 17 permitted codes (the 8 previous codes, the current"
echo -e "\033[31mcode, and the 8 next codes). This will permit for a time skew of up to 4 minutes"
echo -e "\033[31mbetween client and server."
echo -e "\033[31mDo you want to do so? (y/n) y"
echo -e "\033[31m默認情況下,令牌保持30秒有效;爲了補償客戶機與服務器之間可能存在的時滯,\033[0m"
echo -e "\033[31m我們允許在當前時間前後有一個額外令牌。如果你在時間同步方面遇到了問題, 可以增加窗口從默認的3個可通過驗證碼增加到17個可通過驗證碼,\033[0m"
echo -e "\033[31m這將允許客戶機與服務器之間的時差增加到4分鐘。你希望這麼做嗎(y/n)?\033[0m"
echo -e "\033[31mIf the computer that you are logging into isn't hardened against brute-force"
echo -e "\033[31mlogin attempts, you can enable rate-limiting for the authentication module."
echo -e "\033[31mBy default, this limits attackers to no more than 3 login attempts every 30s."
echo -e "\033[31mDo you want to enable rate-limiting? (y/n) y"
echo -e "\033[31m如果你登錄的那臺計算機沒有經過固化,以防範運用蠻力的登錄企圖,可以對驗證模塊\033[0m"
echo -e "\033[31m啓用嘗試次數限制。默認情況下,這限制攻ji者每30秒試圖登錄的次數只有3次。 你希望啓用嘗試次數限制嗎(y/n)?\033[0m"
echo -e "\033[32m 在App Store 搜索Google Authenticator 進行App安裝 \033[0m"

expect << EOF
spawn google-authenticator
expect {
    "y/n" {send "y\n";exp_continue}
    "y/n" {send "y\n"}
    "y/n" {send "y\n"}
    "y/n" {send "y\n"}
}
EOF

#/etc/pam.d/sshd文件,修改或添加下行保存
#auth required pam_google_authenticator.so
sed -i '1a\auth       required     pam_google_authenticator.so' /etc/pam.d/sshd
#編輯/etc/ssh/sshd_config找到下行
#ChallengeResponseAuthentication no
#更改爲
#ChallengeResponseAuthentication yes
sed -i 's/ChallengeResponseAuthentication no/ChallengeResponseAuthentication yes/' /etc/ssh/sshd_config

#重啓SSH服務
systemctl restart sshd

2.運行腳本後直接掃屏幕生成的二維碼或者複製二維碼上方的URL到瀏覽器打開後掃碼,然後使用掃碼生成的動態口令進行登錄
2.1打開遠程連接工具xshell,如圖
如何實現SSH通過掃二維碼登錄Linux服務器
2.2輸入動態口令
如何實現SSH通過掃二維碼登錄Linux服務器
2.3輸入服務器密碼
如何實現SSH通過掃二維碼登錄Linux服務器
2.4查看/var/log/secure日誌可以發現ssh遠程登錄通過了pam_google_authenticator動態模塊的驗證並且成功登錄服務器
如何實現SSH通過掃二維碼登錄Linux服務器

遇到的問題

1.使用某米手機掃二維碼的時候出現如下提示
如何實現SSH通過掃二維碼登錄Linux服務器
於是安裝了Google play應用,但是打開後閃退,搜索相關資料後貌似是沒有谷歌服務框架的原因,於是又下載了谷歌服務框架應用,仍然沒有成功,可能科學上網可以解決此問題。
如何實現SSH通過掃二維碼登錄Linux服務器
由於時間問題沒再折騰,後面換了某爲手機就行了。
2.如果手機掃碼不成功,可以chrome 網上應用店搜索“身份驗證器”插件,然後添加到Chrome瀏覽器進行掃碼
如何實現SSH通過掃二維碼登錄Linux服務器
如何實現SSH通過掃二維碼登錄Linux服務器
如何實現SSH通過掃二維碼登錄Linux服務器
如何實現SSH通過掃二維碼登錄Linux服務器
3.如果手機丟失或者其它情況導致無法獲取到動態口令,可以使用緊急刮刮碼來登錄服務器,默認配置文件是/root/.google_authenticator,不過這裏面的刮刮碼用一個少一個,建議保存到安全的地方。
如何實現SSH通過掃二維碼登錄Linux服務器

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章