How to configure multiple accounts with git/ssh
Lets say you have multiple accounts on a single github server.
When you try to pull from those accounts using SSH keys, you will often get an error, because SSH will indiscriminately try all your keys in no particular order.
This means that if you have two keys for Account A and B, and you want to access a repo under Account B, ssh may choose to use the key for Account A, which means that you will be authenticated succesfully under Account A ( because the account exists and the key is correct) but you will get an error because Account A does not have rights in the repo that is owned by account B.
To fix this, you need to set up an SSH config file in ~/.ssh/config
# Account A
Host github-accountA
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_accountA
IdentitiesOnly yes
# Account B
Host github-accountB
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_accountB
IdentitiesOnly yes
Then for the repos, you need to specify the updated host:
❯ git remote get-url origin
git@github.com:ibm-ae/clinical-notes-ui.git
❯ git remote set-url --pull origin git@patrick-harned:ibm-ae/clinical-notes-ui.git
Here the organization that I am referencing is under the account patrick-harned, not pwharned. I set the hostname to patrick-harned, which tells ssh to use the keys undder patrick-harned in the config file.
Host pwharned
HostName github.com
User pwharned
PubkeyAuthentication yes
PreferredAuthentications publickey
IdentityFile /home/pat/.ssh/id_rsa_pub_git
IdentitiesOnly yes
Host patrick-harned
HostName github.com
User patrick-harned
PubkeyAuthentication yes
PreferredAuthentications publickey
IdentityFile ~/.ssh/patrick-harned
IdentitiesOnly yes