10

I have two Gitlab accounts. On my old account I added an ssh-key that is located in ~/.ssh/id_rsa.pub on my computer.

Now I want to add another ssh-key for my new Gitlab account. How do I do this without having the ssh-keys conflict?

Benyamin Jafari
  • 15,536
  • 14
  • 81
  • 116
  • Possible duplicate of [Best way to use multiple SSH private keys on one client](https://stackoverflow.com/questions/2419566/best-way-to-use-multiple-ssh-private-keys-on-one-client) – phd Jan 14 '18 at 13:24
  • @phd No, my question is different, my question relevant on multiple gitlab account corresponding multiple ssh-keys, and I had already read that qeustion. – Benyamin Jafari Jan 14 '18 at 13:38
  • Possible duplicate of [Multiple SSH Keys on the same device](https://stackoverflow.com/q/43662029/608639) – jww Aug 05 '18 at 06:33

4 Answers4

20

I would recommend a second key, for now without passphrase:

ssh-keygen -t rsa -C "your_email@example.com" -P "" -q -f ~/.ssh/gitlab_rsa

That will create (without any prompt) ~/.ssh/gitlab_rsa (private key) and ~/.ssh/gitlab_rsa.pub (public key)

You need to register that second gitlab_rsa.pub public key to your second GitLab account.

Navigate to the 'SSH Keys' tab in your 'Profile Settings'. Paste your key in the 'Key' section and give it a relevant 'Title'.

Then add a ~/.ssh/config file with:

Host gitlab_rsa
    HostName gitlab.com
    User git
    PreferredAuthentications publickey
    IdentityFile /home/<you>/.ssh/gitlab_rsa

Finally, you can clone any GitLab repo as your second identity with:

git clone gitlab_rsa:<yourSecondAccount>/<yourRepo.git>

That will be replaced automatically with git@gitlab.com:<yourSecondACcount>/<yourRepo.git> and will use your second key.

Benyamin Jafari
  • 15,536
  • 14
  • 81
  • 116
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Do you mistake "/Users" instead of "/home" ? – Benyamin Jafari Jan 14 '18 at 12:03
  • @BenyaminJafari yes, simple typo, but the general idea in this answer still stands. – VonC Jan 14 '18 at 12:06
  • I have problem with this following structure yet: when I git clone,I will encountered this error: sign_and_send_pubkey: signing failed: agent refused operation 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. – Benyamin Jafari Jan 14 '18 at 12:24
  • @BenyaminJafari Is your second Gitlab public key registered in your second Gitlab account? – VonC Jan 14 '18 at 12:29
  • @BenyaminJafari Is your URL `gitlab_rsa:/`? With the right Gitlab username and repo name? – VonC Jan 14 '18 at 12:34
  • can I directed write "replaced automatically" commands ("git clone git@gitlab.com:/") instead of "git clone gitlab_rsa:/"? – Benyamin Jafari Jan 14 '18 at 12:35
  • Yes these are same with gitlab, but I don't know what is wrong? – Benyamin Jafari Jan 14 '18 at 12:37
  • @BenyaminJafari if you are using the `git@gitlab.com:/"`, the default id_rsa private key will be used. Only `gitlab_rsa:/` would force SSH to use the right private key. Internally, SSH will replace that URL with the right one. – VonC Jan 14 '18 at 12:41
  • @BenyaminJafari Can you test the authentication with `ssh -Tv gitlab_rsa`? You should see a welcome message? – VonC Jan 14 '18 at 12:43
  • That result: OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g 1 Mar 2016 debug1: Reading configuration data /home/benyamin/.ssh/config debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: Applying options for * ssh: Could not resolve hostname gitlab_rsa: Name or service not known – Benyamin Jafari Jan 14 '18 at 12:46
  • @BenyaminJafari That is because your `~/.ssh/config` does not have an entry `Host gitlab_rsa` – VonC Jan 14 '18 at 12:47
  • @BenyaminJafari Well done! From there, you should be able to clone your repo – VonC Jan 14 '18 at 13:00
  • Thanks a lot, worked it. but when I use mySecondAccount after gitlab_rsa in git clone commad, encountered error, but I use project owner account instead, and worked: `git clone gitlab_rsa:/` – Benyamin Jafari Jan 14 '18 at 13:09
2

Generate a new key pair with:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

It will ask you to enter a name for the key file:

 Enter a file in which to save the key (/home/you/.ssh/id_rsa): [Press enter]

Choose something different such as /Users/you/.ssh/gitlab_rsa

Then when you need it add this key to your ssh-agent with:

ssh-add ~/.ssh/gitlab_rsa

If you want a permanent access you can edit your ~/.ssh/config file with:

Host gitlab_rsa
    HostName gitlab.com
    User git
    PreferredAuthentications publickey
    IdentityFile /home/<you>/.ssh/gitlab_rsa

Refer to this article for further details.

Benyamin Jafari
  • 15,536
  • 14
  • 81
  • 116
Ortomala Lokni
  • 41,102
  • 15
  • 134
  • 190
2

You need to create the file ~/.ssh/config to define which key should use for every domain.

Create that file with nano and paste your configuration:

nano ~/.ssh/config

And add:

Host your-gitlab.com
    HostName your-gitlab.com
    IdentityFile ~/.ssh/your-gitlab-privkey
  • what do you mean "your-gitlab.com" ? – Benyamin Jafari Jan 14 '18 at 10:55
  • `your-gitlab.com` should be your GitLab url, where you are going to connect. Like if I want to link that key to github.com -> `Host github.com` and also `HostName github.com` – Elena Bonet Jan 14 '18 at 10:55
  • @BenyaminJafari That is what I explain in my answer (written before this duplicate one): what you put in your ssh/config is a key: you will use that key in your SSH URL. – VonC Jan 14 '18 at 11:32
  • @VonC , no need to put '(written before this duplicate one)'. Just comment on your own entry. – Elena Bonet Jan 16 '18 at 18:26
2

Generate SSH please follow below steps.

Open Git Bash on you machine

Enter the below command to genarate

ssh-keygen -t rsa -b 4096 -C "yourmail@example.com"

Generating public/private rsa key pair. Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):Press enter Enter passphrase (empty for no passphrase): Type a passphrase Enter same passphrase again: Type passphrase again

Once enter the confirm passphrase, will get confirmation message.

go to the gitpair.pub file location and right click open with notepad. copy the code and past the in the below text box, your email will pick automatically in the title box. then click add key.

Marian Nasry
  • 805
  • 9
  • 19