Handling Multiple SSH keys

Ashutosh Kumar Singh
Nov 3 · 2 min read

Life was going awesome until the problem came of using a key for different repository while using another set of keys for another set of a repository. Which brings me to the problem of handling multiple SSH keys. So let’s start the process of understanding how to manage them.

Firstly you need to understand that to manage the ssh keys you need to have a config file. That can be found at the `~/.ssh` folder with the name `config`. It’s not present initially, In that case, one needs to create it by the following command.

touch ~/.ssh/config

After this, you need to set the permission for the file.

chmod 700 ~/.ssh/config

The format of the config file is given bellow

Host hostname1

SSH_OPTION value

SSH_OPTION value

Host hostname2

SSH_OPTION value

Since we would be using the config for the repository cloning and accessing of the Github. Let’s suppose we have two sets of keys. Each set includes a private and public key. Filename of the each set is id_rsa1 & id_rsa2. Hence the updated config file looks like this.

Host github.com

HostName github.com

IdentityFile ~/.ssh/id_rsa1

Host office-github.com

HostName github.com

IdentityFile ~/.ssh/id_rsa2

Now if you want to clone the repository using the id_rsa2 key. You need to make some changes while writing the git URL. For example, let’s suppose the original URL you need to clone is

git@github.com:juggernaut451/fullstack-tutorial.git

This would be using the id_rsa1 key to clone the repository.

If you want to clone with id_rsa2 key. You need to change the domain of the repository URL.

So the new URL would be ` git@office-github.com:juggernaut451/fullstack-tutorial.git `.

As ` github.com ` has been replaced with ` office-github.com `.

Happy SSHing :)

Originally publish here.

Ashutosh Kumar Singh

Written by

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade