How to configure multiple SSH keys in GIT

Gurinderpal Singh Narang
2 min readFeb 3, 2024

--

Why there is a need for multiple SSH keys:

Working on projects hosted on different platforms or services (GitHub, GitLab, Bitbucket, Azure DevOps, etc.) may necessitate separate SSH keys. Each service requires its own SSH key for secure authentication and access.

This is what happened recently to me, I encountered a challenge while managing SSH keys for Git repositories. Initially, I configured an SSH key named id_rsa for a specific project, which is hosted on GIT. However, when I acquired a new project hosted on Azure, I needed a distinct SSH key, which I named as id_rsa_azure. Despite this, attempting to clone the Azure repository resulted in an “access error”. It seems that GIT still defaults to the original id_rsa key during the cloning process.

So, to clone a Git repository using a specific SSH key, you can follow these steps:

1. Make sure the SSH key is added to your SSH agent:

Before you proceed, ensure that your SSH key (id_rsa_azure.pub) is added to your SSH agent. You can use the following command to add it:

ssh-add ~/.ssh/id_rsa_azure

This assumes that your private key is named id_rsa_azure (without the .pub extension). The ssh-add command adds the private key to the SSH agent.

2. Configure Git to use the specific key for the repository:

You can configure Git to use a specific key for a particular repository by editing the SSH configuration file.

Open or create the ~/.ssh/config file using a text editor:

nano ~/.ssh/config

Add the following configuration:

Host https://dev.azure.com/
HostName https://dev.azure.com/
User git
IdentityFile ~/.ssh/id_rsa_azure

Replace https://dev.azure.com with the actual hostname of the Git repository if it's not on Azure.

3. Clone the repository:

Now you can clone the Git repository using the configured key:

git clone git@github.com:username/repository.git

Make sure to replace username/repository.git with the actual path to the Git repository.

This setup ensures that when you interact with the specific Git repository, the id_rsa_azure private key will be used. If you have multiple repositories with different SSH keys, you can configure each repository in the ~/.ssh/config file accordingly.

Thanks for Reading!!

--

--