Using SSH with two GitHub accounts
October 16, 2018 - 3 min
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.
Generate Keys
The 'ssh-keygen' command allows you to generate a public and a private keypair.
- Create an SSH keypair:
ssh-keygen -t rsa -C "youremail@gmail.com"
. - Type a location and name for the file, such as
/Users/yourname/.ssh/id_rsa_primary
(replacingprimary
with desired name). - Hit
Enter
twice, leaving the passphrase empty. - Repeat steps 1-3 to create a second keypair, providing a different name in step 2, such as
/Users/tim/.ssh/id_rsa_secondary
.
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:
cd ~/.ssh
. - Running
ls -a
should list your new keys. - Enter
cat id_rsa_primary.pub
and copy the output. It should start withssh-rsa
and end with your email address.
tim@mac .ssh $ cat id_rsa_primary.pub
ssh-rsa [your key] email@gmail.com
- 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':
touch 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
ssh-add -D
. - 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
ssh-add -l
.
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.
- Enter
ssh -T git@primary.github.com
. - Repeat step 1 for your second account.
$ ssh -T git@primary.github.com
`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
cd projectname/.git/.config
- Overwrite the 'origin' section with the following.
[remote "origin"]
url = git@secondary.github.com:username/project.git
secondary
= the host alias in your ssh config fileusername
= your github username associated with this repoproject
= the name of this repo
Conclusion
If you made it this far, congrats!