Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github deploy keys: How do I authorize more than one repository for a single machine?

I have a host, call it rob. I used ssh-keygen on rob to get a public key, which I gave to github in the add a new deploy key screen for repository cheech. Now I want to deploy chong on rob as well. But if I go to the add new deploy key screen for repository chong on github, and paste in the public key I generated on rob it says key already in use. I thought, if the key was in use, I could clone chong on rob but that says permission denied.

So clearly this is more complicated than I thought and it involves having multiple keys or something. What should I do to clone chong on rob?

like image 930
user1552512 Avatar asked Jul 25 '12 18:07

user1552512


People also ask

Can I use same deploy key multiple repositories?

You can't use the same deploy key in more than one repo, so the workaround becomes adding that key to their user account (or a dedicated machine account). Taking the least path of resistance, most users will add it to their own account resulting in a greater security risk.

How do I deploy multiple keys in GitHub?

GitHub allows you to attach a deploy key to any of your repositories. However, each repo must have its own unique key. If you're deploying multiple GitHub repos on a single machine, that means you'll need to set up multiple ssh keys for that machine. The easiest way to achieve this is to leverage ssh's host config.

Can you have multiple GitHub SSH keys?

For instance, you can run an Organization's GitHub account and another one for your personal projects all on the same computer. In this article, you will learn how to use multiple SSH keys for different GitHub accounts. While working with two different GitHub accounts, you must set them up using an SSH key.

How do I authorize a SSH key in GitHub?

In the "Access" section of the sidebar, click SSH and GPG keys. Next to the SSH key you'd like to authorize, click Enable SSO or Disable SSO. Find the organization you'd like to authorize the SSH key for. Click Authorize.


1 Answers

Once a key has been attached to one repo as a deploy key, it cannot be used on another repo. If you're running into this error while setting up deploy keys, then you'll need to modify your remote and set up your ~/.ssh/config file to use a non-existent github.com hostname that ssh will be able to use to pick the correct ssh deploy key for your repository.

# first we remove the origin $ git remote -v origin  [email protected]:username/foo.git (fetch) origin  [email protected]:username/foo.git (push) $ git remote rm origin  # here we add a new origin using a host nickname called # foo.github.com that we will reference with a Host stanza in our # ~/.ssh/config to specify which key to use with which fake hostname. $ git remote add origin [email protected]:username/foo.git $ git remote -v origin  [email protected]:username/foo.git (fetch) origin  [email protected]:username/foo.git (push) 

Generate the deploy key for your repository and name it something reasonable like:

$ ssh-keygen -t rsa -f ~/.ssh/id_rsa-foo -C https://github.com/username/foo Generating public/private rsa key pair. Enter passphrase (empty for no passphrase):  Enter same passphrase again:  Your identification has been saved in /home/username/.ssh/id_rsa-foo. Your public key has been saved in /home/username/.ssh/id_rsa-foo.pub. The key fingerprint is: c0:ff:ee:34:24:11:5e:6d:7c:4c:b1:a0:de:ad:be:ef https://github.com/username/foo The key's randomart image is: +--[ RSA 2048]----+ |  E   o..o.oo.   | | M o o o .+CoW   | |  + o = o. ..    | | .   . +         | |        S        | |       o .       | |        +        | |       . o       | |        ..o.     | +-----------------+ 

Once you've added the deploy key you will then need to add the following stanza to your ~/.ssh/config file:

Host fake-hostname-foo.github.com     Hostname github.com     IdentityFile ~/.ssh/id_rsa-foo 

Now you can test it with:

$ ssh -T [email protected] Hi username! You've successfully authenticated, but GitHub does not provide shell access. 
like image 81
aculich Avatar answered Sep 28 '22 07:09

aculich