如何在gitbash上的Windows计算机上生成多个SSH公钥并进行配置?

时间:2019-06-09 19:12:49

标签: linux windows git ssh ssh-keys

让我们首先描述我所遇到的问题:

  • 我有两台PC,其中一台是Windows计算机,另一台是Linux计算机。

  • 我想在这两台计算机上生成两个SSH公共密钥。一个用于我的GitHub帐户,另一个用于我的GitLab帐户。

  • 我已经使用以下命令生成了公共SSH密钥,该密钥在我的Linux机器上可以正常使用,但在Windows机器上却不能。

命令:

步骤1:生成SSH密钥

ssh-keygen

步骤2:将目录和文件名写入要保存SSH密钥的位置。

[就我而言,我在下面插入了Windows路径]

/c/Users/PC_USER_NAME/.ssh/id_rsa_hub

第3步:从文件中获取公钥

cat ~/.ssh/id_rsa_hub.pub

这些步骤在我的Linux机器上可以正常工作,但是在Windows上,我看到了身份验证错误。如何配置SSH公钥?

2 个答案:

答案 0 :(得分:2)

对于Windows机器,您必须再进行一次配置。只需按照下面的步骤操作(如果您使用的是Git Bash):

  1. 转到.ssh目录/c/Users/PC_USER_NAME/.ssh/,单击鼠标右键并选择“此处Git Bash”
  2. 使用以下命令创建一个名为“ config”的文件:
touch config
  1. 现在使用以下命令打开配置文件:
nano config
  1. 现在在配置文件中写入以下行

假设您已经为Github创建了两个名为id_rsa_hub的文件,为GitLab创建了两个id_rsa_lab的文件

# GITHUB
Host github.com
   HostName github.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_rsa_hub

# GITLAB
Host gitlab.com
   HostName gitlab.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_rsa_lab

答案 1 :(得分:1)

尝试在Windows上使用-m PEMlegacy format, in case the new OpenSSH one isn't fully recognized生成SSH密钥:

ssh-keygen -m PEM -t rsa -P "" -f /c/Users/<yourLogin>/.ssh/id_rsa_hub
ssh-keygen -m PEM -t rsa -P "" -f /c/Users/<yourLogin>/.ssh/id_rsa_lab

然后在~/.ssh/config文件中声明您的两个密钥:

Host gh
 HostName github.com
 User git
 IdentityFile /c/Users/<yourLogin>/.ssh/id_rsa_hub  

Host gl
 HostName gitlab.com
 User git
 IdentityFile /c/Users/<yourLogin>/.ssh/id_rsa_lab  

Host *
  PreferredAuthentications publickey

您的SSH URL将为gh:<user>/<repo>gl:<user>/<repo>