How to manage multiple GitHub accounts on one computer with SSH keys

gniches

Guilherme Niches

Posted on March 10, 2023

How to manage multiple GitHub accounts on one computer with SSH keys

In our journey through the world of programming it is very common to have to create more than one account on Github.

Either to have a personal account, which we use for our own projects, and a professional account linked to the company where we work. Either to have separate personal accounts for different purposes.

The question is, how can we use two or more different Github accounts on a single computer?

One of the possible answers is: using SSH keys.


SSH Keys


What is it?

SSH (Secure Shell) or Key Pair is a tool of remote access with focus on security, it's the means of identification between your computer and an SSH server using public encryption key authentication method.

One of it's advantages is that it does not transmit information such as login and password, so if your request is intercepted by malicious agents, they will not have access to your credentials.

Note: It's not the purpose of this article to explain in depth how the SSH keys works and it's types of encryption. However, at the end of the article I will put some links that help us to understand the concept in more depth.

Note 2: This article will focus on Linux/Mac commands, if you use Windows you can install Git Bash to emulate a Unix Shell. Git Bash is a build-in package of Git for Windows. Or, in some modern Windows environments is possible to use Windows Subsystem for Linux (WSL) to reproduce a Linux system inside Windows. After installing Git Bash or WSL you can follow the below steps to reach the same result that a Linux/Mac user.


Hands On

Okay, so now we know a little more about SSH keys and why use it let's start to configure our SSH to management two or more Github account.

We will reproduce the most common scenario: When you have a personal GitHub account for your personal projects and a company/work account.

I will suppose that you have the basic knowledge of terminal commands such as navigation between directories, create and edit files, etc.

  • ssh directory

The ssh directory is usually located in the main directory of
user: /home/yourUser

Is a occult directory, so, if you want list the directory, run the ls command with the -a flag to list all files and directories including the occult ones.

Open your terminal and type the command below to access ssh directory.

cd ~/.ssh
Enter fullscreen mode Exit fullscreen mode
  • Create SSH Keys

Now, we will create our first SSH key for our personal account. In terminal run the following command:

ssh-keygen -t rsa -C "your-personal@email" -f "github-personal"
Enter fullscreen mode Exit fullscreen mode

Replace "your-personal@email" by your personal e-mail account. You can replace the "github-personal" too, but the name you put here will be used later, so take this in mind in the next steps.

Repeat the same command before, changing the "your-personal@email" to your work e-mail account, to create a new SSH key to your professional account.

ssh-keygen -t rsa -C "your-work@email" -f "github-work"
Enter fullscreen mode Exit fullscreen mode
  • Add the SSH Keys to your SSH-agent

After create our two SSH keys we will add this keys to the SSH-agent. Type in terminal the following commands to add our personal and work accounts.

ssh-add ~/.ssh/github-personal
Enter fullscreen mode Exit fullscreen mode

ssh-add ~/.ssh/github-work
Enter fullscreen mode Exit fullscreen mode

As said before, the above commands will be slightly differents if you set another value to the "github-personal" or "github-work" in the creation command.

To test if the SSH keys were correctly added to the SSH agent, run the below command to list your active keys by fingerprint:

ssh-add -l 
Enter fullscreen mode Exit fullscreen mode

The output should be similar to this:

3072 SHA256:a lot of random characters your-personal@email (RSA)

3072 SHA256:a lot of random characters your-work@email (RSA)  
Enter fullscreen mode Exit fullscreen mode
  • Add to Github account

Here we have two options.

1) We can set our two SSH keys to our personal account, in this way, you can access your personal account by the two SSH keys (personal and work keys). The same is valid if you set the two keys to the work account.

2) Or, we can set just the personal key to personal account and the work key to the work account, in this way, your personal key will not have access to the your work account and vice versa.

I personally, prefer to keep the things separated. So I will follow the second option.

The next step will be copy your public key and link to your Github account. In Mac, type the below command to copy the public ssh key.

