Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage one only key per each git repository?

Tags:

git

github

ssh

I use git under two scenarios:

  • I use some Github repositories.
  • I'm currently working with OpenShift, which uses ssh and git for deployment.

First, I used ssh-keygen for generating a key which updated at OpenShift site. Such key is stored at ~/.ssh/ and created id_rsa and id_rsa.pub.

Then I started cloning a repository from Github, I once did ssh-keygen again and started pushing, it worked ok. Then I cloned another repository and started having problems:

I got problems when cloning to the second repository. Every time I try to push will show something like:

ERROR: Permission to diegoaguilar/cursoJava.git denied to diegoaguilar/cursoCannibalCreatures. fatal: The remote end hung up unexpectedly

But as it can be seen diegoaguilar/cursoCannibalCreatures isn't correct as it's another repository.

I even tried removing such repository directory, and cloning it again, same happened.

I already got under ~/.ssh:

config:

Host cursoJava Hostname github.com User git IdentityFile ~/.ssh/id_java  Host cursoCannibalCreatures Hostname github.com User git IdentityFile ~/.ssh/id_cannibal  Host openshift Hostname openshift.com User git IdentityFile ~/.ssh/openshift 

And so got:

id_cannibal  id_cannibal.pub  id_java  id_java.pub  known_hosts 

Something like id_openshift and id_openshift.pub isn't there but as it's not working, I don't care much now.

I created such files and they're .pub by ssh-keygen -f <filename> and gave different pass phrases to each. I added the content of the .pub's as deploy keys at each Github repository settings.

What am I doing wrong? How is this supposed to work? And, when working at another machine, how to properly obtain these keys, proof it's me and work transparently?

EDIT

Output of git remote -v:

  • For cursoJava repository

origin [email protected]:diegoaguilar/cursoJava.git (fetch) origin [email protected]:diegoaguilar/cursoJava.git (push)

  • For cursoCannibalCreatures

origin [email protected]:diegoaguilar/cursoCannibalCreatures.git (fetch) origin [email protected]:diegoaguilar/cursoCannibalCreatures.git (push)

like image 478
diegoaguilar Avatar asked Mar 31 '14 17:03

diegoaguilar


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 add a key to a Git repository?

Adding the Public Key to GitHubVisit the Settings page for the repository, and then click on Deploy keys. Click on Add deploy key and enter a name for the repository SSH key as the Title field, and copy the contents of the public key file into the Key field.

Can I have 2 SSH keys GitHub?

However, sometimes you need to work with multiple GitHub accounts on the same system, like a work account and a personal account. To accomplish this, you can create multiple SSH keys and associate each one with different GitHub accounts.


1 Answers

As mentioned in "ssh,github,it doesnot work", the trick is to not use the default id_rsa(.pub) names for your public:private keys (because yo can only define one couple of those), but different names.

But that would be only if you were to access those repos as different users

In your case, you are accessing the repos with the same users, and one ssh key should be enough.

See "GitHub help":

This error means the key you are pushing with is attached to another repository as a deploy key, and does not have access to the repository you are trying to push to.

To remedy this, remove the deploy key from the repository, and attach the key to your user account instead.


This is for using GitHub for two different users.

You then define a ~/.ssh/config file in which you reference each private keys by their full path:

Host github1   HostName github.com   User git   IdentityFile ~/.ssh/id_repo1  Host github2   HostName github.com   User git   IdentityFile ~/.ssh/id_repo2 

Instead of using [email protected]:user/repo1, you would use:

github1:user/repo1 

That uses the key Host entry 'github1' to reference the user (git), hostname (github.com) and the exact private/public key to use ~/.ssh/id_repo1(.pub)


So if you have a second repo which use a second key stored as ~/.ssh/id_repo2(.pub), you need to use the entry 'github2' (you can name it as you want) defined above, and then change the url you have for origin:

git remote set-url origin github2:user/repo2 

That way, a git push will use the right key (the one for the repo2)

If you don't, you will be able to push for one repo (using the default key ~/.ssh/id_rsa(.pub), default name), but you won't be able to push to the second repo, which need a different set of public/private key.

like image 78
VonC Avatar answered Sep 28 '22 15:09

VonC