2

I use my computer for many different things, as I’m sure many devs do. But I only have one public ssh key, and I hope to keep it that way. I have two accounts for github.com: one for personal use, and one for my employer.

So, when I am using the account with my employer, I’m trying to add my ssh key as I usually do, by clicking “Profile” and then “Settings” and then “ssh and gpg keys”. And then I paste my id_rsa.pub contents, and click “add ssh key”. But it is giving me the following:

“Key is already in use”

And of course that is because I am using that key for my personal account. How do I allow this key to also be used for my employer account with github? Thank you.

As an additional inquiry, I do not understand why it matters if the same public key is used twice for two different accounts. What is the harm in that? and why does github not allow that?

There seems to be a difference between committing using different ssh keys at the host level, and at the repo level.

makansij
  • 9,303
  • 37
  • 105
  • 183
  • 1
    Possible duplicate of [Multiple GitHub Accounts & SSH Config](https://stackoverflow.com/questions/3225862/multiple-github-accounts-ssh-config) – phd Jan 05 '18 at 22:40
  • 1
    There is no way. If you add one key to 2 accounts how is Github supposed to recognise to what account you're connecting? – phd Jan 05 '18 at 22:40
  • hm. okay i see what you are saying. – makansij Jan 06 '18 at 00:30
  • so then do I just need to do `ssh-keygen` etc., and `git config --global user.name "jaja"` and `git config --global user.email "jaja@email.com"` ? and then add the corresponding public key under "settings" -> "ssh and gpg keys" -> "add ssh key"? anything else needed to do? – makansij Jan 06 '18 at 00:36
  • 1
    You need multiple SSH keypairs — one keypair for every account. You also need multiple name/email pairs — also one for account. Configure local name/email in every repo or configure global name/email for one Github account and override them in some repos for the 2nd/3rd/etc accounts. Register emails at Github. – phd Jan 06 '18 at 00:42
  • The problem with the solution in https://stackoverflow.com/questions/3225862/multiple-github-accounts-ssh-config is that it request a separate entry in `~/.ssh/config` for *each repo* that you wish to commit to. – makansij Jan 06 '18 at 01:54
  • Not for each repo — for each keypair (i.e., for each Github account). – phd Jan 06 '18 at 10:33

1 Answers1

5

As others have pointed out, you need to generate two key pairs. And then when you need to work using your personal key on personal projects, clone the repo as follows.

ssh-agent bash -c 'ssh-add /home/<user>/.ssh/personal; git clone git@github.com:user/project.git'

For employer projects, clone as follows.

ssh-agent bash -c 'ssh-add /home/<user>/.ssh/employer; git clone git@github.com:user/project.git'

Note that personal and employer are the respective private key names.

After doing the cloning as above, you don't need to worry about the key. Git will use the SSH key you cloned the repo with for all future operations on that repo.

Ishan Thilina Somasiri
  • 1,179
  • 1
  • 12
  • 24
  • you mean by default, whatever private key i use for cloning the repo, the corresponding public key will be used when I do all of my remote git commands? – makansij Jan 06 '18 at 02:01
  • "After doing the cloning as above, you don't need to worry about the key. Git will use the SSH key you cloned the repo with for all future operations on that repo" - really? if so that is great. but then why is there such a long explanation here about needing to change the `~/.ssh/config` file, etc.? https://stackoverflow.com/questions/3225862/multiple-github-accounts-ssh-config – makansij Jan 06 '18 at 02:08
  • 1
    The answer is trying to set the ssh key per host, `me.github.com` and `work.github.com`. My answer is setting it per repo. – Ishan Thilina Somasiri Jan 06 '18 at 02:19
  • Yes, but isn't that answer for `me.github.com` and `work.github.com` essentially just making a different hostname for each repo, so then it *is* setting it per repo basically? That's the way I am interpreting it at least...(I actually think that your answeris much more convenient, because for the other answer I linked to, it seems to me like you would have to have a **different** entry in the `~/.ssh/config` file for each and every repo in github) – makansij Jan 06 '18 at 02:26
  • 1
    Yes, if you want the ssh key on a per host level, that's the answer. Then this question is a duplicate of that question. – Ishan Thilina Somasiri Jan 06 '18 at 02:27
  • 1
    I see thanks @IshanThilinaSomasiri. so if I wanted to push from one local repo, to two different remote repos, each requiring a separate ssh key and separate credentials, then I could not use your answer. I would have to use the "per-hostname" answer in the question I linked to, and specify a different `git remote add ` for each repo I want to push to. correct? – makansij Jan 06 '18 at 02:44
  • I have not tested that answer, but by looking at what he suggests, your understanding on that seems to be correct. – Ishan Thilina Somasiri Jan 06 '18 at 03:41
  • `ssh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"` And then use the private key with the command. – Coder Jan 07 '19 at 02:03