Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple Git SSH keys on Eclipse?

Tags:

git

ssh

eclipse

I looked several answers and forums for a solution but I could not find a single one that works.

I have this scenario:

  • Eclipse Luna Service Release 2 (4.4.2)
  • Ubuntu 14.04 x64
  • Two ssh keys on my ~/.ssh folder
  • Two bitbucket accounts (one for personal projects and one for enterprise)
  • A git repository only accessible with my primary key (~/.ssh/id_rsa)
  • A git repository only accessible with my secondary key (~/.ssh/other)

I created a ~/.ssh/config file with the contents:

Host bitbucket bitbucket.org
    Hostname bitbucket.org
    IdentityFile ~/.ssh/id_rsa
    IdentityFile ~/.ssh/other
    User git

And for the sake of sanity I added the second key using ssh-add as well. Running ssh-add -l lists both keys.

When using the command line, all git commands work like a charm, with both repositories. But when using Eclipse, I always get the Invalid remote: origin error when trying to clone or pull from the repository with the secondary key:

Caused by: org.eclipse.jgit.errors.NoRemoteRepositoryException: [email protected]:myuser/myrepository.git: conq: repository access denied.

I added the secondary key at Window > Preferences > Network Connections > SSH2 > Private keys, and set the GIT_SSH environment variable to point to my ssh executable:

$echo $GIT_SSH
/usr/bin/ssh

I've restarted Eclipse and even the OS several times, with no luck.

Since I can use git from the command line without problems, I tend to believe there's something wrong with Eclipse.

How to use multiple Git SSH keys on Eclipse? Or how to force Eclipse to use my secondary key on a single project?

like image 973
Gilberto Torrezan Avatar asked Mar 16 '23 13:03

Gilberto Torrezan


1 Answers

Host bitbucket bitbucket.org? You don't declare multiple entry names on one Host section.

I would expect to see in a ssh config file declaring multiple keys:

Host bitbucketuserA
    Hostname bitbucket.org
    IdentityFile ~/.ssh/id_rsa
    User git

Host bitbucketuserB
    Hostname bitbucket.org
    IdentityFile ~/.ssh/other
    User git

And you would use ssh url like

bitbucketuserA:userA/myrepo1
bitbucketuserB:userB/myrepo2

(this is similar to what I suggested for "How to work on personal GitHub repo from office computer whose SSH key is already added to a work related GitHub account?")

like image 143
VonC Avatar answered Mar 18 '23 05:03

VonC