Using multiple accounts on macOS

Hi there! 

I’m having trouble with the SSH keys while using multiple accounts on macOS the key I add 1st into SSH agent only works, the 2nd one doesn’t work. Thoughts? How can them both working at the same time or how to setup the accounts? 

Thanks in advance!

Hi @abd-ur-rehman,

Thank you for being here! Working with multiple accounts and multiple ssh keys can be a tricky thing to set up.

Most of the time we recommend against having multiple accounts specifically for this reason, and usually just recommend that users use a single account for both work and personal projects. Still, we know this isn’t a good solution for everyone, so hopefully, we can point you in the right direction for setting this up!

To do this you’ll need to create an ssh config file in:

~/.ssh/config

The contents should be similar to this, defining two different SSH hosts that point to GitHub but use different keys. You’ll need to modify the contents to point to your two separate SSH keys in the IdentityFile section:

Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_personal

Host work.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_work

This config sets it up so that every time you clone a repository using a standard SSH URL (git@github.com:username/repo.git) it will use your personal account key, but if you modify the URL slightly you can tell it to use your work key instead (git@work.github.com:username/repo.git).

I hope this helps!

Thanks for your reply. I already have found a solution from here

In My Case this worked

Host github.com
IdentityFile ~/.ssh/id_rsa_personal

Host work.github.com
Host github.com
IdentityFile ~/.ssh/id_rsa_work