Handling multiple SSH keys

Created at 2019-11-03 Updated at 2019-11-03 Tag ssh, shell

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

1
2
3
4
5
6
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.

1
2
3
4
5
6
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 :)

Table of Content

Site by Ashutosh Kumar Singh using Hexo & Random

Traveller - Developer - Foodie - Biker - Cyclist - Rider - Technocrat