Within GitHub it is possible to set up two types of SSH key - account level SSH keys and and repository level SSH keys. These repository level SSH keys are known in GitHub as deploy keys.
Deploy keys are useful for deploying code because they do not rely on an individual user account, which is susceptible to change, to “store” the server keys.
There is, however, an ‘issue’ with using deploy keys; each key across all repositories on GitHub must be unique. No one key can be used more than once. This becomes a problem when deploying to repositories to the same server with the same user. If you create two keys, the SSH client will not know which key to use when connecting to GitHub.
One solution is to use an SSH config file to define which key to use in which situation. This isn’t as easy as it seems.. you might try something like this:
Host github.com
HostName github.com
IdentityFile ~/.ssh/repo-1-deploy-key
However, how would you add the second deploy key? The Host
would be the same. The solution is to add a subdomain to the GitHub URL:
Host repo-1.github.com
IdentityFile ~/.ssh/repo-1-deploy-key
Host repo-2.github.com
IdentityFile ~/.ssh/repo-2-deploy-key
You’ll also need to update your remote origin URLs:
cd /path/to/repo-1
git remote set-url origin [email protected]:username/repo-1.git
You can test your SSH keys are set up like so:
ssh -T [email protected]
If all is well, you’ll see something like the following:
Hi username/repo-1! You've successfully authenticated, but GitHub does not provide shell access.
note for an improvement:
the alias you define does not actually have to be a subdomain of github.com
it can even be a bare word, like
alias_for_repo_x
.that avoids the "timeout" issue, just giving you a dns error instead.
(the timeout happens because github has a wildcard record resolving to an ip that does not respond to ssh connection requests.)