用 Google Authenticator 設定 CentOS 7 OTP 認證

要先安裝 epel

# yum install epel-release

然後再安裝下列套件

# yum install google-authenticator

在用戶端可以先用 google-authenticator 指令產生設定檔案,請回答好該程式的問題進行設定。然後,記得掃描 QR Code到你的手機。

$ google-authenticator

Do you want authentication tokens to be time-based (y/n) y

(omit)

Do you want me to update your “/home/USERID/.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) 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


修改 pam.d 的 sshd 設定檔案,看是要不要強制使用 otp 不強制可以用 nullok 選項

# vi /etc/pam.d/sshd
auth required pam_google_authenticator.so
# or
auth required pam_google_authenticator.so nullok

然後修改 sshd_config

# vi /etc/ssh/sshd_config
ChallengeResponseAuthentication yes

看要讓所有的人都使用 otp 還是部份的使用者,如果要所有人就把 Match User 拿掉

Match User USERID
AuthenticationMethods publickey,password publickey,keyboard-interactive

從新啟動 sshd

# systemctl restart sshd

就可以使用 兩階段認證

$ ssh lalala
Password:
Verification code:
Last login: Thu Jun 27 10:17:46 2019 from yoyoyo
[USERID@lalala ~]

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.