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

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

问题描述

GitHub 用户,

我是 github 上的新手,在 git 设置方面有一些问题.我在 github 上有 2 个不同用户的帐户,并且在我的系统上设置了 git 2 次<块引用>

第一个 /.ssh 文件夹(像往常一样)(包含第一个复制的 id_rsaaccount)
第二个/.ssh/new文件夹(包含复制进去的id_rsa第二个帐户)

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

因为如果我想推送第二个帐户,它将使用 .ssh 密钥而不是 .ssh/new 并给我错误.

如果我在这里理解错了,请纠正我.

谢谢.

解决方案

(我已投票决定关闭此问题,因为它可能重复,但无论如何我还是添加一个类似的答案.)

使用 SSH 传输时,GitHub 根据您用于身份验证的 SSH 密钥将您识别为用户.因此,您需要确保 git 将一个 SSH 密钥用于一个存储库,而另一个用于另一个存储库.

我将假设:

  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.

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

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

宿主 github-as-user1主机名 github.com用户 gitIdentityFile/home/whoever/.ssh/id_rsa

然后你可以添加一个名为 gh-user1 的遥控器,比如:

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

... 然后,如果您想使用 ~/.ssh/id_rsa<将 master 分支推送到 GitHub 上的存储库 user1/whatever/code> 键,你可以这样做:

git push gh-user1 master

为了以其他用户 (user2) 的身份推送到第二个存储库,您需要向您的 ~/.ssh/config 文件添加第二个别名.例如:

主机 gh-as-user2主机名 github.com用户 gitIdentityFile/home/whoever/.ssh/new/id_rsa

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

git push gh-user2 master

GitHub Users,

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

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)

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

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.)

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.

I'm going to assume that:

  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.

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.)

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

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

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

... 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

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天全站免登陆