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 ed25519 -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 _ntwklab_gitlab. The location is also default, ~/.ssh/
ssh-add ~/.ssh/id_ed25519_ntwklab_gitlab
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_ed25519_ntwklab_gitlab.pub
Test to see it works
ssh -T git@gitlab.com Welcome to GitLab, @ntwklab1!
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.
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
%gitclonegit@gitlab.com:ntwklab1/Terraform.git
Cloning into'Terraform'...
The authenticity of host'gitlab.com (172.65.251.78)'can't be established.
ED25519 key fingerprint is SHA256:eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gQp4taiCfCLB8.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitlab.com'(ED25519)tothe list of known hosts.
git@gitlab.com:Permission denied(publickey).
fatal:Could notread from remote repository.
Please make sure you have the correct access rights
andthe repository exists.
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
0
1
2
3
4
5
Host gitlab.com
AddKeysToAgent yes
UseKeychain yes
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
0
1
2
3
ssh-Tgit@gitlab.com
Welcome toGitLab,@ntwklab1!
Re-run the clone command, and I have added a file, committed and pushed this back to the repo.
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
git clonegit@gitlab.com:ntwklab1/Terraform.git
cd Terraform
touch test.txt
git status
On branch main
Your branch isup todate with'origin/main'.
Untracked files:
(use"git add <file>..."toinclude inwhat will be committed)
test.txt
nothing added tocommit but untracked files present(use"git add"totrack)
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.
remote:HTTP Basic:Access denied.Ifapassword was provided forGit authentication,the password was incorrect oryou're required to use a token instead of a password. If a token was provided, it was either incorrect, expired, or improperly scoped. See https://gitlab.com/help/topics/git/troubleshooting_git.md#error-on-git-fetch-http-basic-access-denied
fatal: Authentication failed for 'https://gitlab.com/ntwklab1/awscloudformation_addingnumbers.git/'
Here are the commands that I ran to resolve the problem and resulted in a successful push.