17
OS Description: Ubuntu 14.04.3 LTS
ssh: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.3, OpenSSL 1.0.1f 6 Jan 2014

I am unable to use multiple ssh key for gitlab. The error I am getting is

GitLab: The project you were looking for could not be found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists. 

I am pretty much sure that I did everything right This is my config file

Host work gitlab.com
    Hostname gitlab.com
    IdentityFile ~/.ssh/ida_rsa
    User git

Host integrate gitlab.com
    Hostname gitlab.com
    IdentityFile ~/.ssh/ida_rsa_personal
    User git

The keys are added automatically in ssh-agent, but to make sure I also added the key manually

$ ssh-add -l
2048 e7:08:d6:8c:00:28:31:f9:3f:21:4a:0f:4e:1e:ee:20 id_rsa (RSA)
2048 ff:22:f6:90:2b:7c:9f:ed:45:41:df:79:06:de:fe:81 id_rsa_personal (RSA)

My ssh-agent is also running

$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-uTC6tA5HMt1x/agent.4899; export SSH_AUTH_SOCK;
SSH_AGENT_PID=4900; export SSH_AGENT_PID;
echo Agent pid 4900;

Permissions of ssh key

-rw-------  1 yogi yogi  1679 jun 25 15:38 id_rsa
-rw-------  1 yogi yogi  1679 ago 25 10:53 id_rsa_personal
-rw-r--r--  1 yogi yogi   404 ago 25 10:53 id_rsa_personal.pub
-rw-r--r--  1 yogi yogi   391 jun 25 15:41 id_rsa.pub

I added my ssh public key on gitlab.

Could any one please tell me what I am missing ?

Note :- I noticed that out of my two ssh keys, the ssh key which is on top( id_rsa - e7:08....) works fine.

$ ssh-add -l
**2048 e7:08:d6:8c:00:28:31:f9:3f:21:4a:0f:4e:1e:ee:20 id_rsa (RSA)**
2048 ff:22:f6:90:2b:7c:9f:ed:45:41:df:79:06:de:fe:81 id_rsa_personal (RSA)

If I add id_rsa_personal(ff:22..) after id_rsa, than id_rsa_personal starts working fine and id_rsa stops working.

$ ssh-add -l
**2048 ff:22:f6:90:2b:7c:9f:ed:45:41:df:79:06:de:fe:81 id_rsa_personal (RSA)**
2048 e7:08:d6:8c:00:28:31:f9:3f:21:4a:0f:4e:1e:ee:20 id_rsa (RSA)

I think , ssh-agent is unable to iterate through keys. It only reads first key and stops. Is this a bug in openssh ? If yes, any workaround ?

Thanks

Yogesh Yadav
  • 4,557
  • 6
  • 34
  • 40
  • what is the command you are trying to connect to gitlab? You are providing keys through the agent or through the config? If you want to use only specific identity, make sure you specify also `IdentitiesOnly` option – Jakuje Aug 25 '15 at 20:20
  • I am trying to use git clone, git pull, git push. – Yogesh Yadav Aug 25 '15 at 20:48
  • 1
    ssh-agent doesn't take the first one, I am also experiencing the same problem with my correct ssh-key on top. – Zangetsu Nov 17 '15 at 23:52
  • `Host integrate gitlab.com` Can the host have spaces in? what name are you using to push/pull to/from? – Holloway Nov 24 '15 at 16:52
  • @Holloway No, the argument to `Host` is a list of host names, space separated. I guess the second `gitlab.com` overrides the first, or the other way around. – tripleee Nov 24 '15 at 16:55
  • Try removing the `gitlab.com` from the host line. (leaving just `Host work` and `Host integrate`. – Holloway Nov 24 '15 at 16:58
  • @Holloway **Host work** and **Host integrate** did not work. I strongly feel this is some bug with this particular version of openssh or ubuntu – Yogesh Yadav Dec 19 '15 at 21:37
  • I'm having these same problems, trying out GitLab.com on OS X. Following @Zangetsu's answer did not help. I'm thinking it's something to do with GitLab. – akauppi Aug 02 '16 at 08:43
  • @akauppi You are right. I also shifted to OS X but still facing the problem. The multiple keys work with different providers like bitbucket, gitlab and github. But 2 or more keys with gitlab ( same provider ) does not work for me. I have not tried with 2 or more keys with github. – Yogesh Yadav Aug 11 '16 at 04:17

2 Answers2

27

You need to have ~/.ssh/config file

Inside file

# Default GitHub user
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/personalid

# Work user account
Host bitbucket.org
    HostName bitbucket.org
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/workid

Details can be found in the link below https://confluence.atlassian.com/bitbucket/configure-multiple-ssh-identities-for-gitbash-mac-osx-linux-271943168.html

If the domain is same, use different Host

Host work.gitlab.com
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa.work

Host home.gitlab.com
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa.home

Clear the cache

ssh-add -D

Add all ssh keys

ssh-add ~/.ssh/id_rsa.work
ssh-add ~/.ssh/id_rsa.home

Also set your repos' url in (.git/config) accordingly as given:

url = git@work.gitlab.com:xxxxxxxxxx.git
url = git@home.gitlab.com:yyyyyyyyy.git 

If your ssh config file is as what I suggested.

Zangetsu
  • 1,900
  • 17
  • 25
  • I already have ~/.ssh/config file as I mentioned in my question. Most probably it is some bug with this particular version of Ubuntu or ssh. I am using multiple ssh keys in windows and other linux based OS without any issue. – Yogesh Yadav Nov 19 '15 at 00:58
  • I have edited my answer. Please have a look. Using different host is working for me. Just clear the cache as add the keys again. – Zangetsu Nov 24 '15 at 16:50
  • Using different host did not work. And even after clearing the cache with ssh-add -D, i can see my keys if I do ssh-add -l . So probably clearing cache is not working. – Yogesh Yadav Dec 19 '15 at 21:41
  • Perhaps you need to set url in ".git/config" for different repos as url = git@work.gitlab.com:xxxxxxxxxx.git and url = git@home.gitlab.com:yyyyyyyyy.git If your ssh config file is as I suggested. – Zangetsu Mar 03 '16 at 07:32
  • Just to add that the `ss-add` is not necessary when you set your custom `Host work.gitlab.com` – Sina Jun 24 '17 at 16:57
0

Adding these lines will help
I faced same issue https://i.stack.imgur.com/SUKAf.png

thienkhoi tran
  • 341
  • 2
  • 9