Linux下部署SSH登陸時的二次身份驗證環境記錄(利用Google Authenticator)

通常來講,使用ssh遠程登陸服務器,只須要輸入帳號和密碼,顯然這種方式不是很安全。爲了安全着想,可使用GoogleAuthenticator(谷歌身份驗證器),以便在帳號和密碼之間再增長一個驗證碼,只有輸入正確的驗證碼以後,再輸入密碼才能登陸。這樣就加強了ssh登陸的安全性。帳號、驗證碼、密碼三者缺一個都不能登陸,即便帳號和密碼正確,驗證碼錯誤,一樣登陸失敗。其中,驗證碼是動態驗證碼,而且是經過手機客戶端自動獲取(默認每隔30秒失效一次)。好了,廢話很少說了,下面記錄下GoogleAuthenticator部署過程(Centos6系統下):linux

1、關閉SELINUXvim

[root@test ~]# vim /etc/selinux/config      #永久關閉。須要reboot重啓後生效
SELINUX=disabled 

[root@test ~]# setenforce 0   #臨時性關閉。不須要reboot重啓

2、安裝編輯工具包安全

[root@test ~]# yum install wget gcc make
[root@test ~]# yum install pam-devel libpng-devel

下載repoforge第三方yum源及libpam-google-authenticator-1.0-source.tar.bz二、qrencode-3.4.4.tar.gz(後兩個軟件須要在***條件下才能下載。這裏我提早下載了)
下載地址:https://pan.baidu.com/s/1i4WDbyX
提取密碼:anxd

下載到/data/software目錄下
[root@test ~]# cd /data/software/
[root@test software]# ls
libpam-google-authenticator-1.0-source.tar.bz2  qrencode-3.4.4.tar.gz  rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

[root@test software]# rpm -ivh rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm

[root@test software]# yum install mercurial 

安裝google authenticator PAM插件
[root@test ~]# cd /data/software/
[root@test software]# tar -jxvf libpam-google-authenticator-1.0-source.tar.bz2 
[root@test software]# cd libpam-google-authenticator-1.0 
[root@test libpam-google-authenticator-1.0]# make && make install

安裝QrenCode,此工具能夠在Linux命令行下生成二維碼
[root@test ~]# cd /data/software/
[root@test software]# tar -zvxf qrencode-3.4.4.tar.gz 
[root@test software]# cd qrencode-3.4.4 
[root@test qrencode-3.4.4]# ./configure --prefix=/usr 
[root@test qrencode-3.4.4]# make && make install

3、配置ssh服務調用google authenticator PAM插件服務器

[root@test ~]# vim /etc/pam.d/sshd       #在第一行(即auth       required pam_sepermit.so的下一行)增長如下代碼     
auth required pam_google_authenticator.so
 
[root@test ~]# vim /etc/ssh/sshd_config
......
ChallengeResponseAuthentication yes          #修改no爲yes
 
[root@test ~]# service sshd restart

 

4、使用google authenticator PAM插件爲ssh登陸帳號生成動態驗證碼
注意:哪一個帳號須要動態驗證碼,請切換到該帳號下操做。(能夠在不一樣用戶下執行這個命令以生成各自的二次驗證碼)ssh

[root@test ~]# google-authenticatoride

Do you want authentication tokens to be time-based (y/n) y
https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@BJLX_NET_TEST-01%3Fsecret%3DCGB5NWP6SABN3TM7          #這個連接只能在***條件下才能打開工具

Your new secret key is: CGB5NWP6SABN3TM7    #若是在手機的谷歌身份驗證器上不想經過"掃描條形碼"的方式添加,就輸入這個key,經過"手動輸入驗證碼的方式"。帳號就是服務器主機名。
Your verification code is 730249
Your emergency scratch codes are:      #下面會生成5個緊急驗證碼(當沒法獲取動態驗證碼或驗證碼不能使用使用可使用這5個)
 66151894                                              #須要注意的是:這5個驗證碼用一個就會少一個!請保存好!
 91475582
 37383236
 70667696
 70522112ui

Do you want me to update your "/root/.google_authenticator" file (y/n) y       #提示是否要更新驗證文件,選擇ythis

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    #禁止使用相同口令google

By default, tokens are good for 30 seconds and 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 1:30min to about 4min. Do you want to do so (y/n) n          #默認動態驗證碼在30秒內有效,因爲客戶端和服務器可能會存在時間差,可將時間增長到最長4分鐘,是否要這麼作:這裏選擇是n,繼續默認30秒

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        #是否限制嘗試次數,每30秒只能嘗試最多3次,這裏選擇y進行限制

5、手機安裝Google身份驗證器,經過此工具掃描上一步生成的二維碼圖形,獲取動態驗證碼。
在App Store裏直接能夠下載Authenticator

而後掃描上面在服務器上生成的二維碼,每一個用戶都會有一個單獨的二維碼

接着在ssh的客戶端裏設置,以下,設置"Keyboard Interactive"方式登陸

而後再次鏈接的時候,就會提示先輸入二次身份驗證碼,再輸入用戶密碼。

在linux客戶機上遠程登陸,效果同樣:

[wangshibo@BJLX_NET_TEST-01 ~]$ ssh root@172.29.32.251 
The authenticity of host '[172.29.32.251]:22 ([172.29.32.251]:22)' can't be established.
RSA key fingerprint is 5c:e7:1a:05:8b:2e:66:99:20:90:1f:47:56:bf:b9:41.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[172.29.32.251]:22' (RSA) to the list of known hosts.
Verification: 
Password: 
[root@test ~]#
相關文章
相關標籤/搜索