SSH在Windows上寻找公钥/私钥对的位置有误。

38

我正在尝试在Windows XP机器上配置GIT,但是SSH一直在非常奇怪的地方创建和查找公共/私有密钥对,例如/.ssh/id_rsa。

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


“无法创建目录”错误可以通过以管理员身份运行sudo并重新启动您的shell来解决。我意识到这并没有解决您问题的根本原因,但认为值得提及,因为有人可能会在此处遇到该问题。 - Paul
5
Windows机器。你期望在那里使用SUDO做什么? - Kostadin
7个回答

75

我找到了如何手动设置Windows XP中的主目录:

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

要通过命令提示符执行相同操作,请执行以下操作:

setx HOME "your\path\to\home"

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


4
只是一个提示,我不确定XP系统是否有此选项,但至少在较新的Windows系统中,您可以将其添加为用户变量而不是系统变量。显然,您的用户主目录不是系统范围的东西。 - btleffler
1
这解决了我在Windows 10中的问题。 我正在运行Cygwin git,但它没有拉取ssh密钥。 - Haskell McRavin
这仍然失败了。$ pwd /cygdrive/c/Users/myuser debug1: 身份文件 /home/myuser/.ssh/id_rsa 类型 1 - Jigar Shah

9

推荐的方法是使用MSYS2,在/etc/nsswitch.conf中设置db_home为您的主目录。

@VarunAgw指出了正确的方向,特此鸣谢。

Git-for-Windows将env包含在db_home中,应该读取您的HOME bash变量,但这种方法不可靠


我发现一些软件如Orcad会将 %HOME%=C:\Users\[账户名称]\AppData\Roaming\SPB_16.6C:\SPB_data设置为家目录,但出于某些原因,我不想更改它。然后在 /etc/nsswitch.conf 文件中修改的方法真的有所帮助。像下面这行设置就可以让 ssh-keygen 设置默认位置为 %USERPROFILE%:db_home: /%H - Seagal82
到了2019年,OrCAD仍然存在这个问题。今天我刚帮助一个同事解决了这个问题! - AngCaruso

5

使用which ssh命令检查你是否使用的是/usr/bin/ssh或其他可能由于意外安装而被替换的版本。

在我的情况下,在安装rsync之后,Chocolatey替换了我的ssh。 运行choco uninstall rsync后,我的SSH又能正常工作了。


5
我在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.

0

当我使用一个设置用户配置文件到网络驱动器(在这种情况下映射到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的有效方式。

1
你是否知道你是如何解决这个问题的? - Igor
从 Git Bash 克隆。 - nathanchere
我有同样的问题,但是无论是cmd还是git bash都不起作用。 - Kalpesh Soni

0

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


-3

在Windows 7上,只需将您的私钥(id_rsa)放置在C:\ Users \ .ssh文件夹中。如果.ssh文件夹不存在,请创建它。 下载Windows版GIT,进入您的项目目录并执行以下操作: git clone ssh://@/.git


3
问题实际上与HOME环境变量未设置有关,正如被接受的答案所指出的那样。 - kakoma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接