In this tutorial we’re going to setup multiple GitHub accounts on the same computer with SSH. This is useful if you have a work account and a personal account, or any other time you have multiple GitHub accounts that you’re using on the same machine.
Assuming you don’t have ANY ssh keys setup for git, the steps are the following:
- Create SSH key (id_rsa is standard name)
- Add SSH key to primary GitHub account
- Add SSH key to SSH-Agent
- Create secondary SSH key (id_rsa_other_account)
- Add secondary SSH key to other GitHub account
- Add secondary SSH key to SSH-Agent
- Add both keys to a “config” file located in the .ssh/ folder
- Change Git repository user & email
If you already have an SSH key (id_rsa) and it’s added to SSH-Agent, then you can just skip the first 3 steps. That looks like a lot of steps but they’re pretty simple to follow along with. Let’s begin.
Create SSH Key
Run the command
$ ssh-keygen -t rsa -C "email@example.com" to create your SSH key. -t is the “type” flag, which we set to rsa, and -C is for comment. See OpenBSD manual.
You’ll get something like this:
Generating public/private rsa key pair. Enter file in which to save the key (/Users/john/.ssh/id_rsa): #### This is *Nix flavored machines Enter file in which to save the key (C:\Users\John/.ssh/id_rsa): #### Poor souls on Windows
Note: If you already have an id_rsa and id_rsa.pub file, note that this command will overwrite the existing id_rsa files and will mess up anything currently using that key. See below when we create our second SSH key.
I left the passphrase empty when prompted for one.
Now check to make sure you have an id_rsa and id_rsa.pub file in your \Users\youruser\.ssh\ directory. If so, you’re ready to add the public key to GitHub
Add SSH Key to GitHub
Login to your main GitHub account. If you go into your \Users\youruser\.ssh\ folder you can copy the entire contents of the id_rsa.pub file and paste it into the key field as seen below. The title can be used to describe the machine. Something like “Desktop Computer home” or “Office space 27 xyz co” would be good.
Add SSH Key to SSH-Agent
An SSH-Agent is something the client (your computer) uses to “sign” a message that gets forwarded back to the server. The server looks at the signature from the SSH-Agent and sees if it matches the private key. You can read more about SSH-Agent here.
$ ssh-add \path\to/.ssh/id_rsa in your terminal. If you have problems, you may need to setup your ssh-agent. I don’t think there’s problems on *Nix machines, but Windows users can see the below troubleshooting steps if you run into issues. Your mileage may vary.
Windows users may need to verify ssh-add actually works and an ssh-agent is installed. Here’s a stackoverflow post that helped me. (See tamj0rd2’s answer and the 2019 solution)
Make sure OpenSSH is installed and started
- Start Menu
- Search “Manage optional features”
- Make sure “Open SSH Client” is in the list. If not, figure out how to add it.
- Open “Services” from start menu
- Scroll down to “OpenSSH Authentication Agent” and right-click “properties”.
- Change startup type from “disabled” to “Automatic (Delayed Start)” so the SSH-Agent is turned on automatically after booting.
- Restart computer??? These steps didn’t work until I restarted.
At this point you may want to test this ssh key works by creating a remote repository and pushing a commit from your local computer. I’ll save that until the end to save time.
Rinse & Repeat for Steps 4, 5, 6
Now do the same thing for your other id_rsa key with your other email, but add an identifier at the end of the key! Make sure you add the filename including the path to your .ssh/ folder.
$ ssh-keygen -t rsa -C "yourOTHERemail@email.com" ## you'll receive terminal output Enter file in which to save the key (C:\Users\John\.ssh/id_rsa): C:\Users\John/.ssh/id_rsa_other_account
Now go check in .ssh/ and you should see the id_rsa_other_account and id_rsa_other_account.pub files AND the original id_rsa and id_rsa.pub files.
Add the id_rsa_other_account to your OTHER GitHub account’s SSH keys like we did above. (see Github screenshot)
Now add the second SSH key into the SSH-Agent with:
$ ssh-add \path\to\.ssh\id_rsa_other_account
Create SSH config file
Create the config file located in .ssh/ It doesn’t exist by default so you’ll have to create it. /Users/John/.ssh/config (no extension on config)
# Default Account (TruthSeekers for me.) Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa # Other account (XYZ co) Host github-other HostName github.com User git IdentityFile ~/.ssh/id_rsa_other_account
Test your multiple GitHub account setup!
We’re only going to bother with the “other” account, since you’ve most likely been using the one-account setup for a while and already know that part.
Create a GitHub repository and get to the “instructions” for setting up the repo locally.
Now you’ll need to replace the “host” portion of the remote add origin string with the “Host” from the config file. If you look at the previous part of the post, you’ll see that the .ssh/id_rsa_other_account is attached to the
Host github-other and the
id_rsa_other_account.pub goes to my secondary GitHub account. If I want to push to that GitHub account I need to run the git remote add command like this:
git remote add origin git@github-other:fastpenguin91/testotheraccount.git
After this, commit like normal, assuming you have a change to commit:
git add . git commit -m "testing 2nd acct" git push origin master
Now the only thing you have to do on each repository is set the user/email you’d like to use on that repo.
git config user.name "John C" git config user.email "firstname.lastname@example.org"
That’s it![optin-monster-inline slug=”bynnpzyzkyxgkwjxuehp”]