SSH Keys with Multiple GitHub Accounts On a Single Machine

Bill - 03 February, 2021
Reading time 2 mins

SSH key is very useful for us if we want to get rid of the need for typing in the username and password every time we make a Git push. But if we have multiple GitHub accounts on the same machine, we need to manage the SSH keys for each account.

1. Generating the SSH Keys

Before generating an SSH key, we need to check our existing SSH Keys by command: ls -la ~/.ssh Our existing public and private key pairs will list out (if exists).

Let's assume ~/.ssh/id_rsa is not available (if it's available we can reuse it). We can create a new SSH key by running: ssh-key -t rsa. Accept the default location by pressing enter when asked for the location to save the keys. We will get a private and public key at the default ssh location ~/.ssh/.

Above step is supposed to be used for our primary GitHub account, but for our another account, maybe our secondary account, we will create different SSH keys with a specific name.

$ ssh-keygen -t rsa -b 4096 -C "secondary@email.com" -f "id_rsa_secondary"
OR
$ ssh-keygen -t ed25519 -C "secondary@email.com" -f "id_rsa_secondary"

We have two different SSH keys created:

~/.ssh/id_rsa
~/.ssh/id_rsa_secondary

2. Add SSH Keys to GitHub

We already have the SSH keys ready. We will ask our GitHub accounts to recognize the keys we have created. You can follow GitHub Doc in case this process changes on GitHub website.

To copy the public key to the clipboard, run the following command, substituting for the correct filename. We will copy the primary key first.

$ xclip -selection clipboard < ~/.ssh/id_rsa.pub

Then log in to your primary GitHub account:

  • Go to Settings
  • Select SSH and GPG Keys
  • Click on New SSH key button, provide a title, and paste the key in the box.
  • Click Add SSH key and you are done

Repeat the above steps in your secondary GitHub account.

3. Registering the new SSH Keys

New keys need registered before they are useful. We have to register them with the ssh-agent on our machine. make sure it is running by running eval "$(ssh-agent -s)".

Once running, then add our new keys to the ssh-agent like so:

ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_secondary

4. Create a Git Config File

This config file is used to tell git to identify which SSH key is to to be used. It lives at ~/.ssh/config, if the config does not yet exist, we can easily create it:

touch ~/.ssh/config

Here is an example from my ~/.ssh/config file.

Host github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa

Host secondary.github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_secondary

Note that:

  • git URL that uses github.com should use id_rsa (my primary GitHub account)
  • git URL that uses secondary.github.com should use id_rsa _secondary (my secondary GitHub account)

5. Cloning Repository

Now that the config are in place and ready to use, we can try it by cloning the corresponding repositories. Make a note that when we clone a repo, we use the host names that we used in the SSH config file.

// uses our primary SSH key
$ git clone git@github.com:primary/repoName.git

// uses our secondary SSH key
$ git clone git@secondary.github.com:secondary/repoName.git

It is a little tricky but now we are done it. Just remember to clone the url with the correct Host format depending on which account you want to use.

# Linux - Blog posts