如何使用多个ssh密钥 [英] How to work with multiple ssh keys

查看:86
本文介绍了如何使用多个ssh密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GitHub用户,

我是github上的新手,并且在安装git时遇到了一些问题。
我在github上有不同用户的2个帐户,并且我已经在系统上设置了git 2次

I am newbie on github and have some issue with git setup. I have 2 account with different users on github and i have setup git on my system 2 times


第一个 / .ssh 文件夹(与往常一样)(包含复制到第一个
帐户中的id_rsa)
第二个 / .ssh / new 文件夹(包含复制到$ b中的id_rsa) $ b第二个帐户)

First /.ssh folder (as usual) (contain id_rsa which copied in first account)
Second /.ssh/new folder (contain id_rsa which copied in second account)

现在在推送时我该如何在ssh密钥之间切换?

now at the time of push how can i switch between ssh key?

因为如果我想要推入第二个帐户,它将使用.ssh键而不是.ssh / new,并且给我错误。

Because if i would like to push for second account it will use .ssh key instead of .ssh/new and gives me error.

请让我正确的,如果我在这里理解错了什么。

Please make me correct if i am understood something wrong here.

谢谢。

Thanks.

推荐答案

(我已经投票决定把它作为一个可能的副本来关闭,但我也可以添加类似的答案。)

(I've voted to close this as a possible duplicate, but I might as well add a similar answer anyway.)

使用SSH传输时,GitHub根据您用于验证的SSH密钥将您识别为用户。所以,你需要确保git为一个存储库使用一个SSH密钥,另一个为另一个。

When using the SSH transport, GitHub identifies you as a user based on the SSH key that you use to authenticate. So, you need to make sure that git is using one SSH key for one repository and another for the other.

我将假设:


  1. 您有一个名为 user1 的GitHub帐户,并且您已将该公钥添加到该帐户对应于您的本地私钥 /home/whoever/.ssh/id_rsa 。假设您有兴趣作为 user1 访问的存储库在GitHub上是 user1 / whatever

  2. 您有第二个名为 user2 的GitHub帐户,并且您已将与您的本地私钥 /home/whoever/.ssh/new/id_rsa 。假设您有兴趣作为 user2 访问的存储库在GitHub上是 user2 / whatever

  1. You have a GitHub account called user1, and you've added to that account the public key corresponding to your local private key /home/whoever/.ssh/id_rsa. Let's say that the repository you're interested in accessing as user1 is user1/whatever on GitHub.
  2. You have a second GitHub account called user2 and you've added to that account the public key corresponding to your local private key /home/whoever/.ssh/new/id_rsa. Let's say that the repository you're interested in accessing as user2 is user2/whatever on GitHub.

处理此问题的最简单方法是为每个存储库创建一个新的远程(即远程存储库的昵称),其中主机名在每个远程的URL中,实际上是您在〜/ .ssh / config 中设置的别名。 (如果该配置文件不存在,您必须创建它。)

The simplest way to do deal with this is to create a new "remote" (i.e. a nickname for a remote repository) for each repository, where the hostname is in each remote's URL is actually an alias that you've set up in ~/.ssh/config. (If that config file doesn't exist, you'll have to create it.)

例如,〜/中的一个条目。 ssh / config 文件可能如下所示:

For example, one entry in the ~/.ssh/config file might look like:

Host github-as-user1
  HostName github.com
  User git
  IdentityFile /home/whoever/.ssh/id_rsa



<然后你可以添加一个名为 gh-user1 的远程,比如说:

Then you can add a remote called gh-user1, say, with:

git remote add gh-user1 git@github-as-user1:user1/whatever.git

...然后如果你想在GitHub上使用GitHub上的 user1 / whatever 将你的 master 〜/ .ssh / id_rsa 键,您可以:

... and then if you want to push your master branch to the repository user1/whatever on GitHub using the ~/.ssh/id_rsa key, you can just do:

git push gh-user1 master

为了像其他用户那样推送( user2 )到第二个存储库,您需要为您添加第二个别名〜/ .ssh / config 文件。例如:

In order to push as the other user (user2) to the second repository, you need to add a second alias to you ~/.ssh/config file. For example:

Host gh-as-user2
  HostName github.com
  User git
  IdentityFile /home/whoever/.ssh/new/id_rsa

然后推到第二个存储库不同的用户,你可以这样做:

Then to push to that second repository as the different user, you can just do:

git push gh-user2 master

这篇关于如何使用多个ssh密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