GitHub 错误: 密钥已在使用中

我创建了两个 GitHub 账户。一个给我的工作用户,一个给我自己。我需要做赶上一些工作,因此克隆我的工作回购到我的个人电脑。为了做简单的“ git 推原点主人”提交没有输入我的用户名和密码整个时间,我只是想添加我的公钥从我的家电脑到工作回购。然而 Github 给出了这个错误:

Error: Key already use

在 Google 了一会儿之后,我发现了这个 链接,上面写着: “要解决这个问题,从其他帐户或存储库中删除密钥,并将其添加到你的帐户中。”当然还有一个重复的密钥,因为我已经将我的主公钥添加到 github 中,这样我就可以在自己的个人项目中编写代码了。毕竟,我想能够代码到我的工作回购使用我的工作电脑和个人电脑。

如何在添加多个“相同”公钥的同时不抛出该错误,以及为什么会首先抛出该错误?

57652 次浏览

You can create one more key pair, say id_rsa_personal.pub, and add it to the Github account.

Next, create/edit the .ssh/config file.

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

Next, you will need to alter your .git/config remote url to point to:

git@github-personal:<gh_username>/<gh_reponame>.git

Rather than the usual:

git@github.com:<gh_username>/<gh_reponame>.git

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 git@github.com/<reponame>.git

The key could be already in use on other github projects as deploy key, that's a bit tricky to find but run:

ssh -T -ai ~/.ssh/KEY_NAME git@github.com

change KEY_NAME with the name of your SSH private key and you will be good to go

from: https://help.github.com/articles/error-key-already-in-use/#finding-where-the-key-has-been-used

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

You can create a different key in your machine without disturbing your existing keys like:ssh-keygen -t rsa -C "your_email@example.com"
Now provide your file name to identify your key for the repository

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-keys for details.

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 can use different SSH keys for each account. The only downside then is how to easily switch between them?

I use ssh-agent and it can use multiple keys at the same time. I auto add them through .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.

You probably just need to change the name of your key.

ssh-keygen -t rsa -b 4096 -C "<email>"

Then it will ask to enter the file name, make sure that you enter the path name which does not exist on your system. Usually, "id_rsa" file name is created by default on MAC. Just change the name and then use following command to copy

pbcopy < {Path where SSH-Keygen is stored}

That's it. You can simply paste this keygen (copied from above command on the clipboard) in GitHub and use that without any problem.

This works for me,

  1. Generate the SSH keys using ssh-keygen. Let's say the key is in ~/.ssh/id_rsa_foo
  2. Now, $ cat ~/.ssh/id_rsa_foo.pub and paste it on your GitHub keys.
  3. You can use GIT_SSH_COMMAND environment variable to achieve what you want.
    Do $ GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa_foo' git push origin master. Check your Github repo for the magic. Cheers!