How to configure authentication for two separate GitHub accounts from the same ssh client?

Short version:

Is there a way to configure automatic public key ssh authentication from one Linux account to two different Github accounts?


I have two Github accounts, one working and personal, which I want to completely separate.

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

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

EDIT: OK, I think it’s possible to do what I want to do with the appropriate settings in ~/.ssh/config, but I still do not understand what it should be. First, it’s not clear to me how to specify two different authentication details ( User, IdentityFile) for the same host ( github.com), and as soon as I do this, I don’t see how git knows which of the two keys to be present when I do git push.

+2
source share
2 answers

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

You can link to them through the ssh configuration file, as described in the GitHub section : a few account settings "/

#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
+2
source

, GitHub RSA .

RSA :

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

. GitHub .

:

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"

, - :

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'\'
0

Source: https://habr.com/ru/post/1692657/


All Articles