22

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 : git@gitlab.com: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 git@gitlab.com-username1 ssh -T git@gitlab.com-username2 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 = git@gitlab.com:username1/test-project.git to [remote "origin"] url = git@gitlab.com-username1:username1/test-project.git

and do the same for the username2

Community
  • 1
  • 1
Osama Mohamed
  • 647
  • 1
  • 6
  • 10

2 Answers2

21

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

ssh -T git@gitlab.com-username1
ssh -T git@gitlab.com-username2

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 = git@gitlab.com:username1/test-project.git

Replace it with

[remote "origin"]
  url = git@gitlab.com-username2:username1/test-project.git

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

Jakuje
  • 24,773
  • 12
  • 69
  • 75
  • 1
    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 :/ ` – Osama Mohamed Jun 18 '16 at 21:11
  • 1
    Maybe without the `verify`. But yes, it looks ok. So fix the config file. Or is there still something unclear? – Jakuje Jun 18 '16 at 21:19
  • yes how can i fix the config :/ , i'll try it without verify – Osama Mohamed Jun 18 '16 at 21:26
  • okay it's works without a verify returns `Welcome to GitLab, username1` and `Welcome to GitLab, username2` – Osama Mohamed Jun 18 '16 at 21:28
  • 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 – Osama Mohamed Jun 18 '16 at 21:32
  • 1
    Update the question with related lines from your `.git/config` and what you changed. – Jakuje Jun 18 '16 at 21:33
  • 1
    it's works , the solving was to edit the `.git/config` file from `[remote "origin"] url = git@gitlab.com:username1/test-project.git` to `[remote "origin"] url = git@gitlab.com-username1:username1/test-project.git` – Osama Mohamed Jun 18 '16 at 22:03
  • Thanks a lot , i'm very happy ^_^ – Osama Mohamed Jun 18 '16 at 22:04
2

Use the exact ~/.ssh/config from above and update the URLs you use with git to git@gitlab.com-username1:username1/test-project.git for the first user and git@gitlab.com-username2:username2/test-project.git for the second one (e.g., git clone git@gitlab.com-username1: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.

MrTux
  • 32,350
  • 30
  • 109
  • 146
  • ` Host git@gitlab.com:username1/test-project.git HostName gitlab.com User git IdentityFile ~/.ssh/id_rsa Host git@gitlab.com:username1/test-project.git HostName gitlab.com User git IdentityFile ~/.ssh/id_rsa_username2` – Osama Mohamed Jun 18 '16 at 10:19
  • 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 :/ – Osama Mohamed Jun 18 '16 at 10:20
  • Use the exact .ssh/confit file from your question but use the mentioned urns in your git commands. – MrTux Jun 18 '16 at 10:20
  • 1
    Problem solved now? – MrTux Jun 18 '16 at 10:34
  • No , it didn't :/ the thing you said i'm already did it and it doesn't work so i write this question – Osama Mohamed Jun 18 '16 at 10:49
  • 1
    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. – MrTux Jun 18 '16 at 10:59
  • ` 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 Jun 18 '16 at 11:24
  • i set the remote as git@gitlab.com:username1/test-project.git for username1 project and git@gitlab.com:username2/test-project.git for the usenamw2 project – Osama Mohamed Jun 18 '16 at 11:24
  • 1
    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 Jun 18 '16 at 11:34
  • 1
    So, any news? Working now? – MrTux Jun 18 '16 at 12:10
  • 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 Jun 18 '16 at 19:13
  • I did that but nothing new :/ – Osama Mohamed Jun 18 '16 at 19:14
  • 1
    ok .ssh/config seems to be right. Have you tried cloning or updating the remotes? – MrTux Jun 18 '16 at 19:38
  • 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 Jun 18 '16 at 19:48