kjo
kjo

Reputation: 35301

How to set up authentication for two separate GitHub accounts from same ssh client?

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.

Upvotes: 3

Views: 2124

Answers (2)

kenorb
kenorb

Reputation: 166319

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 [email protected]:priv/site1.git'\'
ssh user@remote 'ssh-agent sh -c '\''cd /webroot/site2 && ssh-add ~/.ssh/rsa_site2 && git fetch [email protected]:priv/site2.git'\'

Upvotes: 0

VonC
VonC

Reputation: 1323115

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

Upvotes: 2

Related Questions