1. 程式人生 > >使用Google Authenticator給ssh進行登錄驗證

使用Google Authenticator給ssh進行登錄驗證

谷歌 google 安全 ssh 賬號登陸

普通情況下的服務器登錄,是“服務器+密碼”這種直白的驗證方式,但是這種方式太過簡單,一旦密碼泄露,服務器就有危險,於是為了安全我們就要在登錄上再加一把鎖,那就是使用Google Authenticator(谷歌身份驗證器)這個工具,在登錄的時候進行一次驗證,只有“驗證通過了”+“密碼正確”才能登陸服務器。


安裝前準備:

1)關閉Selinux :#setenforce 0

2)安裝依賴:#yum -y install gcc make pam-devel libpng-devel libtool wget git

3)添加阿裏雲epel 源:

RHEL 6/Centos 6
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
RHEL 7/Centos 7
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

4)安裝Qrencode,谷歌身份驗證器需要調用該程序生成二維碼並顯示:#yum install -y qrencode


安裝谷歌身份驗證器

這個時候很多教程會讓你去執行#git clone https://github.com/google/google-authenticator.git ,然而現在這個git裏面已經不再含有libpam這個文件夾了,下載下來是一個錯誤的包,那麽這個時候你可以使用#yum install google-authenticator,不過yum安裝的身份驗證器的版本很老,這個時候可以執行#wget https://github.com/google/google-authenticator-libpam/archive/1.04.tar.gz


下載下來1.0.4版本的然後拆包解壓縮,裏面是這樣幾個文件:

技術分享


然後就#./bootstrap.sh && ./configure && make && make install 進行編譯和安裝。


安裝過程完畢之後,還要復制google 身份驗證器pam模塊到系統下,命令是:#cp /usr/local/lib/security/pam_google_authenticator.so /lib64/security/


調整登陸方式

1)編輯/etc/pam.d/sshd 這個文件,我這個centos的版本是7.0的,裏面的內容可能跟centos 6.x的優點不同,不過沒關系,就需要插入黃色框內的“auth required pam_google_authenticator.so”,如圖:

技術分享


修改完畢之後,保存退出。


註意!修改了這步之後,服務器千萬不能斷開連接,否則再連是需要google驗證碼的,而我們現在還沒有生成碼,所以肯定是無法連接服務器,如果是雲服務器,可以通過登陸控制臺的方式把這個文件修改回來,如果是實體服務器,那就呵呵呵了。


2)編輯/etc/ssh/sshd_config,就修改一個地方:ChallengeResponseAuthentication yes

3)保存退出之後,重啟一下ssh服務:

RHEL6 /Centos6:Service sshd restart
RHEL7 /Centos7:Systemctl resart sshd


生成登陸驗證碼

這次以root用戶為例,那麽切換成root用戶執行下面的過程。

1)執行#google-authenticator,由於我們之前已經安裝了qrencode,那麽這個時候會生成一個超級超級巨大的二維碼,給各位感受一下:

技術分享


紅色內容是生成的密鑰,很重要。綠色的內容是備用的緊急救助碼,緊急救助碼就是當你無法獲取認證碼時(比如手機丟了),可以當做認證碼來用,每用一個少一個,但其實可以手動添加的,建議如果 root 賬戶使用 Google Authenticator 的話一定要把緊急救助碼另外保存一份。


Do you want me to update your "/home/test/.google_authenticator" file? (y/n) y

是否更新用戶的 Google Authenticator 配置文件,選擇 y 才能使上面操作對當前用戶生效,其實就是在對應用戶的 Home 目錄下生成了一個 .google_authenticator 文件,如果你想停用這個用戶的 Google Authenticator 驗證,只需要刪除這個用戶 Home 目錄下的 .google_authenticator 文件就可以了。


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

每次生成的認證碼是否同時只允許一個人使用?這裏選擇 y。


By default, tokens are good for 30 seconds. 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. If you experience problems with poor time synchronization, you can increase the window from its default size of +-1min (window size of 3) to about +-4min (window size of 17 acceptable tokens). Do you want to do so? (y/n) n

是否增加時間誤差?這裏隨便選擇, ny都可以。


If the computer that you are logging into isn‘t hardened against brute-force login attempts, you can enable rate-limiting for the authentication module. By default, this limits attackers to no more than 3 login attempts every 30s. Do you want to enable rate-limiting (y/n) y

是否啟用次數限制?這裏選擇 y,默認每 30 秒最多嘗試登錄 3 次。


如果想要寫成腳本的話,那麽上面交互式的設置也可用通過參數一次性設置:google-authenticator -t -f -d -l [email protected] -i MR.chen -r 3 -R 30 -W。


-I 和 -i是可以隨便寫的,但是-i後期可以改,-I不能改。


搭配手機端

如果手機是ios,就去apple store裏搜索“Google Authenticator”,如果是安卓,就去應用商店搜索“谷歌動態口令”。


安裝完後,打開App,點擊“開始設置”,選擇“掃描條形碼”掃描上面 google-authenticator 命令生成的二維碼,或者是選擇“輸入密鑰”,然後手機上就能看到對應的六位數認證碼了。


最後一步,返回xshell,修改登陸方式,設置登陸方法為Keyboard Interactive,如圖:

技術分享


這個時候,推薦各位保留原有的ssh不要動,在另外一個xshell窗口登陸一下看看效果,如果正常的話,這個時候會看到系統會讓你先輸入一個Verification code。這個值就是手機裏的那個六位數,然後再輸入密碼,只有兩個都是正確的,才能登陸!


至此整個配置完成,如果登陸時遇到問題,請查看日誌文件/var/log/secure。


更改存儲位置

在生成二維碼那一步的時候,如果你錯過了記住密鑰也不要怕,系統會自動把密鑰和緊急救助碼保存在~/.google_authenticator這個文件裏。


如果想要改變密鑰存儲位置,請使用--secret參數:#google-authenticator --secret="/文件路徑/用戶名"


然後更改/etc/pam.d/sshd內的路徑配置:

auth required pam_google_authenticator.so user=root secret=/PATH_FOLDER/${USER}


上面那句話裏“user=root” 用於強制PAM使用root用戶權限來搜索文件。


另外請註意,由於我們當時切換成了root用戶,所以密鑰文件的所有者是root,生成文件的用戶只能讀取文件(chmod: 400):

#chown root.root /PATH_FILE/SECRET_KEY_FILES

#chmod 400 /PATH_FILE/SECRET_KEY_FILES



最後的最後,如果您覺得本文對您升職加薪有幫助,那麽請不吝贊助之手,刷一下下面的二維碼,贊助本人繼續寫更多的博文!

技術分享


本文出自 “生活就是等待戈多” 博客,請務必保留此出處http://chenx1242.blog.51cto.com/10430133/1980860

使用Google Authenticator給ssh進行登錄驗證