SSH Config for Multiple SSH Authentication

RMAG news

TL;DR

Having multiple SSH keys is useful when you want to separate the purpose of each key, for example you want to have a different SSH keys for GitLab and GitHub for whatever reason. This can be achieved via the config file.

Creating Config File

To be able to have multiple ssh for different purpose, we need to create a config file inside the .ssh directory.

$ cd ~/.ssh
$ touch config
$ vim config

The commands above will directs us to the .ssh directory and creates the config file and then uses vim to edit the content. You can use other editor like nano, it’s up to you.

Now let’s edit the config file to look like this.

Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~.sshmy_gitlab_ssh
IdentitiesOnly yes

Host github.com
HostName github.com
User git
IdentityFile ~.sshmy_github_ssh
IdentitiesOnly yes

Now there you have it, a ssh config file for multiple SSH keys. You are not limited to only GitHub and GitLab, you can also use it for server authentication and many more.

~ Dadah 👋