网站搜索

如何在 Linux 中为 SSH 设置双因素身份验证


默认情况下,SSH 已在远程计算机之间使用安全数据通信,但如果您想为 SSH 连接添加一些额外的安全层,您可以添加 Google 身份验证器 (双因素身份验证)模块,允许您在连接到SSH服务器时输入随机一次性密码(TOTP)验证码。连接时,您必须通过智能手机PC输入验证码。

Google 身份验证器是一个开源模块,其中包含由 Google 开发的一次性密码 (TOTP) 验证令牌的实现。

它支持多种移动平台以及PAM(可插拔身份验证模块)。这些一次性密码是使用OATH开放身份验证倡议创建的开放标准生成的。

在本文中,我将向您展示如何在基于 RedHat 和基于 Debian 的 Linux 发行版(例如 Fedora、CentOS Stream、Rocky Linux 和 AlmaLinux)下设置和配置 SSH 进行双因素身份验证, Ubuntu、Debian 和 Mint。

在 Linux 中安装 Google 身份验证器

打开要设置双因素身份验证的计算机,然后安装以下 PAM 库以及 PAM 模块与 PAM 模块正常工作所需的开发库strong>Google 身份验证器模块。

在基于 RedHat 的系统上,使用以下 yum 命令安装“pam-devel”软件包。

yum install google-authenticator -y

在基于 Debian 的系统上,使用以下 apt 命令安装“libpam0g-dev”软件包。

sudo apt install libpam-google-authenticator -y

生成 Google 身份验证令牌

运行“google-authenticator”命令后,它会提示您一系列问题。

google-authenticator

在大多数情况下,只需键入“y”(yes) 作为答案即可。如果出现问题,您可以再次输入“google-authenticator”命令来重置设置。

  • 您是否希望身份验证令牌基于时间 (y/n) y

问完这个问题后,您将获得“密钥”和“紧急代码”。在某处写下这些详细信息,稍后我们将需要“密钥”来设置Google Authenticator应用。

google-authenticator

Do you want authentication tokens to be time-based (y/n) y
Warning: pasting the following URL into your browser exposes the OTP secret to Google:
  https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@tecmint%3Fsecret%3DCYZF2YF7HFGX55ZEPQYLHOO5JM%26issuer%3Dtecmint
Failed to use libqrencode to show QR code visually for scanning.
Consider typing the OTP secret into your app manually.
Your new secret key is: CYZF2YF7HFGX55ZEPQYLHOM
Enter code from app (-1 to skip): -1 Code confirmation skipped Your emergency scratch codes are: 83714291 53083200 80975623 57217008 77496339

接下来,按照设置向导进行操作,在大多数情况下,键入答案“y”(yes),如下所示。

Do you want me to update your "/root/.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, tokens are good for 30 seconds and 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 1:30min to about 4min. 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

在 Linux 中配置 SSH 以使用 Google 身份验证器

打开 PAM 配置文件“/etc/pam.d/sshd”并将以下行添加到文件底部。

auth required pam_google_authenticator.so nullok
auth required pam_permit.so

接下来,打开 SSH 配置文件“/etc/ssh/sshd_config”并向下滚动以找到显示的行。

ChallengeResponseAuthentication no

将其更改为“”。于是,就变成了这样。

ChallengeResponseAuthentication yes

最后,重新启动 SSH 服务以进行新的更改。

systemctl restart sshd
Or
sudo systemctl restart sshd

配置Google身份验证器应用程序

在您的智能手机上启动Google Authenticator应用。按+并选择“输入设置密钥”。如果您没有此应用,您可以在 Android/iPhone/Blackberry 设备上下载并安装 Google 身份验证器应用。

添加您的帐户“名称”并输入之前生成的“密钥”。

它将生成一个一次性密码(验证码),该密码将在您的手机上每30秒不断更改。

现在尝试通过SSH登录,系统将提示您输入Google身份验证器代码验证码)和密码 每当您尝试通过 SSH 登录时。您只有30秒的时间输入此验证码,如果错过,将重新生成新的验证码。

login as: tecmint
Access denied
Using keyboard-interactive authentication.
Verification code:
Using keyboard-interactive authentication.
Password:
Last login: Tue Apr 23 13:58:29 2022 from 172.16.25.125
[root@tecmint ~]#

如果您没有智能手机,您还可以使用名为 Authenticator 的 Firefox 插件来进行双因素身份验证。

重要:双因素身份验证适用于基于密码的 SSH 登录。如果您使用任何私钥/公钥 SSH 会话,它将忽略双因素身份验证并直接让您登录。