7

Currently I'm using GitLab as my remote GIT server.
I have no problem using single Gitlab account with SSH key assigned to it.

But now I applied another Gitlab account and I'm trying to use the same SSH key for it, but I cannot add the key to this new account.
The error is as follows when I tried to add the key:

Key has already been taken
Fingerprint has already been taken

So how should I use the same key to access the second Gitlab account? if it is not possible, how should I use two keys at the same time.

By the way, I'm using windows system.

Thanks in advance!!

Updates

Below is my config file. And it is as follows:

#my primary account
Host {account1}
    User git
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile C:/Users/{username}/.ssh/id_rsa1

#for NPR_HPTG account
Host {account2}
    User git
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile C:/Users/{username}/.ssh/id_rsa2

And I'm having two Gitlab account,

git@gitlab.com:{account_1}/repo1.git
git@gitlab.com:{account_2}/repo1.git

Still, I cannot access to the account_2.

Previously, before I'm having this 2nd GitLab account, I simply upload the ssh key to the account1 without needing set This. But now by following this, still, in the end I could push to the git@gitlab.com:{account_2}/repo1.git. And I'm using TortoiseGit to push/pull.

Simson
  • 3,373
  • 2
  • 24
  • 38
2342G456DI8
  • 1,819
  • 3
  • 16
  • 29

1 Answers1

21

Simply declare each private ssh keys in a %HOME%/.ssh/config file:

Host gitlabuser1
    User git
    Hostname {hostname}
    PreferredAuthentications publickey
    IdentityFile C:/Users/{username}/.ssh/id_rsa1

Host gitlabuser2
    User git
    Hostname {hostname}
    PreferredAuthentications publickey
    IdentityFile C:/Users/{username}/.ssh/id_rsa2

That supposes your set of ssh keys are:

%HOME%/.ssh/id_rsa1 ; %HOME%/.ssh/id_rsa1.pub
%HOME%/.ssh/id_rsa2 ; %HOME%/.ssh/id_rsa2.pub

You can then use the urls for clone/push/pull:

gitlabuser1:yourRepo1
gitlabuser2:yourRepo2

Make sure your CMD session has %HOME% defined, usually to %USERPROFILE% (which is done for you with git-cmd.bat)

You have a more detailed procedure in this blog post.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I'm inclined to beleive that merely writing `IdentityFile id_rsa2` should pick that `id_rsa2` file from `~/.ssh/` thus doing the Right Thing automagically. I'd try and see. – kostix May 19 '15 at 09:12
  • What should be the correct format for hte `gitlabuser1`. is it something like this: `git@gitlab.com:my_username`? – 2342G456DI8 May 19 '15 at 09:17
  • @GordonQu `gitlabuser1` is there to replace `git@gilab.com`. Note that with ssh url, the `username` is *never* part of the url, since it is the job of the public ssh key to authenticate you on the GitLab server side. – VonC May 19 '15 at 09:19
  • 1
    So how about the `{hostname}`, is it also the same, it should be `git@gitlab.com` as well? – 2342G456DI8 May 19 '15 at 09:21
  • @GordonQu `{hostname}` is to be replaced by the hostname of your gitlab server. If it is gitlab.com, then it would be replaced by `gitlab.com` – VonC May 19 '15 at 09:23
  • @GordonQu `Host git@gitlab.com` is incorrect: this is supposed to be a key, like `gitlabuser1` or `gitlabuser1`. The user must be specified with `User git` within the `Host gitlabuser1` section. – VonC May 19 '15 at 09:41
  • @GordonQu Fix the `Host` value, and add `User git`. – VonC May 19 '15 at 09:43
  • 2
    @GordonQu if you use `Host {account2}`, then the url to use would be `{account_2}:{account_2}/repo1.git`. Forget about `git@gitlab.com`. `Host` is a *key*. If you had set `Host` to `xxx2`, then the url would be `xxx2:{account2}/repo1.git`. – VonC May 19 '15 at 10:29