DEV Community

Cover image for SSH Config for Multiple SSH Authentication
Mikhael Esa
Mikhael Esa

Posted on

SSH Config for Multiple SSH Authentication

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
Enter fullscreen mode Exit fullscreen mode

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 ~\.ssh\my_gitlab_ssh
    IdentitiesOnly yes

Host github.com
    HostName github.com
    User git
    IdentityFile ~\.ssh\my_github_ssh
    IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

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 👋

Top comments (0)