google Authenticator(谷歌身份驗(yàn)證器),是谷歌公司推出的一款動(dòng)態(tài)令牌工具,解決賬戶使用時(shí)遭到的一些不安全的操作進(jìn)行的“二次驗(yàn)證”,認(rèn)證器基于RFC文檔中的HOTP/TOTP算法實(shí)現(xiàn) ,是一種從共享秘鑰和時(shí)間或次數(shù)一次性令牌的算法。在實(shí)際應(yīng)用中可以通過認(rèn)證器方式對(duì)賬戶有更好的保護(hù)
下面在centos7下利用Google Authenticator實(shí)現(xiàn)SSH登錄的二次身份驗(yàn)證
1、配置EPEL源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache
2、安裝Google Authenticator
yum install google-authenticator -y
3、google-authenticator初始化配置
哪個(gè)賬號(hào)需要?jiǎng)討B(tài)驗(yàn)證碼,切換到該賬號(hào)下操作進(jìn)行操作 下面創(chuàng)建如下一個(gè)賬戶進(jìn)行操作
useradd yuanfan
passwd yuanfan
su - yuanfan
google-authenticatorYour new secret key is: G5RR2IJG2X74MI4ADW622R6PUA #如果在手機(jī)的谷歌身份驗(yàn)證器上不想通過"掃描條形碼"的方式添加,就輸入這個(gè)key,通過"手動(dòng)輸入驗(yàn)證碼的方式"Your verification code is 906194
Your emergency scratch codes are:
#下面會(huì)生成5個(gè)緊急驗(yàn)證碼(當(dāng)無(wú)法獲取動(dòng)態(tài)驗(yàn)證碼或驗(yàn)證碼不能使用時(shí)可以使用這5個(gè)),
#需要注意的是:這5個(gè)驗(yàn)證碼用一個(gè)就會(huì)少一個(gè)!請(qǐng)保存好!
45731126
22009120
27836115
94685459
48945214
Do you want me to update your "/home/yuanfan/.google_authenticator" file? (y/n) y #提示是否要更新驗(yàn)證文件,選擇yDo 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 #默認(rèn)動(dòng)態(tài)驗(yàn)證碼在30秒內(nèi)有效,由于客戶端和服務(wù)器可能會(huì)存在時(shí)間差,可將時(shí)間增加到最長(zhǎng)4分鐘,是否要這么做:這里選擇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 #是否限制嘗試次數(shù),每30秒只能嘗試最多3次,這里選擇y進(jìn)行限制
4、修改SSH登錄相關(guān)配置文件參數(shù)
如下行添加如下參數(shù)
vim /etc/pam.d/sshd (如下位置增加下面一行)
auth required pam_google_authenticator.so
vim /etc/ssh/sshd_config
將ChallengeResponseAuthentication修改為yes
ChallengeResponseAuthentication yes
service sshd restart
5、手機(jī)下載google身份驗(yàn)證器
以Android/ target=_blank class=infotextkey>安卓手機(jī)為例 掃描QR碼方式來(lái)進(jìn)行綁定
6、登錄驗(yàn)證測(cè)試
ssh yuanfan@localhost
然后輸入手機(jī)google身份驗(yàn)證器生成的動(dòng)態(tài)驗(yàn)證碼(每30秒更新一次)
SecureCRT登錄時(shí),將Keyboard Interactive登錄方式調(diào)整為第一個(gè)位置
注意,如果沒有用root用戶進(jìn)行第三步的操作,需要先其它用戶登錄后su root切換到root
https://wiki.archlinux.org/index.php/Google_Authenticator