To Create an SSH key navigate to: User Settings
>> SSH Keys
Generate a 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 named this /.ssh/id_ed25519_GitLab
and it is saved in the default location /Users/USERNAME/.ssh/id_ed25519_GitLab
Once created, cat
the file to view the public key. Do not share the private key with anyone. Paste the public key into the box shown in the GUI.cat /Users/stefankelly/.ssh/id_ed25519_GitLab.pub
Clone Repo Error – GitLab SSH Key Not Used
At this stage you should be able to clone a repo from the GitLab account. Hopwever, I am not able to because it is using the wrong SSH key. I have multiple SSH keys setup.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
% git clone git@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) to the list of known hosts. git@gitlab.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the 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_ed25519_GitLab
0 1 2 3 4 5 |
Host gitlab.com AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_ed25519_GitLab |
Testing
Re run the clone command, and I have added a file, commited 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 clone git@gitlab.com:ntwklab1/Terraform.git cd Terraform touch test.txt git status On branch main Your branch is up to date with 'origin/main'. Untracked files: (use "git add <file>..." to include in what will be committed) test.txt nothing added to commit but untracked files present (use "git add" to track) git add . git commit -am "new test file added" git push Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 272 bytes | 272.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 To gitlab.com:ntwklab1/Terraform.git 85d03f5..4dbfc8b main -> main |