歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> Linux 利用Google Authenticator實現ssh登錄雙因素認證,authenticatorssh

Linux 利用Google Authenticator實現ssh登錄雙因素認證,authenticatorssh

日期:2017/3/3 17:56:46   编辑:學習Linux

Linux 利用Google Authenticator實現ssh登錄雙因素認證,authenticatorssh

Linux 利用Google Authenticator實現ssh登錄雙因素認證,authenticatorssh


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 sources
210 Number of sources = 4
MS 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 ~]# date
2016年 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://github.com/google/google-authenticator-libpam.git
[root@localhost ~]# ll
drwxr-xr-x 11 root root 4096 12月 27 16:29 google-authenticator-libpam

[root@localhost ~]# cd google-authenticator-libpam/
[root@localhost google-authenticator-libpam]# ./bootstrap.sh
[root@localhost google-authenticator-libpam]# ./configure
[root@localhost google-authenticator-libpam]# make && make install
[root@localhost google-authenticator-libpam]# google-authenticator
[root@localhost google-authenticator-libpam]# cd ~
[root@localhost ~]# vim /etc/pam.d/sshd

auth       required     pam_google_authenticator.so no_increment_hotp

[root@localhost ~]# vim /etc/ssh/sshd_config

asswordAuthentication yes
ChallengeResponseAuthentication yes
UsePAM yes

[root@localhost ~]# systemctl restart sshd
[root@localhost ~]# google-authenticator

Do you want authentication tokens to be time-based (y/n) y
#你想做的認證令牌是基於時間的嗎? Warning: pasting the following URL into your browser exposes the OTP secret to Google: https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/[email protected]%3Fsecret%3DN4HLEJOQHT27VCR6RX66WXB2SY%26issuer%3Dlocalhost.localdomain
Your new secret key is: N4HLEJOQHT27VCR6RX66WXB2SY
Your verification code is 299695
Your emergency scratch codes are:

  44477086
  92790948
  29251218
  26350870
  30696065

Do you want me to update your "/root/.google_authenticator" file? (y/n) y
#你希望我更新你的“/root/.google_authenticator”文件嗎(y/n)? 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
#你希望禁止多次使用同一個驗證令牌嗎?這限制你每次登錄的時間大約是30秒, 但是這加大了發現或甚至防止中間人攻擊的可能性(y/n)? 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
#默認情況下,令牌保持30秒有效;為了補償客戶機與服務器之間可能存在的時滯,
我們允許在當前時間前後有一個額外令牌。如果你在時間同步方面遇到了問題, 可以增加窗口從默認的3個可通過驗證碼增加到17個可通過驗證碼,
這將允許客戶機與服務器之間的時差增加到4分鐘。你希望這麼做嗎(y/n)? 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/n)?

3.4 手機安裝身份驗證器

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

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" 

開始再次登錄

到了激動人心的時刻了,從手機app中獲取此刻驗證碼為077625,在Verification code裡面輸入,如下:

http://xxxxxx/Linuxjc/1183947.html TechArticle

Copyright © Linux教程網 All Rights Reserved