SSH: Use Dedicated SSH Key for a Git Host

Normally, when using ssh for git (github, gitlab) you just need to upload a public key so that your terminal can authenticate. And normally that will be something like ~/.ssh/id_ed25519.pub because that’s your default key.

But what if you want to use a dedicated ssh key for some host, say corporate gitlab or github? That is possible.

Create a New Key

ssh-keygen.exe -t ed25519 -C "email for new key"

and when prompted to save the key, put it under different name, say ~/.ssh/work_gitlab.

That will create work_gitlab and work_gitlab.pub which is what you can use with a trivial configuration.

Configure SSH

If you don’t have ~/.ssh/config file, then create it, and add a section:

Host work-git
    HostName hostname.of.work-git
    User git
    IdentityFile ~/.ssh/work_gitlab
    IdentitiesOnly yes

Host specifies an alias for the host (read later), HostName is actual DNS name of the host.

Clone Using New Key

Normally when you clone something, the url will look like git clone ssh://git@hostname.of.work.git:2222/org/repo.git

but to use the new key just use the alias you’ve just created:

git clone ssh://git@work-git:2222/org/repo.git.

Thanks! You can always email me or use contact form for more questions/comments etc.