ssh開啟MFA雙因素認(rèn)證
為了防止咱服務(wù)器被嗨客囸穿咱可是煞費(fèi)苦心,不過ssh沒法不用,單用秘鑰登錄的話有急事電腦不在手上的話就不太方便,開密碼登錄右容易被暴力破解,在了解到有MFA這個(gè)好玩意后,覺得挺不錯(cuò)的,就想拿來用用。
在網(wǎng)上查到很多ssh開啟mfa的教程,但是都不適合咱的服務(wù)器,沒法子,照著翻譯和百度把配置文件一條條捋,經(jīng)過一番摸索,終于找到了能用的配置方法,在此記錄一下防止以后遺忘。
此處只針對(duì)ubuntu22.04系統(tǒng),其他系統(tǒng)搞毀了咱概不負(fù)責(zé)!記得整的時(shí)候備份好配置文件!
好了,開沖
一、安裝Google Authenticator PAM模塊
更新本機(jī)軟件源和軟件包
sudo apt update -y
sudo apt upgrade -y
安裝libpam-google-authenticator
sudo apt install libpam-google-authenticator
二、生成身份驗(yàn)證令牌
在此之前,手機(jī)需要安裝身份驗(yàn)證器,在此推薦使用 Google身份驗(yàn)證器或Microsoft Authenticator,你可以在Google Play或小米應(yīng)用商店等手機(jī)應(yīng)用商店里下載(首先,排除酷安,沒想到你的濃眉大眼的這兩個(gè)軟件竟然都沒有)
因?yàn)镚oogle服務(wù)在國(guó)內(nèi)基本算是個(gè)殘廢,這里咱用的是Microsoft Authenticator
執(zhí)行生成身份令牌命令
google-authenticator
Do you want authentication tokens to be time-based (y/n) y
----------------------
此處二維碼使用手機(jī)掃描
已驗(yàn)證ID->掃描QR碼
----------------------
Your new secret key is:XXXXXXXXX
Enter code from app (-1 to skip):【輸入手機(jī)上的六位數(shù)字】
Code confirmed
Your emergency scratch codes are:
?XXXXXXXX
?XXXXXXXX
?XXXXXXXX
?XXXXXXXX
?XXXXXXXX
【剩下的步驟全都輸入y即可】
Do you want me to update your "/home/XXXXXX/.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 atime 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
三、配置ssh使用MFA
打開PAM配置文件/etc/pam.d/sshd
sudo nano /etc/pam.d/sshd
將以下行添加到文件底部
auth required pam_google_authenticator.so nullok
auth required pam_permit.so
打開SSH配置文件/etc/ssh/sshd_config
sudo nano /etc/ssh/sshd_config
找到以下配置參數(shù)
ChallengeResponseAuthentication no
將配置參數(shù)改為yes
ChallengeResponseAuthentication yes
如果找不到上面參數(shù),直接將其復(fù)制粘貼到文件最底部也可
找到PasswordAuthentication
和KbdInteractiveAuthentication
兩個(gè)配置,將其參數(shù)改為以下
(注意:基本上所有教程都沒有說關(guān)于KbdInteractiveAuthentication
的配置,如果你的文件中默認(rèn)這個(gè)配置是no的話,一定要把它改成yes,否則mfa將不生效!這個(gè)讓我之前踩了很多的坑 )
PasswordAuthentication no
KbdInteractiveAuthentication yes
保存退出,然后重啟ssh服務(wù)
sudo service ssh restart
至此,ssh已經(jīng)成功設(shè)置MFA雙因素認(rèn)證登錄
在此之后,在不使用密鑰的情況下登錄ssh,需要先輸入ssh密碼,再輸入MFA動(dòng)態(tài)口令。
這樣即使是在別的電腦上登錄安全性也有所保障,雖然多了這一步,但應(yīng)應(yīng)急夠用了。
至于使用秘鑰登錄的話,該咋樣還是咋樣,么得如何影響。