1. 程式人生 > 實用技巧 >CentOS 7安裝Google Authenticator進行登陸二次驗證

CentOS 7安裝Google Authenticator進行登陸二次驗證

1、安裝依賴

yum install -y epel-* mercurial autoconf automake libtool pam-devel

2、安裝google-authenticator

yum install -y google-authenticator

4、PAM元件配置google-authenticator

vim /etc/pam.d/sshd
auth required pam_google_authenticator.so
# 或者
echo   "auth       required     pam_google_authenticator.so" >>/etc/pam.d/sshd

5、修改SSH配置

vim /etc/ssh/sshd_config
# 將
ChallengeResponseAuthentication no
# 改為
ChallengeResponseAuthentication yes
# 或者
sed -i 's/ChallengeResponseAuthentication no/ChallengeResponseAuthentication yes/g' /etc/ssh/sshd_config

6、重啟SSH服務

systemctl restart sshd.service

7、生成google-authenticator配置

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/root@demo%3Fsecret%3DXQ2WB526GLPJ7SI64Z3RZISOEE%26issuer%3Ddemo
                                                        
                                                                                  
                                                                                  
                                                                                  
             這裡會有一個二維碼,需要在手機上下載`googleauthenticator`APP掃碼繫結
             安卓 IOS手機都可以在應用商店搜尋安裝
                                                                                  
                                                                                  
                                                                                                                                                                     
Your new secret key is: XQ2WB526GLPJ7SI64Z3RZISOEE
Your verification code is 917990
Your emergency scratch codes are:
  42623319
  72314571
  14476695
  95764389
  38976136

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

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

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

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

8、調整XShell登陸配置

XShell登陸需要更改為Keyboard Interactive驗證登陸。

9、登陸時需要結合手機APP上的驗證碼才可以登陸

登陸的時候選擇Keyboard Interactive驗證登陸。

然後輸入伺服器密碼

輸入手機APP上的驗證碼

登陸成功

因為手機上的 google authenticator這個APP是根據時間來生成隨機驗證碼的,所以必須保證伺服器上的時間和手機時間一致,不然可能會出現無法登陸的情況!

建議安裝時間同步伺服器或者安裝一個自動同步時間的軟體(例如:chrony)

yum install -y chrony
systemctl enable chronyd
systemctl start chronyd