How to Set Up Multiple GitHub Accounts on a Single Computer with SSH Keys

Laura Malovich
4 min readMar 11, 2021

--

Photo by Luke Chesser on Unsplash

If you’re here it’s because multiple GitHub accounts would be useful to you. Maybe you’re a Freelancer. Maybe you just want to keep your other accounts separate. Here’s one method of managing multiple accounts using the same computer.

If you don’t know what SSH keys are here’s a quick explanation: SSH stands for Secure SHell, and it’s a cryptographic network protocol that allows users to securely access and manage resources on a remote computer. Check out how SSH works for more details.

If you’re using the Mac or Linux operating systems you can use the built-in terminal. If you’re using Windows then you can use Git Bash, a source control management system for Windows that you may already have installed.

Ready? Here we go:

STEP 1 — GENERATING THE SSH KEYS

In your terminal run the following script to generate your first SSH key:

ssh-keygen -t rsa

This generates a private key (“id_rsa”) and a public key (“id_rsa.pub”) at the ~/.ssh/ folder. The -t in the command stand for “type” which means there are other encryption types we could specify here. RSA is the type we are working with here.

To generate a second (or further) SSH keys you will want to change the directory to the ~/.ssh folder by running the following code. If you don’t, the second key might be created in the root directory instead of ~/.ssh/ folder.

cd .ssh

Then run the following:

$ ssh-keygen -t rsa -C "<comment>" -f "id_rsa_<second_key_name>"

The comment and second_key_name can be anything that makes sense to you. The comment may be useful to identify the key. If you do not add a comment with the -c option, the default comment on the key will be the user of the computer at the laptop’s name (e.g. — “user@hostname”). It can always be changed after creation.

You can create keys for as many accounts as you want.

STEP 2— CONFIGURING GITHUB

You should now have two different keys in the ~/.ssh/ folder.

~/.ssh/id_rsa
~/.ssh/id_rsa_<second_key_name>

Now you need to add the new ssh keys to their respective Github accounts. Note: you need the public keys within the “.pub” extension to do this!

  • Copy the key from the .pub file
  • Login to Github
  • Go to Settings
  • Select the SSH and GPG keys tab
  • Paste the key for the account into the key box and add an appropriate title
  • Click on Add Key
  • Repeat the above steps for each account.

STEP 3— ADDING THE KEYS TO THE SSH_AGENT

The ssh-agent is a program that tracks the ssh keys and their configurations. The agent uses the keys to automatically authenticate the corresponding remote servers.

The ssh-agent is usually configured to run automatically at login. If you don’t see it you can start the agent by running the following command:

eval "$(ssh-agent -s)"

Once it’s running use the ssh-add command to add the private keys to the ssh-agent.

ssh-add ~/.ssh/id_rsa ~/.ssh/id_rsa_<second_key_name>

STEP 4— UPDATING OR CREATING THE SSH CONFIG FILE

This requires you to create a new config file in the ~/.ssh folder. We can do that with the touch command. The touch command will create the file for you if it does not already exist. This file will contain the configurations for each account and their ssh keys.

touch ~/.ssh/config

You can then open the file in a text editor either manually or via the terminal. If you are using VS code you can use this command:

code ~/.ssh/config

Add the following lines to the file. Do NOT forget to edit the comments in the following script to match your account and username settings.

# <first account>
Host <github_username1>.github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

# <second account>
Host <github_username2>.github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_<second_key_name>

Just to understand what you did above: the “Host” is simply an identifier for the account. It’s a name of a particular configuration. You can name the Host whatever makes sense to you. However…

The HostName has to be “github.com”.

The User has to be “git”.

The IdentityFile has to be the path where you saved your private keys.

They are exact values used by the system to connect to Github on your behalf.

STEP 5— LET’S CONNECT TO GITHUB

Congratulations on making it this far! You are now ready to use multiple git accounts on the same computer. All the git commands you know should work the same as before except that you no longer need to you use the full URL from GitHub.

If you wanted to clone a repository from your first account you simply need the appropriate “account_name/repository_name.git” and your command would look like this:

git clone <user>@<Host>:account_name/repository_name.git

Remember above when you learned that the “Host” was simply a name for the configuration? That’s true, and you can use that to your advantage. Since the “user” is always “git” you can shorten the command to exclude everything before the “Host”.

For example: In the previous clone command becomes:

git clone <Host>:account_name/repository_name.git

Isn’t that cool? I think it is!

Thank you for reading my article! I hope that was helpful for you! Until next time.

--

--

No responses yet