How to use same ssh public key again for another o

2019-08-28 20:28发布

问题:

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.

回答1:

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.