Configuring multiple SSH keys with Git and Azure DevOps

I had to generate a new SSH key pair for Azure DevOps recently. The reason was that I got a message in Azure DevOps telling me that I tried to add a duplicate key. That was correct, but I do not want to delete the SSH key I had to replace with a new one. Therefore I created a new SSH key pair that I could use in Azure DevOps.

The process here is not Azure DevOps specific, but this is how your can have multiple SSH keys for different host names when using Git. If you are using some other Git repository (Gitlab, Github, etc.), you can then just change the host name in the config file or add more to the config file if that is needed.

The first thing that happend was that when I tried to push or pull from the git repository, it automatically used the "default" SSH key on my computer /Users/myusername/.ssh/id_rsa, I need git to use a specific key for Azure DevOps /Users/myusername/.ssh/id_rsa_azuredevops. After I did searching for a solution a little bit, I found out that the git client uses the SSH config file to decide which key that should be used for each host. Default is the id_rsakey. On my machine, I did not have a SSH config file, /Users/myusername/.ssh/config. I just created that file and added the following.

Host ssh.dev.azure.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa_azuredevops
  IdentitiesOnly yes
  User git
  
Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa
  IdentitiesOnly yes
  User git

After adding this file, I could communicate with the Azure DevOps repositories with the correct SSH key again.

Teis Lindemark

Read more posts by this author.