Problems connecting to multiple github accounts with different ssh keys

3

Something strange is happening to me, I have 1 app, in which I have to upload it to 2 different github accounts, however even though I create the ssh-keygen with different names:

ssh-keygen -t rsa -b 2048 -C "clave1" -f /home/user/.ssh/clave1
ssh-keygen -t rsa -b 2048 -C "clave2" -f /home/user/.ssh/clave2

I can only connect to a github account, since the other gives me permission error when trying to upload, although I have verified on several occasions that the key is the same with the account I want connect, until I've re-pasted again, plus I have my .ssh / config

Host host1
HostName github.com
User git
IdentityFile "~/.ssh/clave1"

Host host2
HostName github.com
User git
IdentityFile "~/.ssh/clave2"

What could be happening? I appreciate any suggestions, maybe this is missing something, greetings!

    
asked by Hector Hernandez 30.01.2018 в 08:53
source

2 answers

0

Basically, when it comes to connecting to github, any subdomain works. However, by default the behavior of ssh is to try all the keys, and in your case you will try the first key before the second one. That can be prevented by using IdentitiesOnly yes . So your ~/.ssh/config would be:

Host clave1.github.com
HostName github.com
User git
IdentityFile "~/.ssh/clave1"
IdentitiesOnly yes

Host clave2.github.com
HostName github.com
User git
IdentityFile "~/.ssh/clave2"
IdentitiesOnly yes

And when cloning your repos, you use:

git clone [email protected]:hectorhernandez/proyecto1.git
git clone [email protected]:hectorhernandez/proyecto2.git

If you have already cloned the projects, you can use for example

git remote --set-url origin [email protected]:hectorhernandez/proyecto2.git

Or hand edit your ./.git/config in each project

    
answered by 30.01.2018 / 13:15
source
1

I would add both private keys to the authentication agent. It is a much simpler solution and it does not suppose a problem of security in your particular case since both repositories live in GitHub.

ssh-add /home/user/.ssh/clave1
ssh-add /home/user/.ssh/clave2

This I recommend does not require editing the SSH configuration file. This way you are telling your SSH client to test both keys when it comes to authenticating.

    
answered by 30.01.2018 в 13:31