Skip to content

Instantly share code, notes, and snippets.

@cognivator
Last active March 28, 2024 19:32
Show Gist options
  • Save cognivator/91e4f8cbf41cf7fe1b9a31f4d0ec56c7 to your computer and use it in GitHub Desktop.
Save cognivator/91e4f8cbf41cf7fe1b9a31f4d0ec56c7 to your computer and use it in GitHub Desktop.
Manage multiple SSH keys on MacOS

Situation

There are plenty of references for generating and using SSH keys on MacOS. Google for answers and they're everywhere. What is less obvious is how to use multiple SSH keys, especially for the same host, e.g. GitHub. I had multiple SSH keys and a ~/.ssh/config file, but certain of the credentials weren't being found.

Solution

The nuggets of goodness I found describe how to properly use ~/.ssh/config - (here, here, here, here... +many more). The key is actually using the resulting Host aliases!

Go figure, huh? 🤦

Example: Github - SourceTree

In concrete terms, and skipping all the SSH generation tasks — references abound — the key insight in code is...

Add the host-key mappings to ~/.ssh/config,

Host github-alias-1
  HostName github.com
  ...
  IdentifyFile ~/.ssh/key1

Host github-alias-2
  HostName github.com
  ...
  IdentifyFile ~/.ssh/key2

Then anywhere you would normally reference the url host directly... don't. Use the Host alias from the config file instead,

  • NO: git@github.com:username/repository.git
  • YES: git@github-alias-x:username/repository.git

It's magical. 🪄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment