新闻中似乎每天都在报道我们的数据面临风险的安全漏洞。 尽管 SSH 是远程连接到系统的一种安全方式,但您仍然可以使其更加安全。 本文将向您展示如何操作。
这就是双因素身份验证 (2FA) 的用武之地。即使您禁用密码并且只允许使用公钥和私钥进行 SSH 连接,如果未经授权的用户窃取了您的密钥,他们仍然可以访问您的系统。
使用双重身份验证,您无法仅使用 SSH 密钥连接到服务器。 您还需要提供手机上的验证器应用程序显示的随机生成的数字。
基于时间的一次性密码算法 (TOTP) 是本文中展示的方法。 谷歌身份验证器 用作服务器应用程序。 Google Authenticator 默认在 Fedora.
对于您的手机,您可以使用任何兼容 TOTP 的双向认证应用程序。 有许多适用于 Android 或 IOS 的免费应用程序可与 TOTP 和 Google Authenticator 配合使用。 本文使用 免费OTP 作为一个 example.
安装和设置 Google 身份验证器
首先,在您的服务器上安装 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)。 如果您有 Android 手机,您可以在 Google Play 中找到它,或者在 Apple iPhone 的 iTunes 商店中找到它。
屏幕上会显示一个二维码。 在您的手机上打开 FreeOTP 应用程序。 要添加新帐户,请选择应用顶部的二维码形状工具,然后扫描二维码。 设置完成后,每次远程连接到服务器时,您都必须提供验证器应用程序生成的随机数。
完成配置
该应用程序会询问更多问题。 这 example 下面向您展示如何回答以设置合理安全的配置。
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
现在您必须设置 SSH 以利用新的双向身份验证。
配置 SSH
在完成这一步之前, 确保您已经建立了有效的 SSH 连接 使用公共 SSH 密钥,因为我们将禁用密码连接。 如果出现问题或错误,建立连接将使您能够解决问题。
在您的服务器上,使用 sudo 编辑 /etc/pam.d/sshd 文件。
$ sudo vi /etc/pam.d/sshd
注释掉 auth substack password-auth 行:
#auth substack password-auth
将以下行添加到文件的底部。
auth sufficient pam_google_authenticator.so
Save 和 close 文件。 接下来,编辑 /etc/ssh/sshd_config 文件。
$ sudo vi /etc/ssh/sshd_config
查找 ChallengeResponseAuthentication 行并将其更改为 yes。
ChallengeResponseAuthentication yes
查找 PasswordAuthentication 行并将其更改为 no。
PasswordAuthentication no
将以下行添加到文件的底部。
AuthenticationMethods publickey,password publickey,keyboard-interactive
Save 和 close 文件,然后重新启动 SSH。
$ sudo systemctl restart sshd
测试您的双因素身份验证
现在,当您尝试连接到您的服务器时,系统会提示您输入验证码。
[user@client ~]$ ssh [email protected]
Verification code:
验证码是由您手机上的验证器应用程序随机生成的。 由于此数字每隔几秒更改一次,因此您需要在更改之前输入它。
如果不输入验证码,将无法访问系统,并且会出现权限被拒绝错误:
[user@client ~]$ ssh [email protected]
Verification code:
Verification code:
Verification code:
Permission denied (keyboard-interactive).
[user@client ~]$
结论
通过添加这种简单的双向身份验证,您现在使未经授权的用户更难访问您的服务器。