TIL: Using Multiple SSH Keys

Armno's avatar
Published on August 14th, 2019
By Armno P.

Today I learned how to use multiple SSH keys on one computer.

At work, all projects are hosted on our company’s internal git server running GitLab CE. while I put all my personal projects on GitHub.

I wanted to use different keys for GitLab and GitHub. So that I can have different passphrases for each SSH key, making it a bit more secure.

Or even different SSH key type on GitHub and GitLab. From the docs, GitHub uses RSA key while GitLab recommends ED25519 over RSA.

I use RSA key for both services for now.

Generating SSH Keys

I generated 2 SSH key pairs. The default one is for company’s work at GitLab.

$ ssh-keygen -t rsa -b 4096 -C "work-email@company.com"

and another pair for GitHub stored at ~/.ssh/github.

$ ssh-key -t rsa -b 4096 -C "personal@email.com" -f ~/.ssh/github

In my ~/.ssh/ folder, I have 2 pairs of SSH keys.

$ ls -1 ~/.ssh
id_rsa
id_rsa.pub
github
github.pub

Update SSH Config File

Now I have to tell SSH client to use the different private key for GitHub.

In ~/.ssh/config file, add a new configuration for GitHub.com:

host github.com
  identityfile ~/.ssh/github

This means every time it has to make a connection to github.com, use another private key at ~/.ssh/github instead of the default one. This includes git as well.

I added the public key ~/.ssh/github.pub to my GitHub account settings page.

Adding a new SSH Key in GitHub

and make a test connection to verify that it works.

$ ssh -T git@github.com
Hi armno! You've successfully authenticated, but GitHub does not provide shell access.
Tags:

Related posts