【问题标题】:SSH is looking in the wrong place for the public/private key pair on WindowsSSH 在 Windows 上为公钥/私钥对寻找错误的位置
【发布时间】:2011-02-19 22:00:50
【问题描述】:

我正在尝试在我的 Windows XP 机器上配置 GIT,但 SSH 不断在无意义的地方创建和查找公钥/私钥对,例如/.ssh/id_rsa

Windows 的 GIT 安装中是否有一个配置文件,我可以将其切换到我的主目录或其他用户定义的位置?默认情况下,它提供在 //.ssh/id_rsa 创建新的密钥对,但这给了我错误“无法创建目录 '//.ssh'”。当我能够找到一个可以在其中创建它的目录时,GIT 不会在那里查找。

【问题讨论】:

  • “无法创建目录”错误可以通过运行sudo以管理员身份重新启动你的shell来解决。我意识到这并不能解决您问题的根源,但认为值得一提,因为有这个问题的人可能会在这里结束
  • Windows 机器。你期望那里有什么 SUDO?

标签: git github ssh-keys


【解决方案1】:

想出了如何在 Windows XP 中手动设置我的主目录:

  1. 右键单击“我的电脑”
  2. 选择“高级”标签
  3. 点击“环境变量”按钮
  4. 在“系统变量”部分下,点击“新建”
  5. 在“变量名”中输入“home”。
  6. 在“变量值”中输入您个人资料的路径。

通过命令提示符执行相同操作:

setx HOME "your\path\to\home"

(请注意后缀 x - 用于持久化更改)

【讨论】:

  • 作为一个提示,老实说,我不记得这个选项在 XP 中是否可用,但至少在较新版本的 Windows 中,您可以将其添加为用户变量而不是系统变量。显然,用户的主目录不是系统范围的东西。
  • 这解决了我在 Windows 10 中的问题。我正在运行 Cygwin git,但它没有拉 ssh 密钥。
  • 这仍然失败。$ pwd /cygdrive/c/Users/myuser debug1: 身份文件 /home/myuser/.ssh/id_rsa type 1
【解决方案2】:

在 Windows 7 上,只需将您的私钥 (id_rsa) 放在 C:\Users\.ssh 文件夹中 如果 .ssh 文件夹不存在,只需创建它。 下载适用于 Windows 的 GIT,cd 到您的项目目录并执行 git clone ssh://@/.git

【讨论】:

  • 问题实际上与 HOME 未设置有关,正如接受的答案指出的那样
【解决方案3】:

我在 Windows 7 上遇到了类似的问题。HOME 环境变量似乎设置正确:

$ echo $HOME
C:\Users\craibuc

但是,尝试连接测试 github 的ssh 访问失败:

$ ssh -Tv git@github.com
OpenSSH_6.6.1, OpenSSL 1.0.1i 6 Aug 2014
debug1: Connecting to github.com [192.30.252.129] port 22.
debug1: Connection established.
Could not create directory 'C/.ssh'.
debug1: identity file C/.ssh/id_rsa type -1
debug1: identity file C/.ssh/id_rsa-cert type -1
debug1: identity file C/.ssh/id_dsa type -1
debug1: identity file C/.ssh/id_dsa-cert type -1
debug1: identity file C/.ssh/id_ecdsa type -1
debug1: identity file C/.ssh/id_ecdsa-cert type -1
debug1: identity file C/.ssh/id_ed25519 type -1
debug1: identity file C/.ssh/id_ed25519-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 libssh-0.6.0
debug1: no match: libssh-0.6.0
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-sha1 none
debug1: kex: client->server aes128-ctr hmac-sha1 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: RSA 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48
The authenticity of host 'github.com (192.30.252.129)' can't be established.
RSA key fingerprint is xx:xx:xx.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (C/.ssh/known_hosts).
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
debug1: Next authentication method: publickey
debug1: Trying private key: C/.ssh/id_rsa
debug1: Trying private key: C/.ssh/id_dsa
debug1: Trying private key: C/.ssh/id_ecdsa
debug1: Trying private key: C/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).

