【问题标题】:Why does SSH need a passphrase for a passphrase-less key?为什么 SSH 需要一个无密码密钥的密码?
【发布时间】:2025-12-26 23:30:12
【问题描述】:

我有一个不使用密码的 SSH 密钥。我想在构建 Docker 容器时使用该密钥来拉取私有 git 存储库。

我在构建时成功地将密钥放入容器中,但现在 SSH 失败了,因为它无法打开 /dev/tty 来询问密钥的密码。如前所述,钥匙没有。

这是一些 SSH 输出 (-v)

debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: key_load_private_type: incorrect passphrase supplied to decrypt private key
debug1: read_passphrase: can't open /dev/tty: No such device or address
debug1: No more authentication methods to try.
Permission denied (publickey).
fatal: Could not read from remote repository.

【问题讨论】:

  • 请注意,我已经尝试使用in this thread建议的方法添加/dev/tty

标签: git docker ssh


【解决方案1】:

您的密钥文件可能以某种方式损坏。 ssh(至少某些版本)会在无法理解密钥文件的任何时候提示输入密码:

$ dd if=/dev/urandom of=key bs=1500 count=1
1+0 records in
1+0 records out
1500 bytes transferred in 0.000190 secs (7893922 bytes/sec)
$ chmod 600 key
$ ssh -i key foo@localhost
Enter passphrase for key 'key':

【讨论】:

  • 这很可能是问题所在。感谢您的提示。 :)
  • 这确实是问题所在。缺少密钥文件中的换行符。我通过使用另一种方式将密钥插入图像来修复它。
  • 我记不太清了,但我必须在运行映像而不是构建映像时进行克隆。
  • 这也是我的情况:由于 mac os x 上的 textEdit,密钥文件已损坏。我应该记住以后不要将其用作编辑器...无论如何..用Sublime Text打开文件,清空它,再次复制粘贴密钥,它就像一个魅力。谢谢!