Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pcambra/816692b5968ec75f41d2a5ce32cf607c to your computer and use it in GitHub Desktop.
Save pcambra/816692b5968ec75f41d2a5ce32cf607c to your computer and use it in GitHub Desktop.
How to configure multiple deploy keys for different private github repositories on the same computer

This is probably a common issue for people who wants to deploy serveral repository on one VPS or VM.

This is not going to be rocket science.

The first step is to generate your first ssh key, for this type the following command line in your terminal: ssh-keygen -t rsa -b 4096 -C "your@email.com" When the command CLI is asking you if you want to use a passphrase you might want to press ENTER in order to don't have to type it everytime you will want to pull your repository.

Now copy the content of your public key and add it to Github in your first repository (Settings > Deploy keys). For example on debian it might be: cat /home/debian/.ssh/id_rsa.pub

Now we can clone our repository via ssh with the following command: git clone git@github.com:YourGitHubName/repo1.git

The next step is to rename our ssh keys and configure them.

To do this you need to go to the following folder: ~/.ssh by typing: cd ~/.ssh. And now rename your keys:

mv id_rsa id_repo1_rsa
mv id_rsa.pub id_repo1_rsa.pub

Now we need to configure our keys in the repo and ssh, to do this type the following command line:

nano ~/.ssh/config

And add the following content:

Host repo1
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_repo1_rsa
  IdentitiesOnly yes

In our git folder open the git config and edit like this:

nano .git/config
[remote "origin"]
#	url = git@github.com:YourGitHubName/repo1.git
	url = "ssh://git@repo1/YourGitHubName/repo1.git"

Congratulation you can now test by typing: git pull origin master and see that our conifguration is working.

Now you can repeat the following steps for your other repositories and start by generating a new ssh key to use as a deploy key in another repository.

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