git GitHub 错误:密钥已在使用中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21160774/
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
GitHub Error: Key already in use
提问by John Crawford
I have created two GitHub accounts. One for my work user and one for my personal self. I needed to do catch up on some work and as such cloned my work repo onto my personal PC. In order to do simple "git push origin master" commits without entering my username and password the whole time I simply want to add my public key from my home pc to the work repo. However Github gives this error:
我创建了两个 GitHub 帐户。一种用于我的工作用户,一种用于我的个人。我需要赶上一些工作,因此将我的工作存储库克隆到我的个人 PC 上。为了进行简单的“git push origin master”提交而无需一直输入我的用户名和密码,我只想将我的公共密钥从我的家用电脑添加到工作存储库中。但是 Github 给出了这个错误:
Error: Key already use
After a bit of Googling I came across this linkwhich states "To resolve the issue, remove the key from the other account or repository and add it to your account" Of course there is a duplicate key as I've added my home public key to github so that I can code on my own personal projects. After all I want to be able to code to my work repo using both my work pc and personal pc.
经过一番谷歌搜索后,我发现了此链接,其中指出“要解决此问题,请从其他帐户或存储库中删除密钥并将其添加到您的帐户”当然,由于我添加了家庭公钥,因此存在重复的密钥到 github,以便我可以在自己的个人项目上编码。毕竟,我希望能够同时使用我的工作电脑和个人电脑对我的工作存储库进行编码。
How can you add multiple "same" public keys without Github throwing that error and also why in the world, is that error thrown in the first place?
如何在 Github 不抛出该错误的情况下添加多个“相同”公钥,以及为什么在世界上首先抛出该错误?
采纳答案by makevoid
The key could be already in use on other github projects as deploy key, that's a bit tricky to find but run:
该密钥可能已经在其他 github 项目中作为部署密钥使用,找到但运行有点棘手:
ssh -T -ai ~/.ssh/id_rsa [email protected]
ssh -T -ai ~/.ssh/id_rsa [email protected]
to find the used key, delete it and then readd it again in the right user/repo. that was very useful for me
找到使用过的密钥,删除它,然后在正确的用户/存储库中再次读取它。这对我很有用
from: https://help.github.com/articles/error-key-already-in-use/#finding-where-the-key-has-been-used
来自:https: //help.github.com/articles/error-key-already-in-use/#finding-where-the-key-has-been-used
edit: as pointed out by @mikhail-morgunov, this doesn't works all the time, you should really use this snippet only if the default id_rsa
SSH private key is your github's default one
编辑:正如@mikhail-morgunov 所指出的,这并不总是有效,只有当默认的id_rsa
SSH 私钥是您的 github 的默认私钥时,您才应该真正使用此代码段
this is a snippet where the key name has been extracted:
这是提取密钥名称的片段:
ssh -T -ai ~/.ssh/KEY_NAME [email protected]
ssh -T -ai ~/.ssh/KEY_NAME [email protected]
change KEY_NAME
with the name of your SSH private key and you will be good to go
更改KEY_NAME
为您的 SSH 私钥的名称,您将很高兴
回答by Bijendra
You can create one more key pair, say id_rsa_personal.pub
, and add it to the Github account.
您可以再创建一对密钥,例如id_rsa_personal.pub
,并将其添加到 Github 帐户。
Next, create/edit the .ssh/config
file.
接下来,创建/编辑.ssh/config
文件。
# Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
Host github-public
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_public
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
The above file will help you to use more than one Github account. For background info, refer to the answers to this question.
上述文件将帮助您使用多个 Github 帐户。有关背景信息,请参阅此问题的答案。
Next, you will need to alter your .git/config
remote url to point to:
接下来,您需要更改.git/config
远程 url 以指向:
git@github-personal:<gh_username>/<gh_reponame>.git
git@github-personal:<gh_username>/<gh_reponame>.git
Rather than the usual:
而不是通常的:
[email protected]:<gh_username>/<gh_reponame>.git
[email protected]:<gh_username>/<gh_reponame>.git
回答by James Ferguson
John commented that it didn't work for him.
约翰评论说这对他不起作用。
Perhaps the step you're missing is you need to alter your .git/config remote url to point to git@github-personal/<reponame>.git
etc.. rather than the usual [email protected]/<reponame>.git
也许您缺少的步骤是您需要更改 .git/config 远程 url 以指向git@github-personal/<reponame>.git
等...而不是通常的[email protected]/<reponame>.git
回答by HariKishore
you can use the same ssh key for different github repositories but cannot use the same ssh key for many repositories (i.e,same repository from different logins or from forked) as github will not allow same deploy key more than once for a repository
您可以对不同的 github 存储库使用相同的 ssh 密钥,但不能对多个存储库使用相同的 ssh 密钥(即,来自不同登录名或分叉的相同存储库),因为 github 不允许一个存储库多次使用相同的部署密钥
You can create a different key in your machine without disturbing your existing keys like:ssh-keygen -t rsa -C "[email protected]"
Now provide your file name to identify your key for the repository
您可以在您的机器中创建一个不同的密钥,而不会干扰您现有的密钥,例如:ssh-keygen -t rsa -C "[email protected]"
现在提供您的文件名以标识存储库的密钥
Enter file in which to save the key (/home/demo/.ssh/id_rsa):/home/demo/.ssh/id_rsa_mykey<br>
See https://developer.github.com/guides/managing-deploy-keys/#deploy-keysfor details.
有关详细信息,请参阅https://developer.github.com/guides/managing-deploy-keys/#deploy-keys。
回答by mandarin
I've found a workaround that works for me:
我找到了一个对我有用的解决方法:
You cannot add the same SSH key to different accounts, and that is true for GitHub, BitBucket, etc. But you canuse different SSH keys for each account. The only downside then is how to easily switch between them?
您不能将相同的 SSH 密钥添加到不同的帐户,对于 GitHub、BitBucket 等也是如此。但是您可以为每个帐户使用不同的 SSH 密钥。那么唯一的缺点是如何轻松地在它们之间切换?
I use ssh-agent
and it can use multiple keys at the same time. I auto add them through .bashrc
我使用ssh-agent
并且它可以同时使用多个键。我自动添加它们.bashrc
if [ -z "$SSH_AUTH_SOCK" ] ; then
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa2
fi
This approach works regardless of which key is added in your GitHub account. I guess ssh-agent
makes attempts with each key until it succeeds. And if you don't want to add all keys, you just comment out the relevant line in the .bashrc
before starting a new shell.
无论在您的 GitHub 帐户中添加哪个密钥,此方法都有效。我想ssh-agent
对每个键进行尝试,直到成功为止。如果您不想添加所有键,只需.bashrc
在启动新 shell 之前注释掉相关行。