如何在 Mac OS X 上设置 sshd 以仅允许基于密钥的身份验证?

如何在 Mac OS X 上设置 sshd 以仅允许基于密钥的身份验证?

我有一台 Mac OS X 机器(运行 10.5 的 Mac mini),启用了远程登录。我想打开 sshd 端口到互联网,以便能够远程登录。

出于安全原因,我想禁用使用密码的远程登录,只允许具有有效公钥的用户登录。

在 Mac OS X 中设置此功能的最佳方法是什么?

答案1

经过一些反复试验,我自己找到了答案。需要在以下位置设置这些选项/etc/sshd_config

PasswordAuthentication no
ChallengeResponseAuthentication no

只改变其中一个是不够的。

答案2

在 /etc/ssh/sshd_config 中

# To disable tunneled clear text passwords, change to no here! Also,
# remember to set the UsePAM setting to 'no'.
#PasswordAuthentication yes
#PermitEmptyPasswords no

将 PasswordAuthentication 设置为 no,并删除其前面的 #。

答案3

实际上,您可以在 /etc/sshd_config 中设置以下行:

密码验证否

如果您使用的是库存安装(即,您没有自己从源代码构建/安装它),launchd 应该负责获取新配置,而无需重新启动守护进程。

答案4

其他帖子已经介绍了如何仅允许密码身份验证。如果您在使问题的基于密钥的身份验证方面工作时遇到困难,请参阅此答案,它处理可能导致公钥身份验证不起作用的文件权限问题。

如果你仍然无法让它与这里修改的所有注释一起工作/etc/ssh/sshd_config,请查看man sshdOSX 上 FILES 部分下的这一行:

~/.ssh/authorized_keys
             Lists the public keys (DSA, ECDSA, Ed25519, RSA) that can be used for logging in as this user.  The format of this file is described above.
             The content of the file is not highly sensitive, but the recommended permissions are read/write for the user, and not accessible by others.

             **If this file, the ~/.ssh directory, or the user's home directory are writable by other users, then the file could be modified or replaced by
             unauthorized users.  In this case, sshd will not allow it to be used unless the StrictModes option has been set to ``no''.**

因此您可以设置/etc/ssh/sshd_config

StrictModes no

(我没有那样测试),或者确保文档中上面提到的文件的权限是正确的:

chmod 0600 ~/.ssh/authorized_keys
chmod 0700 ~/.ssh

设置 的权限~更为复杂,因为它可能具有特殊属性,如 setuid 位等。首先,使用 stat 找到 的实际、绝对、八进制权限~

stat -f %Op ~

osx 版本和 GNU 版本中 stat 的语法似乎有所不同。对我来说,最初的输出如下:

40777

最后两个 7 表示它rwx适用于组和其他人;我们需要它们为 5,以删除组和其他人写的权限,如文档中所述:只有用户可以对 有写权限~。因此,执行:

chmod 40755 ~

笔记:只改变最后两个数字;如果您更改了其他任何内容,则您将更改您的权限(第三个八进制数)或特殊文件属性(最后三个八进制数之前的任何内容)。

完成此操作后,公钥认证最后有效。值得。

相关内容