Using Different SSH Keys for Different Hosts

It’s possible to configure SSH to use different RSA keys for different hosts. On this short post I’ll show how to set it up so git uses the right key for each repository.

As a guiding example for this post, let’s assume you have a default key. This is the key you use for your repositories in GitHub and perhaps some other websites. You’ve just got hired by Vandelay Industries, a company that uses a self-hosted Bitbucket server. You don’t wish to mix your personal stuff with the company’s stuff, so you create a new key:

$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/vandelayid -C "[email protected]"

Now, edit the ~/.ssh/config file and associate the new key with the company’s hostname. If this file doesn’t exist, create it.

Host vandelay
    IdentityFile ~/.ssh/vandelayid
    IdentitiesOnly yes

That’s it. Now, whenever you clone/pull/push from a repository in, git will use the correct key for authentication.