由于某种原因,ssh 试图访问 C/.ssh

当我使用 setx HOME /c/users/craibuc 设置变量并重新启动 shell 时,我能够连接:

$ ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
Hi craibuc! You've successfully authenticated, but GitHub does not provide shell access.

【讨论】:

    【解决方案4】:

    在使用域策略将用户配置文件设置为网络驱动器(在本例中映射到Z:)的机器时,我遇到了同样的问题。

    当尝试从 Powershell 执行任何操作时,我会收到这样的消息。设置 home PATH 变量没有帮助。

    git clone ssh://myusername@mydomain.com:1234/myproject
    Cloning into 'myproject'...
    Could not create directory '/z/.ssh'.
    The authenticity of host '[mydomain.com]:1234 ([1.1.1.1]:1234)' can't be established.
    RSA key fingerprint is 00:11:22:33:$4:55:66:77:88.
    Are you sure you want to continue connecting (yes/no)? yes
    Failed to add the host to the list of known hosts (/z/.ssh/known_hosts).
    Permission denied (publickey).
    
    Please make sure you have the correct access rights
    and the repository exists.
    

    当我从 git bash 克隆时它很好,因为 /z/.ssh 等是从 shell 指向 Z:\.ssh 的有效方式。

    【讨论】:

    • 您是否知道您为解决问题做了什么?
    • ...cloned from git bash
    • 我有同样的问题,但 cmd 和 git bash 都不起作用
    【解决方案5】:

    MSYS2 中的Recommended 方法是将/etc/nsswitch.conf 中的db_home 设置为您的主目录。

    感谢@VarunAgwpointing 在正确的方向。

    Git-for-Windows 在db_home 中包含env,它应该读取您的HOME bash 变量,但是它是unreliable

    【讨论】:

    • 我发现有些软件比如Orcad会设置%HOME%=C:\Users\[accountname]\AppData\Roaming\SPB_16.6或者C:\SPB_data但是由于某种原因我不想改了。那么这种修改/etc/nsswitch.conf的方法真的很有帮助。设置为/etc/nsswitch.conf中的以下行确实使ssh-keygen将默认位置设置为%USERPROFILE%db_home: /%H
    • 现在是 2019 年,OrCAD 仍然会导致此问题。我今天刚刚帮助了一位同事!
    【解决方案6】:

    检查which ssh 以查看您是否使用/usr/bin/ssh 或其他可能意外安装的东西。

    在我的情况下,我的 ssh 在安装 rsync 后被 Chocolatey 取代。运行choco uninstall rsync 后,我的 SSH 又开始工作了。

    【讨论】:

      【解决方案7】:

      ln -s /c/Users/rofrol/.ssh /home/rofrol/ 为我工作。

      这没有:

      $ echo $HOME
      /c/Users/rofrol
      
      $ cat ~/nsswitch.conf
      db_home: /c/Users/rofrol
      
      $ cat /home/rofrol/nsswitch.conf
      db_home: /c/Users/rofrol
      
      $ cat /etc/nsswitch.conf
      # Begin /etc/nsswitch.conf
      
      passwd: files db
      group: files db
      
      db_enum: cache builtin
      
      #db_home: cygwin desc
      db_home: /c/Users/rofrol
      db_shell: cygwin desc
      db_gecos: cygwin desc
      
      # End /etc/nsswitch.conf
      

      可能是因为这个:

      SSH 将需要 /home/$USER

      https://github.com/git-for-windows/git/issues/736#issuecomment-215123009

      【讨论】:

        猜你喜欢
        • 2015-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-01
        • 1970-01-01
        • 2016-09-15
        • 2023-03-24
        • 2017-09-29
        相关资源
        最近更新 更多