溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

用Google Authenticator加強(qiáng)SSH登錄安全性

發(fā)布時(shí)間:2020-07-05 22:02:22 來(lái)源:網(wǎng)絡(luò) 閱讀:768 作者:ljl_19880709 欄目:安全技術(shù)

環(huán)境:
CentOS 6.3 x86_64

基礎(chǔ)組件安裝:

yum -y install wget gcc make pam-devel libpng-devel


一、安裝qrencode
在Linux上,有一個(gè)名為 QrenCode 的命令行工具可以很容易幫我們生成二維碼,google authenticator命令行生成二維碼就是調(diào)用它
。


wget http://fukuchi.org/works/qrencode/qrencode-3.3.1.tar.gz
tar zxf qrencode-3.3.1.tar.gz
cd qrencode-3.3.1
./configure --prefix=/usr && make && make install


二、安裝google authenticator PAM插件



wget http://google-authenticator.googlecode.com/files/libpam-google-authenticator-1.0-source.tar.bz2
tar jxf libpam-google-authenticator-1.0-source.tar.bz2
cd libpam-google-authenticator-1.0
make && make install


三、配置google authenticator

Google Authenticator 其實(shí)是一套開(kāi)源的解決方案,所以不僅在 Google 的網(wǎng)站上能用,在其他地方也能用的。然而,在 Google 的網(wǎng)站上,會(huì)直接給你一個(gè) QR 碼讓你掃的,而自己配置的 Google Authenticator 則要自己生成了。
首先需要切換到對(duì)應(yīng)的用戶,如果 VPS 上只有一個(gè)用戶的話,自然是可以省略這一步的,但是多用戶的 VPS 需要先切換到對(duì)應(yīng)的用戶,再運(yùn)行 google-authenticator 命令,程序會(huì)問(wèn)你Do you want authentication tokens to be time-based (y/n),大意是基于時(shí)間生成驗(yàn)證碼(及TOTP),這里選擇y。結(jié)果類似這樣:

用Google Authenticator加強(qiáng)SSH登錄安全性
這個(gè) QR 碼自然是給 Google Authenticator 應(yīng)用程序來(lái)掃描的,也可以訪問(wèn)上面的那個(gè)鏈接,用 Google Chart API 生成的 QR 碼來(lái)掃描。還可以照著 QR 碼下面的文字密鑰手工輸入。當(dāng) Google Authenticator 識(shí)別了這個(gè)賬號(hào)之后,驗(yàn)證器就配置好了。在文字密鑰下面還提供了幾個(gè)應(yīng)急碼,為手機(jī)丟了等情況下所用的,可以妥善保管。
這時(shí) Google Authenticator 雖然運(yùn)行了,但是相關(guān)設(shè)置還沒(méi)有保存,程序會(huì)問(wèn)你 Do you want me to update your “/root/.google_authenticator” file (y/n) (是否將配置寫(xiě)入家目錄的配置文件),當(dāng)然是回答 y 了。又會(huì)問(wèn)

Do you want to disallow multiple uses of the same authenticationtoken? This restricts you to one login about every 30s, but it increasesyour chances to notice or even prevent man-in-the-middle attacks (y/n)

大意是說(shuō)是否禁止一個(gè)口令多用,自然也是答 y。下一個(gè)問(wèn)題是

By default, tokens are good for 30 seconds and in order to compensate forpossible time-skew between the client and the server, we allow an extratoken before and after the current time. If you experience problems with poortime synchronization, you can increase the window from its defaultsize of 1:30min to about 4min. Do you want to do so (y/n)

大意是問(wèn)是否打開(kāi)時(shí)間容錯(cuò)以防止客戶端與服務(wù)器時(shí)間相差太大導(dǎo)致認(rèn)證失敗。這個(gè)可以根據(jù)實(shí)際情況來(lái)。我的ipad時(shí)間很準(zhǔn)(與網(wǎng)絡(luò)同步的),所以答 n,如果一些平板電腦不怎么連網(wǎng)的,可以答 y 以防止時(shí)間錯(cuò)誤導(dǎo)致認(rèn)證失敗。再一個(gè)問(wèn)題是

If the computer that you are logging into isn't hardened against brute-forcelogin 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)

選擇是否打開(kāi)嘗試次數(shù)限制(防止暴力***),自然答 y。
問(wèn)題答完了,家目錄中多出一個(gè) .google_authenticator 文件(默認(rèn)權(quán)限為 400),這時(shí)客戶端與服務(wù)端已經(jīng)配套起來(lái)了,以后不用再運(yùn)行 google-authenticator 命令了,否則會(huì)重新生成一組密碼。

四、配置SSH驗(yàn)證
此時(shí)雖然 Google Authenticator 已經(jīng)配置好了,但是并沒(méi)有任何程序會(huì)去調(diào)用它。所以需要設(shè)置 SSH 登錄的時(shí)候去通過(guò)它驗(yàn)證。
打開(kāi) /etc/pam.d/sshd 文件,添加
auth required pam_google_authenticator.so

這一行,保存。再打開(kāi) /etc/ssh/sshd_config 文件,找到
ChallengeResponseAuthentication no

把它改成
ChallengeResponseAuthentication yes

并保存。最后,輸入
service ssh restart

來(lái)重啟 SSH 服務(wù)以應(yīng)用新的配置。
這時(shí)候再用 SSH 登錄的話就會(huì)這樣了:

用Google Authenticator加強(qiáng)SSH登錄安全性
這樣就成功了。


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI