简体   繁体   English

为 Windows 10 上的 GitHub 启用多个 SSH 密钥

[英]Enable Multiple SSH Key for GitHub on Windows 10

I have 2 GitHub account and I can't use the same public key for both accounts.我有 2 个 GitHub 帐户,我不能为两个帐户使用相同的公钥。 I have already generated 2 ssh key我已经生成了 2 个 ssh 密钥

  • C:\Users\{User_Name}\.ssh\id_rsa
  • C:\Users\{User_Name}\.ssh\id_rsa_another

My question is, how I can use multiple ssh keys for Windows?我的问题是,如何为 Windows 使用多个 ssh 键? On Linux, I just have to add by using ssh-add command, but on Windows, this command does not exist.在 Linux 上,我只需要使用ssh-add命令添加,但在 Windows 上,此命令不存在。

What have I done to make the other ssh key work, I have to change the id_rsa to id_rsa_temp and id_rsa_another to id_rsa .我做了什么来使其他 ssh 密钥工作,我必须将id_rsa更改为id_rsa_temp并将id_rsa_anotherid_rsa I know this is stupid.我知道这很愚蠢。

First, ssh-add exists on Windows, as part of Git For Windows:首先,ssh-add 存在于 Windows 上,作为 Git 的一部分,对于 Windows:

C:\Users\vonc>where ssh-add
C:\Program Files\Git\usr\bin\ssh-add.exe

It is needed for caching the passphrase protecting a private key (which is not always needed, since you can create a private key without passphrase)需要缓存保护私钥的密码(这并不总是需要,因为您可以在没有密码的情况下创建私钥)

Using different account, as commented, uses a %USERPROFILE%.ssh\config file, with in it:如评论所述,使用不同的帐户使用 %USERPROFILE%.ssh\config 文件,其中包含:

Host gh1
  Hostname github.com
  User git
  IdentityFile ~/.ssh/id_rsa
Host gh2
  Hostname github.com
  User git
  IdentityFile ~/.ssh/id_rsa_another

You can test them with:您可以使用以下方法测试它们:

ssh -Tv gh1
ssh -Tv gh2

You should see a different "Welcome" message per key.您应该看到每个键不同的“欢迎”消息。

And the URL to use for cloning your repo would be:用于克隆您的存储库的 URL 将是:

gh1:user1/repo1
gh2:user2/repo2

Finally, I found the solution最后,我找到了解决方案

I have to use Git Bash because I can't use a regular Windows terminal when running this command我必须使用Git Bash因为在运行此命令时我不能使用常规 Windows 终端

$ eval $(ssh-agent -s)

And then I can add the SSH key by running this command然后我可以通过运行此命令添加 SSH 密钥

$ ssh-add C:/Users/{User_Name}/.ssh/id_rsa_another

We must use the forward-slash for the path我们必须使用正斜杠作为路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM