git 如何使用多个 ssh 密钥
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8924210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to work with multiple ssh keys
提问by Arun Rana
GitHub Users,
GitHub 用户,
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
我是 github 上的新手,对 git 设置有一些问题。我在 github 上有 2 个不同用户的帐户,并且我在我的系统上设置了 git 2 次
First /.sshfolder (as usual) (contain id_rsa which copied in first account)
Second /.ssh/newfolder (contain id_rsa which copied in second account)
第一个/.ssh文件夹(像往常一样)(包含在第一个帐户中复制的 id_rsa)
第二个/.ssh/new文件夹(包含在第二个帐户中复制的 id_rsa)
now at the time of push how can i switch between ssh key?
现在在推送时如何在 ssh 密钥之间切换?
Because if i would like to push for second account it will use .ssh key instead of .ssh/new and gives me error.
因为如果我想申请第二个帐户,它将使用 .ssh 密钥而不是 .ssh/new 并给我错误。
Please make me correct if i am understood something wrong here.
如果我在这里理解错误,请纠正我。
Thanks.
谢谢。
回答by Mark Longair
(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.
使用 SSH 传输时,GitHub 根据您用于身份验证的 SSH 密钥将您标识为用户。因此,您需要确保 git 将一个 SSH 密钥用于一个存储库,另一个用于另一个存储库。
I'm going to assume that:
我将假设:
- 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 asuser1
isuser1/whatever
on GitHub. - 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 asuser2
isuser2/whatever
on GitHub.
- 您有一个名为 的 GitHub 帐户
user1
,并且您已将与您的本地私钥 对应的公钥添加到该帐户/home/whoever/.ssh/id_rsa
。比方说,你有兴趣访问资源库为user1
是user1/whatever
在GitHub上。 - 您有一个名为的第二个 GitHub 帐户,
user2
并且您已将与您的本地私钥对应的公钥添加到该帐户/home/whoever/.ssh/new/id_rsa
。比方说,你有兴趣访问资源库为user2
是user2/whatever
在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.)
解决这个问题的最简单方法是为每个存储库创建一个新的“远程”(即远程存储库的昵称),其中每个远程 URL 中的主机名实际上是您在~/.ssh/config
. (如果该配置文件不存在,则必须创建它。)
For example, one entry in the ~/.ssh/config
file might look like:
例如,~/.ssh/config
文件中的一个条目可能如下所示:
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:
然后您可以添加一个名为 的遥控器gh-user1
,例如:
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:
...然后如果你想使用密钥将你的master
分支推送到user1/whatever
GitHub 上的存储库~/.ssh/id_rsa
,你可以这样做:
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:
为了以其他用户 ( user2
) 的身份推送到第二个存储库,您需要向您的~/.ssh/config
文件添加第二个别名。例如:
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