Configuring different SSH keys per host

Aug 14, 2019 - 1 min read

For increased security or any other reason, you can have different SSH keys configured for different hosts.

Generate keys

ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa_personal
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa_work

Tip: On Windows, ssh configuration and keys are stored in C:\Users\[user]\.ssh

Configure hosts

Edit (or create) a file named ~/.ssh/config and see example configuration below:

Host gitlab.com
	HostName gitlab.com
	IdentityFile ~/.ssh/id_rsa_personal

Host github.com
	HostName github.com
	IdentityFile ~/.ssh/id_rsa_personal

Host bitbucket.org
	HostName bitbucket.org
	IdentityFile ~/.ssh/id_rsa_work

Host vs-ssh.visualstudio.com
	HostName vs-ssh.visualstudio.com
	IdentityFile ~/.ssh/id_rsa_work

It’s also possible to configure multiple ssh keys for a single host using aliases.