Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 632
Osama Mohamed Avatar asked Jun 18 '16 09:06

Osama Mohamed


People also ask

Can you use the same SSH key for multiple GitLab accounts?

The problem with multiple GitLab accounts is that you can't use one SSH key on both of them. For example, you have a personal email address associated with one GitLab account, and business email with another. This is a great way to distinguish personal work from business, but it also comes with some challenges.

Can I have 2 SSH keys GitLab?

Sometimes you need more accounts than one for access to Github or Gitlab and similar tools. For example you can have one account for your projects at home and second account for your company.

Can I use SSH key for multiple accounts?

You'll need to create an additional SSH key for each extra Bitbucket account you have or each computer you use. For example, if you have four Bitbucket accounts, you need to generate 3 new SSH keys, meaning you'll have 4 keys in all.


2 Answers

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.

like image 55
Jakuje Avatar answered Oct 26 '22 10:10

Jakuje


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.

like image 22
MrTux Avatar answered Oct 26 '22 08:10

MrTux