Multiple SSH keys for different online services

14 Dec 2019


After adding an ssh key to one's github /gitlab profile, managing repos becomes a whole lot easier. No more username/password prompts. Aside from social coding sites like github and gitlab, I have the need for ssh keys on a couple of other platforms too.

In the off chance my key gets compromised, access to everything gets lost. So, having multiple keys for various accounts help. Luckily, configuring the ssh client for this purpose is easy.

In this write-up, we'll be taking a look at how to add multiple keys to github and gitlab.

Generating keys

Generate a new key with ssh-keygen and when it asks for the name of the file, set it to anything other than id_rsa.

$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair. 
Enter file in which to save the key (/home/user/.ssh/id_rsa): id_rsa_github

Repeat the same procedure to generate another key with the name id_rsa_gitlab.

Adding keys to the service

Visit github and add the content of $HOME/.ssh/id_rsa_github.pub in your account's SSH and GPG keys section.

Do the same with the content of $HOME/.ssh/id_rsa_gitlab.pub, with your gitlab account's SSH Keys page.

Configuring our ssh client

Create or edit the config file $HOME/.ssh/config. The content of it is

Host github.com
        User git
        Hostname github.com
        PreferredAuthentications publickey
        IdentityFile /home/user/.ssh/id_rsa_github

Host gitlab.com
        User git
        Hostname gitlab.com
        PreferredAuthentications publickey
        IdentityFile /home/user/.ssh/id_rsa_gitlab

Testing connections

$ ssh -T [email protected]
Welcome to GitLab, @user!

$ ssh -T [email protected]
Hi user! You've successfully authenticated, but GitHub ...

Happy Hacking & have a great day!