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

Ita*_*not 6 ssh login pam google-authenticator

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

相关文件是:

[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
Run Code Online (Sandbox Code Playgroud)

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

auth      required    pam_google_authenticator.so
Run Code Online (Sandbox Code Playgroud)

然后您需要/etc/ssh/sshd_config按如下方式编辑和更改这些行:

PasswordAuthentication no
ChallengeResponseAuthentication yes
Run Code Online (Sandbox Code Playgroud)

在我的情况下,Google 2FA 工作并允许配置了 google-authenticator 的用户通过提供 OTP 和密码登录,但是当我尝试从同一网络中的机器连接到机器上的 root 用户时,我的密码被拒绝(即使虽然这是正确的密码)。当我尝试连接到 root@machine 时,问题如下所示:

Using username "root".
Using keyboard-interactive authentication.
Password:
Access denied
Using keyboard-interactive authentication.
Password:
Run Code Online (Sandbox Code Playgroud)

并在/var/log/secure

 sshd(pam_google_authenticator)[10990]: Failed to read "/root/.google_authenticator"
Run Code Online (Sandbox Code Playgroud)

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

我想要实现的目标如下:

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

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

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

任何人都知道如何使其符合这些规则?

Tho*_*mBR 1

您错过了手册中的一个小细节:

努洛克

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

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

auth      required    pam_google_authenticator.so    nullok
Run Code Online (Sandbox Code Playgroud)

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