每天似乎都有一個安全漏洞的新聞報道,說我們的數(shù)據(jù)會因此而存在風險。盡管 ssh 是一種遠程連接系統(tǒng)的安全方式,但你仍然可以使它更安全。本文將向你展示如何做到這一點。
此時雙因子驗證two-factor authentication(2fa)就有用武之地了。即使你禁用密碼并只允許使用公鑰和私鑰進行 ssh 連接,但如果未經(jīng)授權(quán)的用戶偷竊了你的密鑰,他仍然可以借此訪問系統(tǒng)。
使用雙因子驗證,你不能僅僅使用 ssh 密鑰連接到服務器,你還需要提供手機上的驗證器應用程序隨機生成的數(shù)字。
本文展示的方法是基于時間的一次性密碼time-based one-time password(totp)算法。google authenticator 用作服務器應用程序。默認情況下,google authenticator 在 fedora 中是可用的。
至于手機,你可以使用與 totp 兼容的任何可以雙路驗證的應用程序。andorid 或 ios 有許多可以與 totp 和 google authenticator 配合使用的免費應用程序。本文與 freeotp 為例。
安裝并設置 google authenticator
首先,在你的服務器上安裝 google authenticator。 $ sudo dnf install -y google-authenticator
運行應用程序:
$ google-authenticator
該應用程序提供了一系列問題。下面的片段展示了如何進行合理的安全設置:
do you want authentication tokens to be time-based (y/n) y
do you want me to update your /home/user/.google_authenticator file (y/n)? y這個應用程序為你提供一個密鑰、驗證碼和恢復碼。把它們放在安全的地方。如果你丟失了手機,恢復碼是訪問服務器的唯一方式。
設置手機驗證
在你的手機上安裝驗證器應用程序(freeotp)。如果你有一臺安卓手機,那么你可以在 google play 中找到它,也可以在蘋果 iphone 的 itunes 商店中找到它。
google authenticator 會在屏幕上顯示一個二維碼。打開手機上的 freeotp 應用程序,選擇添加新賬戶,在應用程序頂部選擇二維碼形狀工具,然后掃描二維碼即可。設置完成后,在每次遠程連接服務器時,你必須提供驗證器應用程序生成的隨機數(shù)。
完成配置
應用程序會向你詢問更多的問題。下面示例展示了如何設置合理的安全配置。
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, tokens are good for 30 seconds. 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. if you experience problems with poor time synchronization, you can increase the window from its default size of +-1min (window size of 3) to about +-4min (window size of 17 acceptable tokens).
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現(xiàn)在,你必須設置 ssh 來利用新的雙路驗證。
配置 ssh
在完成此步驟之前,確保你已使用公鑰建立了一個可用的 ssh 連接,因為我們將禁用密碼連接。如果出現(xiàn)問題或錯誤,一個已經(jīng)建立的連接將允許你修復問題。
在你的服務器上,使用 sudo 編輯 /etc/pam.d/sshd 文件。
$ sudo vi /etc/pam.d/ssh
注釋掉 auth substack password-auth 這一行:
#auth substack password-auth
將以下行添加到文件底部:
auth sufficient pam_google_authenticator.so
保存并關(guān)閉文件。然后編輯 /etc/ssh/sshd_config 文件:
$ sudo vi /etc/ssh/sshd_config
找到 challengeresponseauthentication 這一行并將其更改為 yes:
challengeresponseauthentication yes
找到 passwordauthentication 這一行并將其更改為 no:
passwordauthentication no
將以下行添加到文件底部:
authenticationmethods publickey,password publickey,keyboard-interactive
保存并關(guān)閉文件,然后重新啟動 ssh:
$ sudo systemctl restart sshd
測試雙因子驗證
當你嘗試連接到服務器時,系統(tǒng)會提示你輸入驗證碼:
[user@client ~]$ ssh user@example.com
verification code:驗證碼由你手機上的驗證器應用程序隨機生成。由于這個數(shù)字每隔幾秒就會發(fā)生變化,因此你需要在它變化之前輸入它。
如果你不輸入驗證碼,你將無法訪問系統(tǒng),你會收到一個權(quán)限被拒絕的錯誤:
[user@client ~]$ ssh user@example.com
verification code:
verification code:
verification code:
permission denied (keyboard-interactive).
[user@client ~]$結(jié)論
通過添加這種簡單的雙路驗證,現(xiàn)在未經(jīng)授權(quán)的用戶訪問你的服務器將變得更加困難。
via: https://fedoramagazine.org/two-factor-authentication-ssh-fedora/
作者:curt warfield 選題:lujun9972 譯者:mjseven 校對:wxy