Use Multiple SSH keys for git repositories in Ubuntu

Posted on June 24th, 2019

Every Programmer in this world knows that Git is one of the best version control systems available in the market. And there are companies like Github and Bitbucket that provides us an interface to manage our git repositories remotely. It is very much possible that you want to set up multiple ssh keys for multiple Git hosts like Github, Gitlab, or Bitbucket.

And the good news is, It is possible to set up an SSH config file in Ubuntu to achieve this goal. Let’s say You are using Github and Bitbucket to host your git repositories. And you want to use different SSH keys for both the services. Our goal is to set up an environment where our machine will automatically use Github’s SSH key while dealing with a Github repository and Bitbucket’s SSH key while dealing with a Bitbucket repository.

It is effortless to create this environment in Ubuntu. We have to create an SSH config file that will hold all the information like Hosts, SSH keys, users, and much more. For this example, we will set up an SSH configuration file for following git hosts and SSH keys.

HOST 1
Host: bitbucket.org
Identity File (Private key) path: ~/.ssh/bitbucket
User: git

HOST 2
Host: github.com
Identity File (Private key) path: ~/.ssh/github
User: git

So, let’s get started with the actual tutorial. The configuration file we are going to create will not only serve git service providers. You can also put different SSH keys for different SSH hosts, and it will still work.

Setup multiple SSH keys for different Git hosts

It is a straightforward process because we just have to create a configuration file and define the hosts with keys. Assuming that you are in your home directory, execute the following command to create and open ~/.ssh/config file in edit mode.

user@localhost:~$ touch ~/.ssh/config
user@localhost:~$ nano ~/.ssh/config

Once you are in, You have to append the following block of configuration in the config file we just created.

Host gitHost.example
 Hostname gitHost.example
 IdentityFile ~/.ssh/gitHost
 user git

Do not forget to replace Host, Hostname, IdentityFile, and user in the block.

So, Here is how it works. Whenever we will try to create an SSH connection with gitHost.example as a git user, our machine will look for the host and user in our ~/.ssh/config file. If it finds the similar host and user, It will use the key given in the IdentityFile directive of the same block of configuration. In this case, whenever we will try to create an SSH connection with gitHost.example, our machine will use ~/.ssh/gitHost key to authenticate.

In our case, as we want to use multiple git hosts, we have to add two such blocks in the config file. Based on the example information given above for Github and Bitbucket, Here is how our config file should look like.

Host bitbucket.org
 HostName bitbucket.org
 IdentityFile ~/.ssh/bitbucket
 user git

Host github.com
 HostName github.com
 IdentityFile ~/.ssh/github
 user git

Once you have added all your configuration blocks, press CTRL+X followed by Y followed by Enter to save the config file. The configuration will be activated instantly, so there are no extra steps we have to perform to apply changes.

Now, we can push and pull our commits without explicitly defining our SSH key file path while using different SSH keys for different Git hosts.

In this configuration file, you can add as many hosts as you want with different identity files and users. You can also use the same configuration file to define Host, User, and IdentityFile for SSH Hosts. So, this is how you can set up a configuration file to use Multiple SSH keys for different git.

I hope you enjoyed this tutorial and if you are facing any issue following tutorial, Please let us know. We are happy to help!

Leave a Reply