【问题标题】:Git on Windows not asking for SSH key password, or using SSH KeyWindows 上的 Git 不要求输入 SSH 密钥密码或使用 SSH 密钥
【发布时间】:2020-01-26 04:52:43
【问题描述】:

根本问题是我从来没有看到提示输入我的 SSH-Key 密码、SSH-Agent 或否。因此,它就像我输入了错误的密码并默认假装我没有钥匙。

这发生在多台计算机上。不久前我设置了我的 ssh 密钥,一切都很好,但每隔一段时间我会执行一次 git push(通常在重新启动计算机后),我会被要求输入我的 git 原始服务器的密码,而不是我的 ssh 密钥的密码。由于我的源服务器没有密码,这让我无法推送更改。

有时它会在重新启动后自行解决,有时则不会。通常我会做很多猜测,其中一个最终解决了问题,但我目前不记得哪些有效,哪些无效。

解决此问题的正确方法是什么?实际问题是什么?我的 SSH 密钥是否被锁定?某些 Windows 进程是否无法正确启动?路径变量被吃掉了吗?对windows了解不多(一般都是在linux上开发的),所以在这里比较茫然。

编辑:第一个答案提到了 ssh-agent。一些谷歌搜索让我到了这里:

https://help.github.com/articles/working-with-ssh-key-passphrases/#platform-windows

其中解释了如何设置 ssh-agent 以自动启动并了解您在 Windows 中的密钥。 (我什至不知道您可以在 Windows 中使用 bash 配置文件)。

这没有帮助。

我的 git bash 现在说“添加了身份:/c/Users/{{ME}}/.ssh/id_rsa”它不会询问我的密钥密码,我仍然无法推送到我的源服务器(它询问对于服务器密码,仍然)。

我可以确认我的 ssh 密钥确实存在于代理正在查找的位置。我还可以确认已添加密钥:ssh-add -l 从我的密钥存储位置显示单个密钥。

编辑:将 GIT_SSH 设置为指向 ssh 的可执行文件的环境变量也无济于事,但无论如何它是一个很长的尝试。

编辑: ssh git@git.myhost.com 输出:

$ ssh -v git@git.myhost.lan
OpenSSH_6.6.1, OpenSSL 1.0.1i 6 Aug 2014
debug1: Reading configuration data /c/Users/eschjen/.ssh/config
debug1: /c/Users/eschjen/.ssh/config line 1: Applying options for git.myhost
.lan
debug1: Connecting to git.myhost.lan [10.116.22.40] port 22.
debug1: Connection established.
debug1: identity file /c/Users/eschjen/.ssh/id_rsa type 1
debug1: identity file /c/Users/eschjen/.ssh/id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5* compat 0x0c000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<3072<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA ae:81:77:0d:1c:8e:6a:aa:a8:69:36:1b:e4:ca:33:ee
debug1: Host 'git.myhost.lan' is known and matches the RSA host key.
debug1: Found key in /c/Users/eschjen/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mi
c,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /c/Users/eschjen/.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mi
c,password
debug1: Next authentication method: password
git@git.myhost.lan's password:

输出屏幕帮助我确定 ssh 密钥正在被使用,但被服务器拒绝。我发现我的服务器知道的 ssh 密钥与我的机器所拥有的 ssh 密钥不匹配,尽管不到一个月前一切正常。我重新添加了我拥有的 ssh 密钥,并且能够推送我的更改。

有人知道在所有这些过程中如何重新生成 ssh 密钥(我很确定我没有明确地这样做)吗?

【问题讨论】:

  • 请提供 ssh 的输出,并为失败的连接尝试设置 -v 标志。
  • 完成。我无法确定原木的正面或反面。看起来它提供了我的 RSA 公钥……然后就忘记了它并直接跳到密码验证。没有错误,没有问题。如果“服务器不允许漫游”很重要,为什么我在 Linux 中没有问题,有时在我的 Windows 机器上也没有问题? ...也许是因为我在 wifi 上?
  • 不,有线IP地址并不比wifi IP地址好。
  • ...好吧,输出为我做的一件事是让我确认我的密钥实际上在服务器中。不知何故,它不是。我不记得重新生成了我的 windows 密钥,我向你保证我已经花了相当长的时间在我的 windows 机器上成功使用 git。我在我的 git 服务器上看到一个密钥,我相当确定它对应于我的 Windows 机器,它不是我当前的密钥。有什么线索吗?
  • 感谢您的接受。我建议将后续问题移至另一个问题,因为它具有完全不同的要求。

