When you try to clone private repositories for the first time from an account, you may get following error.
Permission denied (publickey). Permission denied (publickey) fatal : Could not read from remote repository Please make sure you have the correct access rights and the repository exists.
This article helps you on fixing this issue. The root cause here is that the remote repository don’t trust you. In order to gain access to the repo, you have to create an SSH key and register that key in your git repository.
Steps to add SSH key in Gitlab
- Run CMD/Powershell/Terminal with administrative (sudo) privilege. (In windows run cmd as administrator. In linux execute ‘sudo su’ to get root privilege).
- Type ssh-keygen.
You will see the following. Here you will be asked for the location where the SSH key will be saved. Press enter to accept default or enter your custom location.
Generating public/private rsa key pair. Enter file in which to save the key (C:\Users\yourUsername/.ssh/id_rsa):
- Git will ask you to save the key to the specific directory.You will be asked for a password. Make sure you remember it since it will be needed for cloning.
Enter passphrase (empty for no passphrase):
- The public key will be created to the specific directory.
- Now go to the directory you have specified in Step 2 and open
- You’ll see a file
id_rsa.pub. Open it on notepad. Copy all text from it.
- Go to https://gitlab.com/profile/keys
Here you can see all the SSH keys specified so far. Paste the copied key.
- Now click on the “Title” below. It will automatically get filled based on the value taken from the SHA Key.
- Then click “Add key” and that’s it. You have successfully configured SSH.
- Now try cloning again. Git will ask for a password. Give the password you have given in Step 2.
And that’s all. Now you will be able to access the repo without any issues.