为什么我仍然可以使用密码 ssh 进入我的 Ubuntu 机器?这是 /etc/ssh/sshd_config
我的 Ubuntu 20.04 在 ovh 托管上的文件(为简洁起见,仅显示未注释的行):
Include /etc/ssh/sshd_config.d/*.conf
Port xxx
PermitRootLogin no
AllowUsers user1 user2
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM yes
X11Forwarding yes
PrintMotd no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
相关文件的权限看起来没问题:
$ stat -c %a /home/user1/.ssh/
700
$ stat -c %a /home/user1/.ssh/authorized_keys`
600
我已经跑了sudo service ssh restart
并且sudo service sshd restart
.
为什么我仍然可以通过 ssh 使用密码登录我的 Ubuntu 机器?我可以通过 ssh (PuTTY) 通过用户名和密码登录,它只要求输入密码。 user1 和 user2 的密钥都位于 .ssh 主文件夹中。缺什么?
我检查了包含文件:
-rw------- 1 root root 27 Dec 1 12:52 50-cloud-init.conf
...:/etc/ssh/sshd_config.d$ sudo cat 50-cloud-init.conf
PasswordAuthentication yes
所以我想这就是原因?但是,我的配置不会覆盖此设置吗?因为它包含在上面(按行)?
答案1
Include /etc/ssh/sshd_config.d/*.conf
Ubuntu/Debian 发行版在发行版开头有非标准条目sshd_config
。这样做的目的是允许用户在不修改核心文件的情况下自定义其 sshd 配置,这可以最大限度地减少OpenSSHsshd_config
上的冲突或意外配置更改。apt update
因为第一的遇到的配置行是应用的配置行,自定义配置文件中的任何密码命令/etc/ssh/sshd_config.d/*.conf
都将抢占PasswordAuthentication no
主配置中的行。确保所有配置都符合您的预期。
正如 @dexter 所指出的,您可以使用 输出有效配置sudo sshd -T
,当一个配置文件覆盖另一个配置文件时,这可能会突出显示。
答案2
密码登录不仅可以通过PasswordAuthentication
.实际上,它是“专用简单”身份验证方法,并且有一种通用方法可以进行密码身份验证,KbdInteractiveAuthentication
以前称为 ChallengeResponseAuhentication。这是一种类似于聊天的通用身份验证(这就是质询响应所代表的意思),可以通过询问附加或自定义问题来进行双因素、使用一次性密码等,而前者仅允许其名称 -通过系统密码进行身份验证。
UsePAM
在某种程度上控制KbdInteractiveAuthentication
(又名ChallengeResponseAuthentication
)如何替换/复制PasswordAuthentication
. PAM 可以向 sshd 提供质询-响应聊天,并且 PAM通常配置了pam_unix.so
执行传统密码认证的方式,因此UsePAM
+KbdInteractiveAuthentication
有效地为SSH提供了密码认证。 (感谢@TooTea 注意到这一点。)
查看点赞数最高的答案这里(遗憾的是,这不是公认的答案,因为它显然更好),man sshd_config
当然也请参阅 。
答案3
包含文件已PasswordAuthentication yes
被使用,感谢man sshd_config
:“对于每个关键字,将使用第一个获得的值。”
现在如果我尝试通过 SSH 登录,我“没有可用的受支持的身份验证方法(公钥)”。
仅 SSH 密钥有效。
答案4
请记住,使用match
节时顺序很重要。
这个可以工作:
Match User user-special
PasswordAuthentication yes
Match User user-ftp
AuthorizedKeysFile /home/user-ftp/.ssh/authorized_keys
ChrootDirectory /DATA/s2survey
ForceCommand internal-sftp
AllowTcpForwarding no
X11Forwarding no
Match all
PasswordAuthentication no
KbdInteractiveAuthentication no
ChallengeResponseAuthentication no
然而,该命令不会按预期工作。要么PasswordAuthentication
被忽略(无法找出原因),要么在更具体的match
部分中不会更改。
PasswordAuthentication no
KbdInteractiveAuthentication no
ChallengeResponseAuthentication no
Match User user-special
PasswordAuthentication yes
Match User user-ftp
AuthorizedKeysFile /home/user-ftp/.ssh/authorized_keys
ChrootDirectory /DATA/s2survey
ForceCommand internal-sftp
AllowTcpForwarding no
X11Forwarding no
Match all