pbcopy < ~/.ssh/github-work.pub
Enter fullscreen mode Exit fullscreen mode

pbcopy < ~/.ssh/github-personal.pub
Enter fullscreen mode Exit fullscreen mode

In Linux, you need use some tool like xclip or just open the created files with any text editor or cat command and copy all the content of files.

The content of this files should be similar to this:

ssh-rsa [...long list of random characters]= your-work@email
Enter fullscreen mode Exit fullscreen mode

Now, you can go to Github webpage, signup in your account and access settings in profile menu.

GitHub Profile Settings

In settings page, go to SSH and GPG Keys in navigation menu.

SSH and GPG keys

Here, will be listed all SSH keys that you already have associated to your account.

Click in the "New SSH key" green button and you will be redirect to the page below.

SSH keys / Add new

In this page we will paste in the Key field the SSH public key that we copy in the last step. Set a Title to this key and click in the green button "Add SSH key". Back in the previous page, the new created SSH key should be listed.

  • Create Config file

In terminal, create a new file called config.

touch ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

In Mac, type this command to open the config file created:

open -e ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

In Linux, you can use vim, nano or other text editor that you like.

nano config
Enter fullscreen mode Exit fullscreen mode

Inside config file paste the following:

# Personal GitHub account
Host github.com-personal
 HostName github.com
 User git
 IdentityFile ~/.ssh/github-personal

# Work GitHub account
Host github.com-work
 HostName github.com
 User git
 IdentityFile ~/.ssh/github-work

Enter fullscreen mode Exit fullscreen mode

Save the file and exit.

  • Make default account global

Set one of the accounts as default. In terminal, type:

git config --global user.name "personal"
Enter fullscreen mode Exit fullscreen mode
git config --global user.email "your-personal@email"
Enter fullscreen mode Exit fullscreen mode
  • Clone repository with the secondary account

I will clone a repository from my professional account.

Go to your Github profile, open a repository, click "Code" button, select SSH tab and copy the SSH link. (Image below)

Clone by SSH

Here we got a point of attention: the default SSH link provided by Github has this format:

git@github.com:repo-owner-user-name/the-repo-name.git
Enter fullscreen mode Exit fullscreen mode

What we will do is a little change in this format adding the Host as we defined in the config file of our SSH, the SSH link will be similar to this:

git@github.com-work:repo-owner-user-name/the-repo-name.git
Enter fullscreen mode Exit fullscreen mode

So, to clone the repository the complete command will be:

git clone git@github.com-work:repo-owner-user-name/the-repo-name.git my-repo
Enter fullscreen mode Exit fullscreen mode

"my-repo" is just the name of the directory where the repository will be clone inside, you can put the name you want, or putting nothing to clone the repository inside the directory where you are.

Go to my-repo directory, type:

cd my-repo
Enter fullscreen mode Exit fullscreen mode

If you run: git config user.name in terminal, you will receive as answer the default global value configured, in my case, was the personal. The same is valid to git config user.email.

So, for this repository that was cloned from my work account I want to use my professional credentials. To do this, just type in terminal, inside the directory of the repository:

git config user.name "work"
git config user.email "your-work@email"
Enter fullscreen mode Exit fullscreen mode

This commands will be set your professional credentials to this specific repository from work and you will be able to perform git actions to this repository using your work account.

If you set your global credentials to the personal account and clone a personal repository, you don't need specify the user.name and user.email to this repository, because the default credential is your personal account and the git actions that you perform in this repository will be associated to your personal account. The same is valid if you set your work account as global and clone a work repository.


Concluding

We have seen how to create, configure and manage multiple Github accounts on one machine using SSH keys, this is very useful when we need different accounts for distinct purposes.

Reviews, suggestions and/or contributions to this article are encouraged and welcome.


References

What is SSH?

SSH Keys Concepts

Git SSH Keys

💖 💪 🙅 🚩
gniches
Guilherme Niches

Posted on March 10, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related