Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up two SSH keys for two GitLab accounts and push/pull by using TortoiseGit?

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,

[email protected]:{account_1}/repo1.git
[email protected]:{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 [email protected]:{account_2}/repo1.git. And I'm using TortoiseGit to push/pull.

like image 544
2342G456DI8 Avatar asked May 19 '15 08:05

2342G456DI8


People also ask

Can I have 2 SSH keys GitLab?

Gitlab won't allow reuse of a public ssh key for multiple accounts. To get around this you need to create a second ssh key for the second account.

Can I use SSH key for multiple accounts?

No. GitHub uses the SSH key to identify the account, so only one account may use a specific key. If you have multiple keys loaded at the same time in your SSH-agent, the first one that matches a GitHub account will be used.


1 Answers

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.

like image 152
VonC Avatar answered Sep 22 '22 23:09

VonC