Gitlab SSH Config Host File, Allowing Multiple Account SSH Access
Let’s do things the correct way.
- Assuming that you are using a Linux or a macOS system with an OpenSSH client installed. — All modern macOS operating systems and linux distributions come with SSH pre-installed.
- Gitlab Account.
I can assume that if you are regularly connecting to multiple Gitlab account repos, then you have probably come across the following error.
Oh no! Now what? This is the error when using the same SSH key across GitLab environments as when you submit a SSH key to GitLab it runs a check across the entirety of the GitLab domain to make sure that the key being used is not in use elsewhere.
What you need is a host config file and it is pretty straight forward to create. So here is a how to guide in order to set this up yourself.
Now when you do not specify what SSH file you wish to use it will use the default
~/.ssh/id_rsa key generated and saved here.
So in order to avoid this issue we need to create a host file which is also called a config file. You will need to create this file, to show how this file should look for you, here is a an example.
#Work GitLab Account
IdentitiesOnly yes#Personal Gitlab Account
So to create this yourself, enter the following in your terminal.
vim ~/.ssh/config this will create a new file inside the SSH directory. To make this easier, copy and paste the above code into your own config/hosts file.
Now you have the above copied you need to make sure you have a new key generated also for
id_rsa SSH key and a personal SSH key, that we are going to reference. To create and generate a new RSA SSH key, enter the following:
ssh-keygen -t rsa -b 4096
Once created you need to copy the public key to your work Gitlab account. To find and copy the new key generated use the following:
cat ~/.ssh/id_rsa.puband copy the output key and copy it to your work gitlab account. Once signed into your account, head to account name > SSH Keys > and paste above copied Key into the SSH Key. You can set a title name for this, you could use:
id_rsathen hit save.
Now moving onto the personal GitLab key, enter the following.
ssh-keygen -o -a 100 -t ed25519 -f ~/.ssh/personal-key
Once again, make sure you save the new personal GitLab SSH Key to your personal gitlab account with the same method above using
cat ~/.ssh/personal-key.puband entering this into account name > SSH Keys > and paste above copied Key into the SSH Key. You can set a title name for this, you could use:
personal-keythen hit save.
You should now have two keys inside of
~/.ssh/ one reading
id_rsa id_rsa.pub and the other reading
personal-key personal-key.pub and one key inside of your work gitlab account and one key in your personal gitlab account.
Now comes the last part. If you want to git clone anything from work related repos just use the normal git clone action and it will work as expected. If you however want to clone from personal repo’s then you use this command:
git clone firstname.lastname@example.org:[enter your user account name here]/medium-test-personal-repo.git
Viola! This now enables you to clone repos from other accounts, without having to switch SSH keys each time. You can keep adding SSH Keys to the config file for other GitLab accounts you need access to. Just make sure to generate a new SSH key each time you add a new account and refer to its new name. Like below.
ssh-keygen -o -a 100 -t ed25519 -f ~/.ssh/new-account#New Gitlab Account
If you have any question or need further help let me know in the comments and I will update this post with updates to help you further! Thanks.
If you wish to support me and my future posts please feel free to use the following link to buy me a coffee ☕️ https://ko-fi.com/darylweston It is much appreciated! 😀 or please feel free to follow me! 👍🏻