Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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:

@racerxdl

This comment has been minimized.

Copy link

commented May 15, 2017

That doesn't work. I get timeout for repo-1.github.com

@philfree

This comment has been minimized.

Copy link

commented Jun 7, 2017

same

@stepharr

This comment has been minimized.

Copy link

commented Jun 12, 2017

Works for me. @philfree is your config file (~/.ssh/config) set like above?

@vitalyu

This comment has been minimized.

Copy link

commented Jun 22, 2017

@theonestep4 same timeout error. ~/.ssh/config is good, ssh with debug doesn't show any errors, only timeout

@jamesmcfadden

This comment has been minimized.

Copy link
Owner Author

commented Jul 3, 2017

I've just had to setup a new environment with these instructions and I'm also getting the timeout issue.

Adding the following line to my SSH config sorted it for me:

HostName github.com

So your config should look like:

Host github.com
 HostName github.com
 IdentityFile ~/.ssh/repo-1-deploy-key

I've updated the Gist to reflect.

@racerxdl
@philfree
@vitalyu

@TimothySealy

This comment has been minimized.

Copy link

commented Jul 20, 2017

The trick is to define aliases for the different hosts and then using the alias in your git clone. After that it is git as usual.
Details can be found here: https://www.justinsilver.com/technology/github-multiple-repository-ssh-deploy-keys/

@ththvseo

This comment has been minimized.

Copy link

commented Oct 22, 2017

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.)

@imikejackson

This comment has been minimized.

Copy link

commented Dec 8, 2017

OR... GitHub could just allow the use of a Deploy Key across multiple Repositories. GitLab certainly does.

@njulsrud

This comment has been minimized.

Copy link

commented Dec 12, 2017

For people getting the error of a connection timeout you are going to have to create a cname to the A record of your github instance OR modify the hosts file on your node running the git clone.

@philfree
@vitalyu
@racerxdl

@jaymehtasa

This comment has been minimized.

Copy link

commented Feb 28, 2018

@TimothySealy thank you, alias approach works like charm!

Avoid using sub-domain approach.

@jamesmcfadden Please consider updating your gist as sub-domain approach not working anymore, it keeps giving timeout error.

@NickWoodhams

This comment has been minimized.

Copy link

commented Mar 5, 2018

@TimothySealy thank you, your approach worked where the original one did not.

For those of you interested in how the new .git/config file looks with the alias, check out mine:

~/.ssh/config:

Host myalias github.com
Hostname github.com
IdentityFile ~/.ssh/id_rsa

Host myalias2 github.com
Hostname github.com
IdentityFile ~/.ssh/id_rsa2

myrepo/.git/config:

[core]
	repositoryformatversion = 0
	filemode = false
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = git@myalias:NickWoodhams/myrepo.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master

myrepo2/.git/config:

[core]
	repositoryformatversion = 0
	filemode = false
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = git@myalias2:NickWoodhams/myrepo2.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
@jjyr

This comment has been minimized.

Copy link

commented Mar 24, 2018

Add HostName github.com option to each Host can solve the timeout issue.

Host repo-1.github.com
 HostName github.com
 IdentityFile ~/.ssh/repo-1-deploy-key
Host repo-2.github.com
 HostName github.com
 IdentityFile ~/.ssh/repo-2-deploy-key
@hisankaran

This comment has been minimized.

Copy link

commented Jul 23, 2018

We can also add user to the config
git clone repo-1-account:hisankaran/repo-1.git

Host 		repo-1-account
HostName 	github.com
User 		git
Identityfile 	~/.ssh/github/repo-1/id_rsa

git clone repo-2-account:hisankaran/repo-2.git

Host 		repo-2-account
HostName 	github.com
User 		git
Identityfile 	~/.ssh/github/repo-2/id_rsa
@zgia

This comment has been minimized.

Copy link

commented Mar 21, 2019

@NickWoodhams thx a lot, good implementation.

@stevecondylios

This comment has been minimized.

Copy link

commented Oct 17, 2019

@NickWoodhams lifesaver!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.