如何设置 google-authenticator 并设置特定的匹配规则以允许不同的登录规则?

如何设置 google-authenticator 并设置特定的匹配规则以允许不同的登录规则?

我正在尝试设置 Google-Authenticator(Google 双因素身份验证)。

相关文件为:

[root@srv01 ~]# cat /etc/pam.d/sshd
#%PAM-1.0
auth      required    pam_google_authenticator.so
auth      required    pam_sepermit.so
auth      include     password-auth
account   required    pam_nologin.so
account   include     password-auth
password  include     password-auth
# pam_selinux.so close should be the first session rule
session   required    pam_selinux.so close
session   required    pam_loginuid.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session   required    pam_selinux.so open env_params
session   required    pam_namespace.so
session   optional    pam_keyinit.so force revoke
session   include     password-auth


[root@srv01 ~]# egrep -v '^#' /etc/ssh/sshd_config | sed '/^$/d'
Protocol 2
SyslogFacility AUTHPRIV
PermitRootLogin no
PasswordAuthentication no
ChallengeResponseAuthentication yes
GSSAPIAuthentication yes
GSSAPICleanupCredentials yes
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
X11Forwarding yes
Subsystem       sftp    /usr/libexec/openssh/sftp-server
UsePAM yes
Match Address 10.13.0.*
  PermitRootLogin yes
  PasswordAuthentication yes

按照互联网上的指南,为了启用 Google-2fa,您需要编辑/etc/pam.d/sshd并添加此行:

auth      required    pam_google_authenticator.so

然后您需要编辑/etc/ssh/sshd_config并更改这些行如下:

PasswordAuthentication no
ChallengeResponseAuthentication yes

在我的案例中,Google 2FA 可以正常工作,并允许已配置 google-authenticator 的用户通过提供 OTP 和密码进行登录,但当我尝试从同一网络中的计算机连接到计算机上的 root 用户时,我的密码被拒绝(即使它是正确的密码)。当我尝试连接到 root@machine 时,问题如下:

Using username "root".
Using keyboard-interactive authentication.
Password:
Access denied
Using keyboard-interactive authentication.
Password:

并且/var/log/secure

 sshd(pam_google_authenticator)[10990]: Failed to read "/root/.google_authenticator"

我从未在 root 用户上运行过 google_authenticator,所以我不知道它为什么要寻找它。

我想要实现的目标如下:

  1. 我希望“PermitRootLogin”在全局范围内设置为“no”(从外部世界连接到服务器时),但如果远程机器 IP 匹配指定本地网络的规则,则将其设置为“yes”(如在配置文件中所示)。

  2. 我希望配置了 google-2fa 的用户仍然能够通过提供 OTP 和密码登录。

可能是线路/etc/pam.d/sshd放错了地方,但我不确定应该把它放在哪里。

有谁知道如何使其按照这些规则发挥作用吗?

答案1

你错过了一个小细节手册

努洛克

如果用户尚未设置 OTP,则允许用户无需 OTP 登录。

你的pam.d/sshd文件应该包含以下内容:

auth      required    pam_google_authenticator.so    nullok

如果您不使用nullok,所有未设置 google 身份验证的用户将被锁定。使用nullok,他们仍然可以在没有 2FA 的情况下登录,直到他们配置它为止。

相关内容