When using the same computer for different purposes, it is likely that we want to use different SSH keys. For example, we may want to push to GitHub or GitLab with a specific SSH key using our work key whereas on other repositories we may want to use our personal key.
On Windows, there is fortunately an easy way to specify which key we want to use. However, we need a small setup first, so let us get to it.
Setup
First, let us assume that we already have at least two different SSH keys.
For example:
id_rsa
andid_rsa.pub
(name of default key)personal_id_rsa
andpersonal_id_rsa.pub
In this case, by default we want to use our work SSH key.
Therefore we name our work SSH key using the default name. For the personal one, we will prepend personal_
.
These keys are all located under C:\Users\<username>\.ssh
.
Configuration
In the same .ssh
folder there must be a config
text file (yes, without extension).
If the file does not exist, we can create an empty config.txt
file and remove the extension.
Following, we proceed to create our entries for configuring which key we want to use in which case.
# Work key for GitHub
Host work.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# Personal key for GitHub
Host personal.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/personal_id_rsa
# Personal key always for GitLab
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/personal_id_rsa
Let me explain what these three examples do:
- When cloning a repository from GitHub, instead of using the normal address like
git@github.com:xyz/xyz.git
, we should instead usegit@work.github.com:xyz/xyz.git
. This will automatically tell GIT we want to use the work SSH key for this repository. - Similarly, we can use
git@personal.github.com:xyz/xyz.git
to specify that we want to use the personal SSH key. - If we just want to associate a service with a specific SSH key, we can directly do it if we do not prepend anything as we did before with
work.github.com
orpersonal.github.com
. In this case we havegitlab.com
and all cloned repositories will use the personal SSH key.
Summary
And that is it! We have seen how to handle multiple SSH keys on Windows and how to easily specify which one to use when cloning GIT repositories.
Thanks for reading!
If you found this article interesting, share it!