Using SSH with two GitHub accounts
October 16, 2018 - 3 min read
This guide walks through setting up two SSH keys on your Mac for use with two different GitHub accounts. You can then seamlessly push to and clone repos on both accounts.
Note: If you just need push access to another account's repo, you can use the Collaborators feature on GitHub.
The 'ssh-keygen' command allows you to generate a public and a private keypair.
- Create an SSH keypair:
ssh-keygen -t rsa -C "email@example.com".
- Type a location and name for the file, such as
primarywith desired name).
Entertwice, leaving the passphrase empty.
- Repeat steps 1-3 to create a second keypair, providing a different name in step 2, such as
Copy Keys to GitHub
The public RSA key is copied to GitHub while the private key lives on your machine.
- Navigate to your SSH directory:
ls -ashould list your new keys.
cat id_rsa_primary.puband copy the output. It should start with
ssh-rsaand end with your email address.
tim@mac .ssh $ cat id_rsa_primary.pub ssh-rsa [your key] firstname.lastname@example.org
- In your GitHub account settings, create a new SSH key and paste the output from the previous step.
- Repeat steps 3-4 with your secondary key and second GitHub account.
Create Config File
- In your ~/.ssh folder, create a file called 'config':
- Use the following template replacing 'primary' and 'secondary' with your accounts.
The first 'Host' block makes your ssh keys persist across reboots. The next two blocks setup an alias which points to an ssh key.
Host * AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_rsa_primary //replace primary IdentityFile ~/.ssh/id_rsa_secondary //replace secondary Host primary.github.com // replace primary HostName github.com User git IdentityFile ~/.ssh/id_rsa_primary // replace primary Host secondary.github.com // replace secondary HostName github.com User git IdentityFile ~/.ssh/id_rsa_secondary // replace secondary
Add keys to your system
The 'ssh-add' command activates a key on your system.
- (Optional) You can delete all previous cached keys with
- To activate the first key on your system:
ssh-add -K ~/.ssh/id_rsa_primary.
- Repeat for second account:
ssh-add -K ~/.ssh/id_rsa_secondary.
- You can confirm they have been added with
2048 SHA256:8moy5Zmdc1XDMv64mh1LHG/13zcmbYCPaY9sFwkWuFM /Users/tim/.ssh/id_rsa_primary (RSA) 2048 SHA256:NOlGpixtyoCK7RyWoVTd7z6k/PFyEaEEeV9YpIij8Sc /Users/tim/.ssh/id_rsa_secondary (RSA)
Test the connection
Connect to GitHub and you should receive a success message.
ssh -T email@example.com.
- Repeat step 1 for your second account.
$ ssh -T firstname.lastname@example.org `Hi tim! You've successfully authenticated, but GitHub does not provide shell access.`
Edit git config
You might need one more change in order to push to your secondary account's repos.
- Navigate to and open the git config file inside your repo folder
- Overwrite the 'origin' section with the following.
[remote "origin"] url = email@example.com:username/project.git
secondary= the host alias in your ssh config file
username= your github username associated with this repo
project= the name of this repo
If you made it this far, congrats!