Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamazad/a75fb50e4c36f861db195d606f7d6028 to your computer and use it in GitHub Desktop.
Save adamazad/a75fb50e4c36f861db195d606f7d6028 to your computer and use it in GitHub Desktop.
Using multiple GitHub deploy keys on a single server with a single user

Using multiple GitHub deploy keys on a single server with a single user

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 git@repo-1.github.com:username/repo-1.git

You can test your SSH keys are set up like so:

ssh -T git@repo-1.github.com

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.

Further Reading:

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