适用于Windows(Git)的.ssh/config文件

75

我一直在寻找如何使用多个ssh密钥的解决方案,发现可以通过在.ssh目录下创建配置文件来实现,但在 Windows 上无法工作。

我的问题是,我正在使用私钥访问Git服务器,格式为ssh:// git@example.com / directory,当我在TortoiseGit中使用时,它能正常工作,因为有可能选择私钥。

但我想在我的IntelliJ IDEA中使用git库,只有使用git本机shell的选项,并且如果我将名为id_rsa的密钥放入.ssh文件夹中,它也会工作。现在我想使用多个ssh密钥(所以我的密钥名称将变为“ id_rsa_test”),那么在Windows下,如何配置.ssh / config文件才能与普通的git服务器一起使用呢?

我找到的大多数示例仅适用于github。


为给定的域名指定一个SSH密钥以进行git push。 - Syscall
6个回答

69

如果您使用"Git for Windows"

>cd c:\Program Files\Git\etc\ssh\

添加到ssh_config中:

AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/id_rsa_test

注意:您需要使用SSH版本不低于7.2(发布日期为2016年02月28日)。


41

在Windows 10上,您可以使用多个ssh密钥并指定允许的访问类型。

假设您已经创建了ssh安全密钥,并将它们存储在C:\ Users \ [User] \ .ssh中。

  1. Open the folder C:\Users\[User]\.ssh

  2. Create the file config (no file extension)

  3. Open the file in a text editor like Notepad, and add these configuration details for the first remote host and user. Keep both CMD and BASH paths or only pick one format. Then copy-and-paste below it for the other host/user combinations and amend as required. Save the file.

    Host [git.domain.com]
    User [user]
    Port [number]
    IdentitiesOnly=yes
    PreferredAuthentications publickey
    PasswordAuthentication no
    # CMD
    IdentityFile C:\Users\[User]\.ssh\[name_of_PRIVATE_key_file]
    # BASH
    IdentityFile /c/Users/[User]/.ssh/[name_of_PRIVATE_key_file]
    
  4. Testing

  • Using Bash (Git for Windows)
    $ ssh -T git@[git.domain.com]
    Welcome to GitLab, @[User]!
    
  • Using Commandline (requires activation of Win 10 OpenSSH Client)
    C:\Users\[User]>ssh -T git@[git.domain.com]
    Welcome to GitLab, @[User]!
    
  1. 如果需要进行故障排除,请使用 ssh -Tv git@[git.domain.com] 命令(或者使用 -Tvv-Tvvv 以获得更高的详细信息级别)。

5
这个答案比当前被接受的答案好得多。 - dthor
完美的解决方案,但我想知道...是否有一种方法可以给这个配置取一个别名,而不是每次都要连接 [ ssh -T user@git.domain.com ]?我知道在Linux上可以通过给[ Host ]指定名称和[ Hostname ]实际地址来实现这一点。我尝试在这里做到这一点,但失败了。 - Jorge_Freitas
@Jorge_Freitas 1) 仅用于测试时使用-T! 2) 最常用的是 ssh user@domain.comssh -F path/.ssh/config domain.com. 可以使用配置文件中提到的主机名或主机别名代替domain.com。例如,ssh -F path/.ssh/config test1。3) 在配置文件中,使用 Hostname(主机名或IP地址)或Host(在ssh命令中引用的连接别名,例如test1)或两者都使用;请参阅 https://stackoverflow.com/a/66562287/5703651。 - Sven Haile
@Jorge_Freitas Host的值是别名!如果不是域名或IP地址。所以使用Host myname并添加HostName git.domain.com。然后可以简单地使用ssh myname - Nilpo

20

这些指令在Linux中可以正常工作。但是在Windows中,今天对我来说不起作用。

我找到了一个对我有帮助的答案,也许这会帮助OP。我尝试了很多方法来解决这个问题。你需要使用"ssh-add"将新的非标准命名的密钥文件添加进去!这里是一个神奇的方法:生成新的SSH密钥并将其添加到ssh-agent。一旦你知道了"在Windows中使用ssh-add添加密钥"的关键词,你就会发现很多其他链接。

如果我经常使用Windows,我会找到一些方法使这个过程更加持久。https://github.com/raeesbhatti/ssh-agent-helper

ssh密钥代理会寻找默认的"id_rsa"和其他它知道的密钥。你用非标准名称创建的密钥必须被添加到ssh密钥代理中。

首先,在Git BASH shell中启动密钥代理:

$ eval $(ssh-agent -s)
Agent pid 6276

$ ssh-add ~/.ssh/Paul_Johnson-windowsvm-20180318
Enter passphrase for /c/Users/pauljohn32/.ssh/Paul_Johnson-windowsvm-20180318:
Identity added: /c/Users/pauljohn32/.ssh/Paul_Johnson-windowsvm-20180318 (/c/Users/pauljohn32/.ssh/Paul_Johnson-windowsvm-20180318)

然后我切换到想要克隆repo的目录

$ cd ~/Documents/GIT/

$ git clone git@git.ku.edu:test/spr2018.git
Cloning into 'spr2018'...
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

我花了很长时间才解决了这个问题。

在此期间,我尝试了其他一些方法。

起初,我认为这是由于文件和文件夹权限引起的。在Linux上,如果文件夹未设置为700,则会拒绝.ssh设置。Windows则为711。在Windows中,我找不到任何方法可以使权限为700。

与此作斗争之后,我认为这不可能是问题所在。原因如下。 如果密钥命名为"id_rsa",则git可以正常工作!Git能够连接到服务器。但是,如果我将密钥文件命名为其他名称,并以一致的方式修复配置文件,则无论如何,git都无法连接。这让我觉得权限不是问题所在。

