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.
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? 🤦
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. 🪄