How to use multiple ssh keys for multiple gitlab accounts with the same host

11,134

Solution 1

You have got complete ssh configuration. First of all, check if it works:

ssh -T [email protected]
ssh -T [email protected]

should succeed in both cases. If not, the keys are not set up correctly. Verify that the keys are on gitlab for respective users.

If it works, move on to your git repository and open .git/config. It will have some part like:

[remote "origin"]
  url = [email protected]:username1/test-project.git

Replace it with

[remote "origin"]
  url = [email protected]:username1/test-project.git

(or username1 if you want to connect using this username). Then it should allow you to push.

Solution 2

Use the exact ~/.ssh/config from above and update the URLs you use with git to [email protected]:username1/test-project.git for the first user and [email protected]:username2/test-project.git for the second one (e.g., git clone [email protected]:username1/test-project.git).

SSH will look up the gitlab.com-username1 alias in ~/.ssh/config and will use the right host name and SSH key file.

Another way would be to just use one user for pushing/pulling and grant the required rights to this one user.

Share:
11,134

Related videos on Youtube

Osama Mohamed
Author by

Osama Mohamed

Updated on June 13, 2022

Comments

  • Osama Mohamed
    Osama Mohamed almost 2 years

    I want to use 2 accounts in Gitlab website, every account with a different ssh key

    I generated the keys successfully and add them in ~/.ssh folder I created ~/.ssh/config file and use one of them , it's works good I can also make swapping between the two keys by editing the ~/.ssh/config file

    The problem is : I want to use them in the same time , but all the tutorials i found taking about different hosts :/

    actually my two accounts are in the same host

    how can i edit the ~/.ssh/config file to accept two accounts for the same host

    Note: I read this question but i can't get help from it

    My two accounts are username1 and username2 repo URL looks like : [email protected]:username1/test-project.git

    My current ~/.ssh/config file:

    Host gitlab.com-username1
      HostName gitlab.com
      User git
      IdentityFile ~/.ssh/id_rsa
    
    Host gitlab.com-username2
      HostName gitlab.com
      User git
      IdentityFile ~/.ssh/id_rsa_username2
    

    Update 1:

    1) When I use one key in the ~/.ssh/config file , everything works perfect (but it's very boring to update it every time i want to change the user i use)

    2) When i use this lines ssh -T [email protected] ssh -T [email protected] its works good and return a welcoming message

    From 1) and 2) , i think the problem is definitely from the ~/.ssh/config file , specifically in Host variable

    Update 2: (the solving) the solving was to edit the .git/config file from [remote "origin"] url = [email protected]:username1/test-project.git to [remote "origin"] url = [email protected]:username1/test-project.git

    and do the same for the username2

    • Osama Mohamed
      Osama Mohamed almost 8 years
      I read it , i's talking about different accounts , in my case i'm want to do that in the same host :/
    • Jakuje
      Jakuje almost 8 years
      "I want to use 2 accounts in Gitlab website"??? The duplicate is using the same host (you only need to substitute github with gitlab. There is only on github.
    • Osama Mohamed
      Osama Mohamed almost 8 years
      it's use github.com and ac2.github.com but in my case the same are gitlab.com without sub-domain
    • Jakuje
      Jakuje almost 8 years
      It is only alias to github.com as defined in the ~/.ssh/config. There is no ac2.github.com
    • Osama Mohamed
      Osama Mohamed almost 8 years
      okay but in gitlab it's different :/ anyway can you help me how to do it with gitlab ?
    • hobbs
      hobbs almost 8 years
      @OsamaMohamed it's not different, it's exactly the same, and that answer is exactly what you need to do.
    • Osama Mohamed
      Osama Mohamed almost 8 years
      @hobbs but it doesn't work for me
    • hobbs
      hobbs almost 8 years
      Only because you're not following the instructions.
    • Osama Mohamed
      Osama Mohamed almost 8 years
      @hobbs i followed it , it's just a simple edits why i maybe don't make it ? :/
    • Kirill Voronin
      Kirill Voronin almost 6 years
      On Mac OS remember to add keys to the keychain: ssh-add -K ~/.ssh/id_rsa_username2
  • Osama Mohamed
    Osama Mohamed almost 8 years
    ` Host [email protected]:username1/test-project.git HostName gitlab.com User git IdentityFile ~/.ssh/id_rsa Host [email protected]:username1/test-project.git HostName gitlab.com User git IdentityFile ~/.ssh/id_rsa_username2`
  • Osama Mohamed
    Osama Mohamed almost 8 years
    i update it i this way but nothing happen , also should i specific every repo in config file ? is there is a way to set the username only :/
  • MrTux
    MrTux almost 8 years
    Use the exact .ssh/confit file from your question but use the mentioned urns in your git commands.
  • MrTux
    MrTux almost 8 years
    Problem solved now?
  • Osama Mohamed
    Osama Mohamed almost 8 years
    No , it didn't :/ the thing you said i'm already did it and it doesn't work so i write this question
  • MrTux
    MrTux almost 8 years
    The comments you posted show that you did it wrong. I updated my answer. Don't update the .ssh/config file, use the one you posted in your question. Then for all git opertions don't use gitlab.com alone but the defined alias which is the Host line in .ssh/config. Please add how call git as a comment, I suppose the error lies there.
  • Osama Mohamed
    Osama Mohamed almost 8 years
    ` Host gitlab.com-username1/test-project.git HostName gitlab.com User git IdentityFile ~/.ssh/id_rsa Host gitlab.com-username2/test-project.git HostName gitlab.com User git IdentityFile ~/.ssh/id_rsa_username2`
  • Osama Mohamed
    Osama Mohamed almost 8 years
    i set the remote as [email protected]:username1/test-project.git for username1 project and [email protected]:username2/test-project.git for the usenamw2 project
  • MrTux
    MrTux almost 8 years
    You changed the host line in ,ssh/config which is wrong! It must not include the path. And you need to update the git remote url to the just defined alias. Please re-read my answer.
  • MrTux
    MrTux almost 8 years
    So, any news? Working now?
  • Osama Mohamed
    Osama Mohamed almost 8 years
    Host gitlab.com-username1 HostName gitlab.com User git IdentityFile ~/.ssh/id_rsa Host gitlab.com-username2 HostName gitlab.com User git IdentityFile ~/.ssh/id_rsa_username2
  • Osama Mohamed
    Osama Mohamed almost 8 years
    I did that but nothing new :/
  • MrTux
    MrTux almost 8 years
    ok .ssh/config seems to be right. Have you tried cloning or updating the remotes?
  • Osama Mohamed
    Osama Mohamed almost 8 years
    i set the remote by git commands then i commit some changes , but i can't push them because of the ssh key :/
  • Osama Mohamed
    Osama Mohamed almost 8 years
    the first two lines returns GitLab: Disallowed command and somethims ssh: Could not resolve hostname gitlab.com-username1: Name or service not known the difference happens when i edit the ~/.ssh/config file but anyway the keys is okay , because when i use one key in ~/.ssh/config like Host gitlab.com HostName gitlab.com User git IdentityFile ~/.ssh/id_rsa the code works good :/ and push the commits to gitlab so i think that the problem is in the config file :/ `
  • Jakuje
    Jakuje almost 8 years
    Maybe without the verify. But yes, it looks ok. So fix the config file. Or is there still something unclear?
  • Osama Mohamed
    Osama Mohamed almost 8 years
    yes how can i fix the config :/ , i'll try it without verify
  • Osama Mohamed
    Osama Mohamed almost 8 years
    okay it's works without a verify returns Welcome to GitLab, username1 and Welcome to GitLab, username2
  • Osama Mohamed
    Osama Mohamed almost 8 years
    but it doesn't make the push it's returns this message 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. this message appers only if i use two keys in the ~/.ssh/config file , but if i use one key in the ~/.ssh/config file the push command works successfully
  • Jakuje
    Jakuje almost 8 years
    Update the question with related lines from your .git/config and what you changed.
  • Osama Mohamed
    Osama Mohamed almost 8 years
    it's works , the solving was to edit the .git/config file from [remote "origin"] url = [email protected]:username1/test-project.git to [remote "origin"] url = [email protected]:username1/test-project.git
  • Osama Mohamed
    Osama Mohamed almost 8 years
    Thanks a lot , i'm very happy ^_^