天天看點

Linux 利用Google Authenticator實作ssh登入雙因素認證

1.介紹

雙因素認證:雙因素身份認證就是通過你所知道再加上你所能擁有的這二個要素組合到一起才能發揮作用的身份認證系統。雙因素認證是一種采用時間同步技術的系統,采用了基于時間、事件和密鑰三變量而産生的一次性密碼來代替傳統的靜态密碼。每個動态密碼卡都有一個唯一的密鑰,該密鑰同時存放在伺服器端,每次認證時動态密碼卡與伺服器分别根據同樣的密鑰,同樣的随機參數(時間、事件)和同樣的算法計算了認證的動态密碼,進而確定密碼的一緻性,進而實作了使用者的認證。因每次認證時的随機參數不同,是以每次産生的動态密碼也不同。由于每次計算時參數的随機性保證了每次密碼的不可預測性,進而在最基本的密碼認證這一環節保證了系統的安全性。

說白了,就像我們幾年前去銀行辦卡送的密碼牌,以及網易遊戲中的将軍令,在你使用網銀或登陸遊戲時會再讓你輸入動态密碼的。

2.目的

實作登入Linux 伺服器時,除了輸入使用者名密碼外,需要輸入一次性的動态密碼才能驗證成功。

3.安裝過程

3.1安裝chrony

生成動态密碼的其中一個因素是時間,需要保持終端裝置和伺服器的系統時間一緻,才能生成同一的動态密碼

簡單說下chrony:chrony 是網絡時間協定的(NTP)的另一種實作,與網絡時間協定背景程式(ntpd)不同,它可以更快地更準确地同步系統始終。

國内比較好用的ntp伺服器:​​官網​​

注,同步時間時請準确設定系統時區

[root@localhost ~]# yum install -y chrony
[root@localhost ~]# vim /etc/chrony.conf
server 0.cn.pool.ntp.org iburst
[root@localhost ~]# systemctl restart chronyd
[root@localhost ~]# chronyc sources210 Number of sources = 4MS Name/IP address Stratum Poll Reach LastRx Last sample===============================================================================
^? 223.112.179.133 0 6 0 10y +0ns[ +0ns] +/- 0ns^* dns1.synet.edu.cn 2 6 33 0 +113us[ +284us] +/- 28ms^? 2001:da8:202:10::61 0 6 0 10y +0ns[ +0ns] +/- 0ns^? 42.96.167.209 2 6 10 8 +2011us[ +949us] +/- 127ms
[root@localhost ~]# date2016年 12月 31日 星期六 09:30:24 CST
      

3.2安裝依賴元件

[root@localhost ~]# yum install -y git automake libtool pam-devel
      

3.3下載下傳谷歌認證子產品

[root@localhost ~]# git clone https:~-xr-x  root root  12月  : google-authenticator-~]# cd google-authenticator-libpam/-authenticator-libpam]# ./bootstrap.-authenticator-libpam]# ./-authenticator-libpam]#  &&  -authenticator-libpam]# google--authenticator-libpam]# cd ~~]# vim /etc/pam.d/~]# vim /etc//~~]# google--based (y/
      

3.4 手機安裝身份驗證器

app下載下傳位址:​​http://www.coolapk.com/apk/com.google.android.apps.authenticator2​​ 

Linux 利用Google Authenticator實作ssh登入雙因素認證
Linux 利用Google Authenticator實作ssh登入雙因素認證
Linux 利用Google Authenticator實作ssh登入雙因素認證
Linux 利用Google Authenticator實作ssh登入雙因素認證
Linux 利用Google Authenticator實作ssh登入雙因素認證

4.登入驗證

注意,第一次登入可能會出現登入失敗的情況,檢視日志資訊顯示錯誤如下:

[root@localhost ~]# tail -n10 /var/log/secure
...
Dec 31 09:42:46 localhost sshd[2393]: PAM unable to dlopen(/usr/lib64/security/pam_google_authenticator.so): /usr/lib64/security/pam_google_authenticator.so: cannot open shared object file: No such file or directory
Dec 31 09:42:46 localhost sshd[2393]: PAM adding faulty module: /usr/lib64/security/pam_google_authenticator.so
...
[root@localhost ~]# ln -sv /usr/local/lib/security/pam_google_authenticator.so /usr/lib64/security/pam_google_authenticator.so"/usr/lib64/security/pam_google_authenticator.so" -> "/usr/local/lib/security/pam_google_authenticator.so"
      

開始再次登入

Linux 利用Google Authenticator實作ssh登入雙因素認證

iterm2下面,登入。先要輸入ssh密碼,然後還需要輸入一個生成器的随機密碼。2個密碼都正确才能登入到伺服器。

Linux 利用Google Authenticator實作ssh登入雙因素認證
Linux 利用Google Authenticator實作ssh登入雙因素認證

繼續閱讀