Rocky10 登录强制双因子认证模式

cat /etc/redhat-release

# Rocky Linux release 10.1 (Red Quartz)

cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

vim /etc/ssh/sshd_config
# 基础
Port 22
Protocol 2
ListenAddress 0.0.0.0
# 全局安全策略
PermitRootLogin no
UsePAM yes
PubkeyAuthentication yes

# 默认禁止纯密码登录
PasswordAuthentication no

# 为 tom 的第二因子开启交互式认证
KbdInteractiveAuthentication yes
ChallengeResponseAuthentication yes

# tom:强制双因子
Match User tom
AuthenticationMethods publickey,password

# git:仅允许密钥
Match User git
AuthenticationMethods publickey
PasswordAuthentication no
KbdInteractiveAuthentication no

# 安全加固
MaxAuthTries 3
LoginGraceTime 30
PermitEmptyPasswords no
X11Forwarding no
AllowTcpForwarding no
AllowAgentForwarding no

# 日志
LogLevel VERBOSE

Subsystem sftp /usr/libexec/openssh/sftp-server

tom 用户
passwd tom
mkdir -p /home/tom/.ssh
chmod 700 /home/tom/.ssh
chown tom:tom /home/tom/.ssh

/home/tom/.ssh/authorized_keys
chmod 600 /home/tom/.ssh/authorized_keys
chown tom:tom /home/tom/.ssh/authorized_keys

git 用户(通常用于仓库访问)
mkdir -p /home/git/.ssh
chmod 700 /home/git/.ssh
chown git:git /home/git/.ssh

chmod 600 /home/git/.ssh/authorized_keys
chown git:git /home/git/.ssh/authorized_keys
git 不需要设置密码,也无法用密码登录。

tom:
sshd -t && systemctl restart sshd
ssh -i tom.key tom@server
# → 提示输入 tom 的 Linux 密码

git:
ssh -i git.key git@server
# → 直接登录,不会询问密码

root:
ssh root@server
# → 拒绝

常见问题排查
journalctl -u sshd -f

如果 tom 不提示密码,通常是:
UsePAM 未启用
KbdInteractiveAuthentication 被禁用
PasswordAuthentication 被错误覆盖

如果提示 AuthenticationMethods 不生效
RHEL10 / OpenSSH 在某些版本中可能默认禁用 challenge-response,需要加一行:
PubkeyAuthentication yes
PasswordAuthentication yes
KbdInteractiveAuthentication yes
AuthenticationMethods publickey,password

posted @ 2025-12-23 14:23  vicowong  阅读(1)  评论(0)    收藏  举报