How to use multiple ssh keys for multiple gitlab accounts with the same host
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.
Related videos on Youtube
Osama Mohamed
Updated on June 13, 2022Comments
-
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
fileThe 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 hostNote: I read this question but i can't get help from it
My two accounts are
username1
andusername2
repo URL looks like : [email protected]:username1/test-project.gitMy 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 messageFrom 1) and 2) , i think the problem is definitely from the
~/.ssh/config
file , specifically inHost
variableUpdate 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 almost 8 yearsI read it , i's talking about different accounts , in my case i'm want to do that in the same host :/
-
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 almost 8 yearsit's use
github.com
andac2.github.com
but in my case the same aregitlab.com
without sub-domain -
Jakuje almost 8 yearsIt is only alias to github.com as defined in the
~/.ssh/config
. There is noac2.github.com
-
Osama Mohamed almost 8 yearsokay but in gitlab it's different :/ anyway can you help me how to do it with gitlab ?
-
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 almost 8 years@hobbs but it doesn't work for me
-
hobbs almost 8 yearsOnly because you're not following the instructions.
-
Osama Mohamed almost 8 years@hobbs i followed it , it's just a simple edits why i maybe don't make it ? :/
-
Kirill Voronin almost 6 yearsOn Mac OS remember to add keys to the keychain:
ssh-add -K ~/.ssh/id_rsa_username2
-
-
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 almost 8 yearsi 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 almost 8 yearsUse the exact .ssh/confit file from your question but use the mentioned urns in your git commands.
-
MrTux almost 8 yearsProblem solved now?
-
Osama Mohamed almost 8 yearsNo , it didn't :/ the thing you said i'm already did it and it doesn't work so i write this question
-
MrTux almost 8 yearsThe 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 usegitlab.com
alone but the defined alias which is theHost
line in.ssh/config
. Please add how call git as a comment, I suppose the error lies there. -
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 almost 8 yearsi 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 almost 8 yearsYou 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 almost 8 yearsSo, any news? Working now?
-
Osama Mohamed almost 8 yearsHost 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 almost 8 yearsI did that but nothing new :/
-
MrTux almost 8 yearsok .ssh/config seems to be right. Have you tried cloning or updating the remotes?
-
Osama Mohamed almost 8 yearsi set the remote by git commands then i commit some changes , but i can't push them because of the ssh key :/
-
Osama Mohamed almost 8 yearsthe first two lines returns
GitLab: Disallowed command
and somethimsssh: 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
likeHost 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 almost 8 yearsMaybe without the
verify
. But yes, it looks ok. So fix the config file. Or is there still something unclear? -
Osama Mohamed almost 8 yearsyes how can i fix the config :/ , i'll try it without verify
-
Osama Mohamed almost 8 yearsokay it's works without a verify returns
Welcome to GitLab, username1
andWelcome to GitLab, username2
-
Osama Mohamed almost 8 yearsbut 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 almost 8 yearsUpdate the question with related lines from your
.git/config
and what you changed. -
Osama Mohamed almost 8 yearsit'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 almost 8 yearsThanks a lot , i'm very happy ^_^