GitHub Error: Key already in use

48,421

Solution 1

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 [email protected]

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

Solution 2

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:

[email protected]:<gh_username>/<gh_reponame>.git

Solution 3

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

Solution 4

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.

Solution 5

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!
Share:
48,421

Related videos on Youtube

John Crawford
Author by

John Crawford

Updated on July 08, 2022

Comments

  • John Crawford
    John Crawford almost 2 years

    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:

    Error: Key already use
    

    After a bit of Googling I came across this link which 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.

    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?

    • PeeHaa
      PeeHaa over 10 years
      Why do you need the same keys? Just create a new key and connect it to your account.
    • Малъ Скрылевъ
      Малъ Скрылевъ over 10 years
      just create an other key pair, and keep it locally, and public part of the second pair put into github's other account
    • poke
      poke over 10 years
      GitHub will use the key as means to identify you when you connect to them via SSH. As such, you cannot have multiple accounts with the same key, as GitHub won’t be able to tell then which of your accounts you want to use.
    • John Crawford
      John Crawford over 10 years
      @Poke, really, thanks for that. Question, wouldn't it just be easier to simply set which "account" I'm using somewhere in the .git/config file instead of generating all these extra keys?
    • poke
      poke over 10 years
      Usually you are not expected to have multiple accounts in the first place. You can use organizations to manage multiple different “sets” or repositories, while having only a single user account.
  • John Crawford
    John Crawford over 10 years
    This didn't seem to work. I still get the permission denied. This is what my ~/.ssh/config file looks like: pastebin.com/JNiTUbVU
  • Малъ Скрылевъ
    Малъ Скрылевъ over 10 years
    And have you added ~/.ssh/id_rsa.pub for company user, and ~/.ssh/John.pub for yourself? But anyway, the correct approach is not to create the specifc user for company, but organization.
  • mmike
    mmike about 6 years
    no. we need to use another one. this is the center of trouble
  • Keval Domadia
    Keval Domadia over 4 years
    Take your bounty! You deserve it for formulating the answer so easyily.
  • NwosuCC
    NwosuCC over 4 years
    Quoting a comment in the other linked answers, "Note that you can also specify multiple IdentityFile entries for the same Host, which are then tried in order when connecting". With this approach, you do not have to create another different config entry (e.g "github.com-personal") for the same hostname (e.g "github.com") in the ~/.ssh/config file. Just add another IdentityFile ~/.ssh/id_rsa_personal line under the IdentityFile ~/.ssh/id_rsa and add the new key to the new github account
  • Rob Streeting
    Rob Streeting over 4 years
    For anyone else getting "Bad owner or permissions on ~/.ssh/config" after following the steps here (especially if you had to create the .ssh/config file from scratch), make sure the file has read only permissions with chmod 600 ~/.ssh/config and that it the current user is the owner with sudo chown $USER ~/.ssh/config. See this serverfault answer for more details.
  • Alexander Torstling
    Alexander Torstling over 4 years
    @NwosuCC Having multiple IdentityFiles doesn't work in the dual git account case, because ssh will retry until one key is accepted. Since both keys are accepted by github.com, ssh will stop at the first one. After this authentication, git proceeds to try to access some resource on the host, and only at this point does the authentication fail. As far as ssh is concerned, the operation was successful. Unfortunately.
  • Henke
    Henke over 3 years
    When running ssh -T -ai ~/.ssh/id_rsa [email protected] I get Permission denied.
  • Curious
    Curious about 3 years
    @Bijendra - can you please add steps for creating new SSH key pair with commands?
  • Bijendra
    Bijendra about 3 years
    @Harshita ssh-keygen should be enough to create one. Read this ssh.com/ssh/keygen
  • Curious
    Curious about 3 years
    @Bijendra Thanks. I was asking for this command to create a new SSH key pair - ssh-keygen -t rsa -b 4096 -f /home/my/.ssh/id_rsa_new
  • makevoid
    makevoid over 2 years
    @Henke you may need to sudo ssh (but it is very uncommon to have a setup where your can't call the ssh command without it)
  • Henke
    Henke over 2 years
    Hmm. I don't even remember if I ran that command in Windows Subsystem for Linux or in Git Bash (but most likely in one of them). The prefix sudo makes no sense in Git Bash / MSYS2, so it might be important whether I ran it in MSYS2 or not ... (:/)
  • Henke
    Henke over 2 years
    Why don't you add sudo as a prefix in your answer? It wouldn't hurt, I think. I mean – people like myself, who run "Linux" commands inside MSYS2 every now and then – we will very likely know sudo makes no sense in an MSYS2 terminal. So I don't think an added sudo would confuse anyone. Users of MSYS2 will know it is not native Linux. (To clarify: I run MSYS2 installed on Windows 10.)
  • Henke
    Henke over 2 years
    On second thought, I believe most likely I ran the command in WSL (which is the closest you can get to "native Linux" on a Windows OS – without running a virtual machine of course). I just tried it again. Without sudo I get Permission denied (publickey). But with sudo I get [sudo] password for henke: (asking for my password). So I conclude the most likely reason I got Permission denied in January is that I left out the sudo. – What is the reason you don't include sudo in your answer? (Sorry for being a bit lengthy in these comments.)
  • Slaiyer
    Slaiyer over 2 years
    How to set this for cloning the repository in the first place?
  • James Ferguson
    James Ferguson over 2 years
    Assuming you have the config as listed above, then you take the ssh url from github: Click "Code" button, select "SSH" option, copy to clipboard. clone it but replace "github.com" with "github-personal"