SSH Default Identity Protip

When SSH isn't picking up your default configuration, here's what probably went wrong.

November 28, 2016 - 1 minute read -
ssh

I was trying to set up different SSH keys and identities, because I have two GitLab accounts (one for work, one for play). However, SSH would not pick the correct default identity/key for all my other SSH connections. Config file below, can you spot what goes wrong?

IdentityFile ~/.ssh/id_rsa_work # Specify default identity

Host staging # Staging instance where I'm working
	HostName 12.34.56.78
	Port 22
	User dubsman
	IdentityFile ~/.ssh/id_rsa_work

Host production # Production instance where I'm working
	HostName 01.23.34.56
	Port 22
	User dubsman
	IdentityFile ~/.ssh/id_rsa_work

Host dubsgit # My second gitlab identity
	HostName gitlab.com
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/id_rsa_home
	User git

Yup, putting the IdentityFile at the top will set it as default for all connections, no matter what follows. The reason being obviously that the daemon parses it from top to bottom looking for matches. The obvious fix is of course to just place the general IdentityFile at the bottom of the config file. Duh.