您可以执行ssh命令的详细输出来调试此问题。

在git bash shell中,运行以下命令:

$ ssh -T git@name-of-your-server

注意,这里的用户名应该是“git”。如果您的密钥已设置并且找到了配置文件,则会看到以下内容,就像我在我的Linux系统中测试的一样:
$ ssh -T git@git.ku.edu
Welcome to GitLab, Paul E. Johnson!

另一方面,在Windows中,在应用“ssh-add”之前,我遇到了与您相同的问题。它需要git的密码,但通常会失败。

$ ssh -T git@gitlab.crmda.ku.edu
git@gitlab.crmda.ku.edu's password:

如果我手动将我的密钥复制到"id_rsa"和"id_rsa.pub",那么这个操作就可以正常工作。在运行ssh-add之后,观察Windows Git BASH中的结果。

$ ssh -T git@gitlab.crmda.ku.edu
Welcome to GitLab, Paul E. Johnson!

如果你在这里,你会听到我跳舞欢呼的声音。
要找出问题出在哪里,你可以使用“ssh -Tvv”命令来运行。
在Linux中,当它成功时,我会看到这个。
debug1: Offering RSA public key: pauljohn@pols124
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug2: input_userauth_pk_ok: fp SHA256:bCoIWSXE5fkOID4Kj9Axt2UOVsRZz9JW91RQDUoasVo
debug1: Authentication succeeded (publickey).

在Windows中,当发生故障时,我会看到它寻找默认名称:
debug1: Found key in /c/Users/pauljohn32/.ssh/known_hosts:1
debug2: set_newkeys: mode 1
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey after 4294967296 blocks
debug2: key: /c/Users/pauljohn32/.ssh/id_rsa (0x0)
debug2: key: /c/Users/pauljohn32/.ssh/id_dsa (0x0)
debug2: key: /c/Users/pauljohn32/.ssh/id_ecdsa (0x0)
debug2: key: /c/Users/pauljohn32/.ssh/id_ed25519 (0x0)
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: publickey
debug1: Trying private key: /c/Users/pauljohn32/.ssh/id_rsa
debug1: Trying private key: /c/Users/pauljohn32/.ssh/id_dsa
debug1: Trying private key: /c/Users/pauljohn32/.ssh/id_ecdsa
debug1: Trying private key: /c/Users/pauljohn32/.ssh/id_ed25519
debug2: we did not send a packet, disable method
debug1: Next authentication method: password
git@gitlab.crmda.ku.edu's password:

那是我需要的提示,它找到了我的~/.ssh/config文件,但从未尝试使用我想要尝试的密钥。
我很少使用Windows,这让我感到沮丧。也许经常使用Windows的人会修复并忘记这个问题。

确认了ssh-add是我需要在Windows上使我的Cygwin设置工作的关键步骤。谢谢!还有一个小问题,"ssh-agent -s"和eval之间有什么区别? - royappa

14

3
谢谢,但我实际上已经尝试过了。我认为Windows不能识别位于“~/.ssh/”目录下的配置文件。有没有可能检查一下,看看Git是否找到了那个文件? - mathew11
1
尝试使用环境变量,例如 USERPROFILEIdentityFile %USERPROFILE%/.ssh/id_rsa_test 或相对路径: IdentityFile id_rsa_test - phts
4
从'man'页面:文件名可以使用波浪线语法引用用户的主目录,也可以使用以下转义字符之一:'%d'(本地用户的主目录),'%u'(本地用户名),'%l'(本地主机名),'%h'(远程主机名)或'%r'(远程用户名)。请尝试使用这些字符。 - phts
1
我不知道为什么它不起作用。实际上,如果我故意犯语法错误,git会识别配置文件,甚至都不会“加载”。但是,尽管如此,我尝试了Unix语法、绝对路径、相对路径。在你上一篇帖子中出现的所有这些字符中,我实际上不知道如何使用它们。 - mathew11
2
在Windows上,请确保您正在写入~/.ssh/config而不是~/.ssh/config.txt。默认情况下,Windows上隐藏文件名扩展名,因此这可能不明显。 - prusswan
显示剩余3条评论

7

对我来说,只需将位于Linux系统上的~/.ssh/config目录中的configssh_config文件添加到Windows的c:\Program Files\Git\etc\ssh\目录中即可。

在某些git版本中,我们需要编辑C:\Users\\AppData\Local\Programs\Git\etc\ssh\ssh_config文件。

之后,我就能够在通过SSH连接或推送到Git Bash时使用所有常规别名和设置了。


2
对我来说,配置文件的名称是“ssh_config”,如果有帮助的话。 - Osmar
1
谢谢你的回答,我发现正确的文件是 C:\Users\itsme\AppData\Local\Programs\Git\etc\ssh\ssh_config - DependencyHell

3
备选方案:告诉git哪个身份文件应该用于某个存储库。因此,您必须使用ssh的-i选项[2]替换git使用的默认ssh命令[1],并使用替代命令。
  1. 在存储库中打开命令行
  2. git config --local core.sshCommand "ssh -i ~/.ssh/<private_key_file>"

[1a] https://git-scm.com/docs/git-config#Documentation/git-config.txt-coresshCommand
[1b] https://git-scm.com/docs/git-config#Documentation/git-config.txt---local
[2] https://www.man7.org/linux/man-pages/man1/ssh.1.html

这是受https://gist.github.com/rbialek/1012262#gistcomment-3730916启发的。

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