Same ssh-key not working on multiple GitLab/GitHub accounts 😕
I have been using my personal GitLab & Github accounts for a while. Recently I had to make another work GitLab account for working on some other projects, but the actual pain was I can only use my same ssh-key(primary ssh key) with only one GitLab account at a time 😔. This is not happening did a couple of searches and got a way to manage multiple ssh keys using an ssh config file. I was lazy enough to follow through and experiment so just delete my ssh-key pair from my personal GitLab and reused the same key for my work account 🤣. Few days ago my friend Nikhil, had the same issue and felt I should try this ssh config file stuff and here I am sharing that work with you :)
I will be using the following terms throughout this post:
- Config file: The OpenSSH config file that is stored in
~/.ssh/configfile - Primary ssh key: The default ssh key you wish to use to for your personal GitLab account.
- Secondary ssh key: The newly generated key you will be using to access your work/other GitLab account.
Whenever you try to ssh into any remote system/service, your OpenSSH library looks into ~/.ssh/ dir for keys, configurations(which we will do), known_hosts, etc. It’s all magic when you do password-less login into any remote system/service.
So what is this config file I have been talking about?
OpenSSH client-side configuration file is named config, and it is stored in the .ssh directory under the user’s home directory. This file is not automatically created one needs to create this manually and set permissions.
$ touch ~/.ssh/config
$ chmod 600 ~/.ssh/config
In this file, you need to specify which host you want to access, which private ssh file you would use for a given host, port number, user name, and a lot more. In our case, we just need hostname, user & identity(private ssh key file).
I am assuming you have your ssh key (primary/default) already created and you have added it to your personal GitLab account and your life is good on the personal account side. Now let our life be easier on work GitLab account.
Creating config dir & new ssh keypair
Create a dir where you will store all your keys, this is completely optional you can even store it at the default location as well. Just make sure you don’t use the same/default key name id_rsa it will overwrite your primary ssh key.
In my case Create custom key dir
$ mkdir ~/.custome_ssh_keys
$ cd ~/.custome_ssh_keys
Copy primary ssh key into it
$ cp ~/.ssh/id_rsa ~/.custome_ssh_keys/primary_key
$ cp ~/.ssh/id_rsa.pub ~/.custome_ssh_keys/primary_key.pub
Generate secondary key in it
$ ssh-keygen -t rsa -f ~/.custome_ssh_keys/secondary_key
Config file setup
Now the magical part
Add the following text to your ~/.ssh/config file
# personal gitlab access
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.custom_ssh_keys/primary_key
# work gitlab access
Host gitlab.com-work
HostName gitlab.com
User git
IdentityFile ~/.custom_ssh_keys/secondary_key
Two important things to note here are hostname & identity file. Those two files will help OpenSSH decide which key to you with which host.
Before proceeding, I assume you have already added ~/.custom_ssh_keys/secondary_key.pub to your work GitLab ssh.
Clone any repository:
When cloning repositories from the work account, you’ll need to modify the URL’s host to match the value in the SSH config’s Host directive. For example:
# Before
$ git clone git@gitlab.com:work/repository.git
# After config
$ git clone git@gitlab.com-work:work/repository.git
Or
You may also update any existing repository by simply
$ git remote set-url origin git@gitlab.com-work:work/repository.git
I have done the same with GitHub too, but still while cloning I forget about updating the host URL and I end up getting errors like this
Cloning into '<project>'...
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
Errors make me realize what an idiot I am. Hope you won’t make this error.
Happy coding :)