DEV Community

r_tanaka
r_tanaka

Posted on • Updated on

How to handle multiple ssh keys for gitlab

When you have multiple account of the gitlab.com (ex: private and company), the gitlab.com will reject your key if you want to use same key for each account.

How to handle it?
I can propose 2 ways.

1. use ~/.ssh/config

 1. Open or create ~/.ssh/config

 2. You can add an entry like below.

host gitlab_company
     HostName gitlab.com
     IdentityFile ~/.ssh/company_id_rsa
     User git
Enter fullscreen mode Exit fullscreen mode
  • The first line is as your preference.
  • HostName and User are fixed for the gitlab.com.
  • IdentityFile is your private key.

 3. Also you can add another entry for your private account or something.

host gitlab_private
     HostName gitlab.com
     IdentityFile ~/.ssh/private_id_rsa
     User git
Enter fullscreen mode Exit fullscreen mode

 4. Open .git/config in your repository.

Let's move to your local git repository.
Then open .git/config, you can see like below.

 [core]
     repositoryformatversion = 0
     filemode = true
     bare = false
     logallrefupdates = true
 [remote "origin"]
     url = git@gitlab.com:company/something.git
     fetch = +refs/heads/*:refs/remotes/origin/*
 [branch "main"]
     remote = origin
     merge = refs/heads/main
Enter fullscreen mode Exit fullscreen mode

Please look url under [remote "origin"].
You can change git@gitlab.com:... to git@gitlab_company:... or git@gitlab_private:... like below.

url = gitlab_company:company/something.git
Enter fullscreen mode Exit fullscreen mode

Finally, git client uses your ssh key which defined in configuration files automatically.

2. make command for switching ssh keys

When you have multiple ssh keys like below.

$ ll ~/.ssh/
total 4
-rw------- 1 m me 3243 Aug 31  2021 company_id_rsa
-rw-r--r-- 1 me me  746 Aug 31  2021 company_id_rsa.pub
-rw------- 1 me me 3243 Sep 29  2021 private_rsa
-rw-r--r-- 1 me me  746 Sep 29  2021 private_rsa.pub
Enter fullscreen mode Exit fullscreen mode

You can add below function into a bash configuration file, such as .bashrc or .bash_aliases.

function switch_ssh() {
    rm ~/.ssh/id_rsa ~/.ssh/id_rsa.pub
    ln -s ~/.ssh/$1_id_rsa ~/.ssh/id_rsa
    ln -s ~/.ssh/$1_id_rsa.pub ~/.ssh/id_rsa.pub
    ls -laF ~/.ssh/id_rsa
    ls -laF ~/.ssh/id_rsa.pub
}
Enter fullscreen mode Exit fullscreen mode

Then you can execute like below

switch_ssh company
Enter fullscreen mode Exit fullscreen mode

Above command makes 2 symbolic links like below.

$ ll ~/.ssh/
total 6
-rw------- 1 ryo ryo 3243 Aug 31  2021 company_id_rsa
-rw-r--r-- 1 ryo ryo  746 Aug 31  2021 company_id_rsa.pub
-rw------- 1 ryo ryo 3243 Sep 29  2021 private_rsa
-rw-r--r-- 1 ryo ryo  746 Sep 29  2021 private_rsa.pub
lrwxrwxrwx 1 ryo ryo   32 Apr 20 16:22 id_rsa -> /home/ryo/.ssh/company_id_rsa
lrwxrwxrwx 1 ryo ryo   36 Apr 20 16:22 id_rsa.pub -> /home/ryo/.ssh/company_id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

This command will make or remake symbolic link named id_rsa from company_id_rsa. Of course, switch_ssh private makes id_rsa from private_id_rsa. id_rsa is default key name git client normally use.

Let's enjoy your git life!

Top comments (0)