How to use multiple SSH keys for different gitlab accounts

First, Generate two ssh keys called “id_rsa_project_a” and “id_rsa_project_b” as below.

ssh-keygen -t rsa -C "[email protected]"

Created ssh keys

~/.ssh/id_rsa_project_a
~/.ssh/id_rsa_project_b

Then add those keys as following.

ssh-add ~/.ssh/id_rsa_project_a
ssh-add ~/.ssh/id_rsa_project_b

You can check ย  your saved keys. Type as below

ssh-add -l

Then create a config file in ~/.ssh folder

 sudo vim ~/.ssh/config 

And edit as below

#Project A
Host gitlab.com-project-a
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa_project_a

#Project B
Host gitlab.com-project-b
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa_project_b

Now you can use both projects with separate ssh keys ๐Ÿ™‚

 

2 thoughts on “How to use multiple SSH keys for different gitlab accounts”

Leave a Comment