标签: windows


【解决方案1】:

添加身份是不够的。在您的C:\Users\{{username}}\.ssh 目录中应该有一个名为config(无扩展名)的文件。

您可以像这样定义用于给定主机的密钥:

Host myhost.name.com
 IdentityFile ~/.ssh/my_keyfile_name

正确配置的结果是输入密码请求:

Enter passphrase for key '/c/Users/{{username}}/.ssh/my_keyfile_name':

Edit1:您可以通过添加 -v 参数来检索 ssh 的详细输出。真实世界示例(主机名已替换):

λ ssh -v igor@myhost.at
OpenSSH_6.6.1, OpenSSL 1.0.1m 19 Mar 2015
debug1: Reading configuration data /c/Users/Igor/.ssh/config
debug1: /c/Users/Igor/.ssh/config line 4: Applying options for myhost.at
debug1: Connecting to myhost.at [192.168.2.1] port 22.
debug1: Connection established.
debug1: identity file /c/Users/Igor/.ssh/myhost-server type -1
debug1: identity file /c/Users/Igor/.ssh/myhost-server-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.5p1 Debian-6+squeeze5
debug1: match: OpenSSH_5.5p1 Debian-6+squeeze5 pat OpenSSH_5* compat 0x0c000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<3072<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA 67:3f:96:7b:6a:68:55:89:a8:30:a9:ed:67:ef:40:a4
debug1: Host 'myhost.at' is known and matches the RSA host key.
debug1: Found key in /c/Users/Igor/.ssh/known_hosts:3
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /c/Users/Igor/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /c/Users/Igor/.ssh/myhost-server
debug1: key_parse_private2: missing begin marker
debug1: key_parse_private_pem: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
Enter passphrase for key '/c/Users/Igor/.ssh/myhost-server':

我认为这会有很大帮助。向请求此信息的 OP 添加了评论。

【讨论】:

  • 什么都没有发生。不过,我不完全确定指定主机会有什么帮助,因为 ssh-add 应该询问我的密钥密码并且它没有任何主机参数。 Git push 似乎仍然没有使用我的密钥,也没有要求输入密码,只是直接询问服务器的密码。
  • 您使用哪些工具来执行连接?我使用与 git (git-scm.com) 捆绑在一起的 linux 工具,其中包括 ssh 二进制文件。我不需要手动为我的启动设置任何关键添加措施。
【解决方案2】:

您需要告诉您的计算机在重新启动系统后再次添加密钥。大多数情况下,这是通过ssh-agent

完成的

【讨论】:

  • 这似乎不是我的问题。每次我想使用我的密钥时都要求我输入密码,我并不感到不安。相反,我没有被要求输入密码,而是 git 跳到要求输入服务器密码。使用 ssh-agent 对此没有帮助。
  • 嗯,但您仍然必须告诉您的系统使用哪个键。那你在哪里做,当不使用 ssh-agent 或手动添加密钥时
  • 我不确定我是否解析了您的问题。如果我只有一个键,为什么我必须指定使用哪个键?如果这必须在任何情况下发生,答案是我没有这样做(因此这可能是我的问题)。我会做更多的研究。我会说使用 ssh-agent 没有帮助,如果这是指定哪个方法的话。
  • 您说有时当您重新启动它时它会起作用,因此您系统上的某些东西似乎打开了钥匙。也许 github for Windows 或 putty 或类似的?
  • Ssh-agent 是一种自动添加密钥的方法。即使你只有一把钥匙,你的系统仍然需要知道它是什么等等。
【解决方案3】:

我浪费了几个小时试图解决同样的问题 - 即使选美比赛没有运行,SSH 也不会要求我的密钥对的密码。

SSH 使用不同的密钥格式(SSH-1 和 SSH-2),更重要的是,如果 RSA 密钥是 SSH-2 格式,则拒绝读取它们。使用 Puttygen 生成密钥对,然后将其转换为 SSH-1 格式以保存在 .ssh\id_rsa 中解决了我的问题。

  1. 生成密钥对
  2. 用有意义的名称保存它(如 Basement-Computer.ppk)
  3. 将文件导出为 OpenSSH 格式并以 id_rsa 名称保存(无扩展名)

Git 克隆现在会要求输入密码(或者至少对我来说是这样)。 Google 关于设置 Git 存储库 https://cloud.google.com/source-repositories/docs/authentication#ssh 的说明忽略了关键的第三步。

【讨论】:

    最近更新 更多