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.
First, let us assume that we already have at least two different SSH keys.
id_rsa.pub(name of default key)
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
These keys are all located under
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
firstname.lastname@example.org:xyz/xyz.git, we should instead use
email@example.com:xyz/xyz.git. This will automatically tell GIT we want to use the work SSH key for this repository.
- Similarly, we can use
firstname.lastname@example.org:xyz/xyz.gitto 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
personal.github.com. In this case we have
gitlab.comand all cloned repositories will use the personal SSH key.
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!