SSH 2FA 适用于除 root 用户之外的所有人森托斯7

SSH 2FA 适用于除 root 用户之外的所有人森托斯7

我目前正在尝试实现 root 用户在从特定主机登录时仅使用密码登录,但无需 2FA 身份验证。到目前为止我的 sshd_config 看起来像这样:

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#Authentication
AllowUsers myuser
ChallengeResponseAuthentication yes
AuthenticationMethods keyboard-interactive
#PermitRootLogin no
PasswordAuthentication no
PermitEmptyPasswords no
UsePAM yes
MaxAuthTries 3


KexAlgorithms [email protected],ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group-exchange-sha256
Ciphers [email protected],[email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr
MACs [email protected],[email protected],[email protected],hmac-sha2-512,hmac-sha2-256,[email protected]


LogLevel VERBOSE


UseDNS yes
IgnoreRhosts yes
HostbasedAuthentication no
X11Forwarding no
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
#Banner /etc/ssh/banner

#SFTP
#Subsystem sftp  /usr/lib/ssh/sftp-server -f AUTHPRIV -l INFO

Match Address 100.100.100.100
  PermitRootLogin yes

然而,这不起作用,一遍又一遍地提示输入密码。因此,经过一些研究,我调整了我的 /etc/pam.d/sshd (我创建了一个组 noauth 并向其中添加了 root 用户)

#%PAM-1.0
auth       required     pam_sepermit.so
auth       substack     password-auth
auth       include      postlogin
# Used with polkit to reauthorize users in remote sessions
-auth      optional     pam_reauthorize.so prepare
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
session    include      postlogin
# Used with polkit to reauthorize users in remote sessions
-session   optional     pam_reauthorize.so prepare
auth [success=done default=ignore] pam_succeed_if.so user ingroup noauth
auth required pam_google_authenticator.so echo_verification_code

但遗憾的是,什么都没有改变,我仍然无法以 root 用户身份登录。我无法将 nullok 添加到 google pam 模块,因为其他人都必须被迫使用 2FA 身份验证

非常感谢帮助:)

答案1

我重新解决了我的问题(有点):我向 pam 模块添加了 nullok,还向 /etc/profile 添加了一个简短的脚本(因此它适用于全局),强制每个用户都设置 2FA(根用户除外)用户)。如果有人感兴趣,这是脚本:

if [ ! -f "$HOME"/.google_authenticator ]; then
    if [ ! "$USER" = "root" ]; then
        google-authenticator -t -d -f -r 3 -R 30 -W
        echo "Please scan the above QR-Code with your 2FA App, or copy the given link. Otherwise, you can no longer log in via SSH"
    fi
fi

相关内容