SSH 到 MacOS 拒绝公钥认证

SSH 到 MacOS 拒绝公钥认证

当我从台式 Linux 计算机使用 ssh 连接到我的 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

从 Linux 桌面(来源):

$ ssh-add -L
ssh-rsa AAAAB3[...]2akN7r /Users/rik/.ssh/id_rsa
ssh-rsa AAAAB3[...]Ig+DS3 /home/rik/.ssh/id_rsa

我已经将我的复制authorized_keys到 raspberry pi 上,并且能够通过 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
...

这就是我想要看到的(发送 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
...

这表明服务器愿意接受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

我不觉得这有什么不对,但问题可能就在这里。

接下来我查看了控制台输出,看看是否有东西出现。在控制台应用程序中,每次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

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

以下行:

UsePrivilegeSeparation sandbox      # Default for new installations.

重新启动 ssh:

# launchctl stop com.openssh.sshd
# launchctl start com.openssh.sshd

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

我最近将笔记本电脑升级到了 Catalina。我已经很久没有使用 ssh 连接它了,但我知道它过去一直正常工作。

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

我不知道如何让publickey身份验证重新工作。欢迎提出任何建议。

答案1

感谢 Spiff 为我指明了正确的方向。问题出在我的主目录的权限上,而不是.ssh子目录。

sshd通过取消选中 Prefrences|Sharing|Remote Login 暂时禁用了此功能。然后我运行/usr/sbin/sshd -d以获取详细输出。它包括以下行:

Authentication refused: bad ownership or modes for directory /Users/rik

因此我将权限改为755,现在它可以正常工作了。

再次感谢 Spiff。

答案2

我看到了同样的错误消息,并尝试了权限角度,但没有效果。问题最终是因为我的 .ssh 是一个符号链接。我删除了符号链接,将 .ssh 文件夹移回 ~,然后重新启动 sshd 以使其再次运行。

故障排除步骤如下:

  1. 卸载 sshdsudo launchctl unload /System/Library/LaunchDaemons/ssh.plist

  2. 从命令行启动 sshdsudo /usr/sbin/sshd -d

  3. 尝试连接ssh thehost

  4. 观察守护进程的输出:

    debug1: sshd version OpenSSH_7.9, LibreSSL 2.7.3
    ...
    Authentication refused: bad ownership or modes for directory /Wherever
    ...
    Failed publickey for user from ipaddr port xxxxx ssh2: RSA SHA256:some
    ...
    
  5. rm .ssh && mv 无论/.ssh ~/.ssh

  6. 停止守护进程CTRL-C

  7. 重新启动守护进程sudo /usr/sbin/sshd -d

  8. ETC。

  9. 当一切恢复正常后,重新加载 sshdsudo launchctl load /System/Library/LaunchDaemons/ssh.plist

答案3

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

我还想知道这是否是某种安全限制。

答案4

我使用的是 Windows,所以也许有人会发现这很有用,因为我在尝试从我的 macbook ssh 进入 Windows 计算机时遇到了非常类似的问题。ssh-copy-id 在这种情况下不起作用,我已经尝试了所有的权限检查,并四次检查了公钥/私钥是否在它们应该在的位置。

sshd 调试提示很有用,我遇到了一个问题,当在调试模式下使用以下 powershell 命令时,公钥身份验证正在工作

Stop-service sshd
C:\Windows\System32\OpenSSH\sshd.exe -d

**在调试模式下,它将仅允许一个连接,然后在客户端断开连接后停止

但作为服务运行时无法工作。这让我检查了 Windows 服务,结果发现“OpenSSH 身份验证代理”服务被禁用了。我将其更改为自动并启动该服务,问题就解决了。我不知道什么时候、为什么或谁认为禁用它是个好主意,但我想这 4 个小时最终取得了胜利 =_=。

相关内容