有没有办法对 Unix PAM 子系统上的 SSH 服务器上的所有用户强制执行 2FA?

Aru*_*rya -2 security users ssh 2-factor-authentication

有没有办法对 Unix PAM 子系统上 SSH 服务器上的所有用户强制执行双因素身份验证 (2FA)?

roa*_*ima 5

我无法访问像您这样的未命名 UNIX 系统,\n因此您必须根据您自己的情况\n调整我在此处描述的解决方案。\xc2\xa0\n(请随意编辑这个答案可以添加您的具体信息。)

\n

PAM子系统允许在身份验证过程中包含其他模块。\xc2\xa0\n其中一个提供Google Authenticator\n(以及\xc2\xa0任何兼容产品,例如 Microsoft 提供的产品)。

\n

sudo在开始之前,请确保您已建立根会话,并且在验证您仍然可以登录(并使用或)的另一个连接之前不要关闭它su

\n

这里是Debian 12(“书虫”)的安装和配置过程。你需要贯穿始终,所以从获取shellroot开始:sudo -sroot

\n
sudo -s\n\napt update\napt install libpam-google-authenticator\n\ncp -p /etc/pam.d/common-auth{,.$(date +\'%Y-%m-%d\')}\necho \'auth required pam_google_authenticator.so nullok echo_verification_code\' >>/etc/pam.d/common-auth\n
Run Code Online (Sandbox Code Playgroud)\n

现在编辑/etc/ssh/sshd_config不是 ssh_config)\n和\xc2\xa0添加或编辑这些行。\xc2\xa0\n如果\xc2\xa0您要添加它们,请将它们直接放在下面UsePAM\xc2\xa0yes

\n
cp -p /etc/ssh/sshd_config{,.$(date +\'%Y-%m-%d\')}\nvi /etc/ssh/sshd_config    # or nano, or any preferred editor\n\nChallengeResponseAuthentication yes\nKbdInteractiveAuthentication yes\n
Run Code Online (Sandbox Code Playgroud)\n

并重新启动sshd

\n
systemctl restart sshd\n
Run Code Online (Sandbox Code Playgroud)\n

现在以普通用户身份登录并设置身份验证器。

\n
google-authenticator\n\n   Do you want authentication tokens to be time-based (y/n) y\n   Warning: pasting the following URL into your browser exposes the OTP secret to Google:\n     https://www.google.com/chart?...secret...stuff...\n
Run Code Online (Sandbox Code Playgroud)\n

您将在文本终端中或单击生成的网页链接时显示一个二维码。像往常一样在身份验证器应用程序中捕捉它。

\n
   Your new secret key is: 4JD3xxxxxxxxxxxxxxxxxxH7EE\n   Enter code from app (-1 to skip): xxxxxx\n   Code confirmed\n   Your emergency scratch codes are:\n      \xe2\x80\xa6\n\n   Do you want me to update your "/home/{user}/.google_authenticator" file? (y/n) y\n   Do you want to disallow multiple uses of the same authentication token? (y/n) y\n   By default [\xe2\x80\xa6] This will permit for a time skew of up to 4 minutes\n   between client and server. Do you want to do so? (y/n) n\n   Do you want to enable rate-limiting? (y/n) y\n
Run Code Online (Sandbox Code Playgroud)\n

现在测试一下。如果它有效,那就太好了。如果没有,则恢复/etc/pam.d/common-auth并重/etc/ssh/sshd_config试。

\n

请注意,因为我已nullok在 PAM 配置条目中包含该选项,所以用户可以选择避免设置身份验证。如果删除它,它将成为必需的值。请务必小心删除此选项,因为它将对所有用户帐户(包括root. 您可以阅读文档中的其他选项(请参阅man pam_google_authenticatorman google-authenticator)。

\n