GitLab Create SSH Key

This is a short post to explain how to setup a GitLab SSH key to securely access your repos. For the full GitLab documentation, please see this article.

In GitLab, to create an SSH key, navigate to: User Settings >> SSH Keys

Generate an SSH Key

I am doing this on a Mac, open a terminal and enter:
ssh-keygen -t rsa -b 2048 -C "your_email@example.com"

Next, it will ask for a location and file name. I have let this default ~/.ssh/id_rsa, however it’s always a good idea to append it with something like _GitLab. The location is also default, ~/.ssh/

Two files have been created: id_rsa which contains the private key and id_rsa.pub which is the public key.

Once created, cat the file id_rsa.pub to view the public key. Paste the public key into the box shown in the GitLab GUI.
cat ~/.ssh/id_rsa.pub

Clone Repo Error – GitLab SSH Key Not Used

At this stage, you should be able to clone a repo from the GitLab account. However, you may encounter an error similar to this where the private key is not correctly configured, and will be refused. The example below is for an older key I was using and is for an example a potential error only.

To fix this, I will add the private key to the ~/.ssh/config file. If this is not created, just create it touch ~/.ssh/config.
IdentityFile ~/.ssh/id_rsa

Testing

You can test directly from the CLI with this command ssh -T git@gitlab.com. If successful, you will see output similar to the below

Re-run the clone command, and I have added a file, committed and pushed this back to the repo.

Other Problems

I thought this was worth mentioning as I encountered this after an update and neglect to how it was configured.
I received this very large error that seemed to be mentioning HTTP authentication. The solution was quite simple, to reconfigure Git to use SSH authentication instead of HTTPS.

Here are the commands that I ran to resolve the problem and resulted in a successful push.

Leave a Comment

Your email address will not be published. Required fields are marked *