3

The short version:

Is there any way to set up automatic public-key-based ssh authentication from one Linux account to two different Github accounts?


I have two Github accounts, a work one and a personal one, which I want to keep entirely separate.

I already set up automatic ssh authentication (using my ~/.ssh/id_rsa.pub) in my work Github account. It works fine.

When I try to add the same ssh key to my personal Github account, I get the error that the "key is already in use."

EDIT: OK, I guess that one may be able to do what I want to do through suitable settings in ~/.ssh/config, but I have not yet figured out what these should be. For one thing, it's not clear to me how to specify two different authentication details (User, IdentityFile) for the same host (github.com), and once I do, I don't see how git knows which of the two keys to present when I do git push.

kenorb
  • 155,785
  • 88
  • 678
  • 743
kjo
  • 33,683
  • 52
  • 148
  • 265

2 Answers2

2

You need to create two sets of (public/private) keys, one for each account.

You can reference them through an ssh config file, as detailed in "GitHub: Multiple account setup"/

#Account one
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile /c/Users/yourname/.ssh/id_rsa
    User git

#Account two
Host ac2.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile /c/Users/yourname/.ssh/id_rsa_ac2
    User git
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks! What's still not clear to me is that, AFAIK, for both my accounts the Host is the same: github.com. I don't know how one gets two distinct hosts (like "github.com" and "ac2.github.com"). One possibility that occurs to me is that these strings are arbitrary (e.g. one could have used "foo" and "bar" instead), but then I don't know how to tell git which of these "fantasy" hostnames to use, since the only place I know to tell git about a repo's hostname is in the url field of .git/config, and it would make no sense to put `git@foo:this/that` in that field... – kjo Feb 04 '13 at 13:55
  • @kjo the `Hostname` is the same, not the "`Host`". the ssh address is therefore different: `github.com:myrepo` vs. `ac2.github.com:myrepo`. – VonC Feb 04 '13 at 16:36
0

It seems GitHub doesn't allow the same RSA key for two repositories.

As workaround, you've to create separate RSA keys for each site:

ssh-keygen -t rsa -f rsa_site1
ssh-keygen -t rsa -f rsa_site2

This will generate private and public keys. Then add public keys into GitHub to Deploy keys.

Then deploy your private keys into the remote:

cat rsa_site1 | ssh user@remote "cat > ~/.ssh/rsa_site1 && chmod 600 ~/.ssh/rsa_site1"
cat rsa_site2 | ssh user@remote "cat > ~/.ssh/rsa_site2 && chmod 600 ~/.ssh/rsa_site2"

And to fetch your private repository on the server, you can use something like:

ssh user@remote 'ssh-agent sh -c '\''cd /webroot/site1 && ssh-add ~/.ssh/rsa_site1 && git fetch git@github.com:priv/site1.git'\'
ssh user@remote 'ssh-agent sh -c '\''cd /webroot/site2 && ssh-add ~/.ssh/rsa_site2 && git fetch git@github.com:priv/site2.git'\'
kenorb
  • 155,785
  • 88
  • 678
  • 743