SSH 到 MacOS 拒绝公钥身份验证

Rik*_*ich 5 ssh openssh ssh-keys macos macos-catalina

当我使用 ssh 从 Linux 桌面计算机连接到 MacOS 笔记本电脑时,我的公钥被拒绝。我可以使用密码进行连接。

我看过几个与此类似的问题(例如5226521335103471753),但没有一个提供我尚未尝试过的适用解决方案。

通常在这一点上我会怀疑我的代理中没有密钥或者文件.ssh/authorized_keys不正确。

从 Mac 笔记本电脑(目标):

$ /bin/ls -ld ~/.ssh{,/authorized_keys}
drwx------  11 rik  staff   352 Oct 30 21:49 /Users/rik/.ssh
-rw-r--r--   1 rik  staff  1981 Oct 30 21:27 /Users/rik/.ssh/authorized_keys
Run Code Online (Sandbox Code Playgroud)

从 Linux 桌面(来源):

$ ssh-add -L
ssh-rsa AAAAB3[...]2akN7r /Users/rik/.ssh/id_rsa
ssh-rsa AAAAB3[...]Ig+DS3 /home/rik/.ssh/id_rsa
Run Code Online (Sandbox Code Playgroud)

我已将其复制authorized_keys到树莓派并能够通过 ssh 访问它。对每台机器的使用ssh -vvv提供了更多线索。

到树莓派:

...
debug1: rekey after 134217728 blocks
debug2: key: /home/rik/.ssh/id_rsa (0x55a9f8044310), agent
debug2: key: /Users/rik/.ssh/id_rsa (0x55a9f8049330), agent
debug2: key: /home/rik/.ssh/id_dsa ((nil))
debug2: key: /home/rik/.ssh/id_ecdsa ((nil))
debug2: key: /home/rik/.ssh/id_ed25519 ((nil))
debug3: send packet: type 5
debug3: receive packet: type 7
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,password
debug3: preferred gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/rik/.ssh/id_rsa
debug3: send_pubkey_test
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 60
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
...
Run Code Online (Sandbox Code Playgroud)

这就是我想看到的(发送 50,接收 60)。以下是连接到 Mac 笔记本电脑时的相同部分:

...
debug1: rekey after 134217728 blocks
debug2: key: /home/rik/.ssh/id_rsa (0x5633631f5310), agent
debug2: key: /Users/rik/.ssh/id_rsa (0x5633631f6950), agent
debug2: key: /home/rik/.ssh/id_dsa ((nil))
debug2: key: /home/rik/.ssh/id_ecdsa ((nil))
debug2: key: /home/rik/.ssh/id_ed25519 ((nil))
debug3: send packet: type 5
debug3: receive packet: type 7
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug3: start over, passed a different list publickey,password,keyboard-interactive
debug3: preferred gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/rik/.ssh/id_rsa
debug3: send_pubkey_test
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Offering RSA public key: /Users/rik/.ssh/id_rsa
debug3: send_pubkey_test
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Trying private key: /home/rik/.ssh/id_dsa
...
Run Code Online (Sandbox Code Playgroud)

这表明服务器愿意接受publickey一般的密钥,但不愿意接受这些特定的密钥。再次,我正在寻找服务器端配置错误。在 MacOS 上,配置位于/private/etc/ssh.

$ grep '^[^#]' /private/etc/ssh/sshd_config
AuthorizedKeysFile  .ssh/authorized_keys
UsePAM yes
AcceptEnv LANG LC_*
Subsystem   sftp    /usr/libexec/sftp-server
Run Code Online (Sandbox Code Playgroud)

我不认为这有什么问题,但也许问题就在那里。

接下来我查看控制台输出,看看是否有东西出现。在控制台应用程序中,我每次ssh尝试都会得到以下 6 行信息:

error   22:25:56.376189-0400    sshd    send failed: Invalid argument
error   22:25:56.376624-0400    sshd    send failed: Invalid argument
error   22:25:56.376676-0400    sshd    send failed: Invalid argument
error   22:25:56.400722-0400    kernel  Sandbox: sshd(1215) deny(1) mach-lookup com.apple.logd
error   22:25:56.400952-0400    kernel  Sandbox: sshd(1215) deny(1) mach-lookup com.apple.diagnosticd
error   22:25:56.689723-0400    kernel  Sandbox: com.apple.WebKit(37255) deny(1) mach-lookup com.apple.CoreDisplay.Notification
Run Code Online (Sandbox Code Playgroud)

看到Sandbox这一点并注意到旧版本的一行,sshd_config我将其复制进去并重新启动 ssh。

该行:

UsePrivilegeSeparation sandbox      # Default for new installations.
Run Code Online (Sandbox Code Playgroud)

重新启动 ssh:

# launchctl stop com.openssh.sshd
# launchctl start com.openssh.sshd
Run Code Online (Sandbox Code Playgroud)

这没有效果,所以我恢复了配置更改。与此相关的搜索将我带到了这里。我已确认已选中“设置|安全和隐私|隐私|全磁盘访问|sshd-keygen-wrapper”。

我最近在笔记本电脑上升级到 Catalina。我已经有一段时间没有使用 ssh 连接到它了,但我知道它过去一直在工作。

我认为 Catalina 可能对其接受的公钥类型变得更加严格。因此,我在另一个文件中生成了一个新密钥,将公钥添加到我的authorized_keys中,将密钥添加到我的代理中,验证代理是否拥有新密钥,然后重试。如果原因是对密钥类型有更大的限制,那么他们就会忘记升级ssh-keygen默认值以匹配。(但我并不是说苹果不会做类似的事情。)

我不知道如何让publickey身份验证再次工作。欢迎大家提出意见。

Rik*_*ich 5

感谢斯皮夫带领我走向正确的方向。问题是我的主目录的权限,而不是.ssh子目录。

sshd通过取消选中首选项|共享|远程登录来暂时禁用。然后我跑去/usr/sbin/sshd -d获取详细输出。它包括这一行:

Authentication refused: bad ownership or modes for directory /Users/rik
Run Code Online (Sandbox Code Playgroud)

所以我将权限更改为755,现在它可以工作了。

再次感谢斯皮夫。


小智 0

典型的 Mac 故障排除技术...您是否启用和禁用了远程管理?我有一个类似的问题,无法使用 VNC 解决(它与 SSH 隧道松散关联)。VNC 密码不允许连接,但使用用户名和密码可以。

我还想知道这是否是一些安全限制。