How to fix ” Permission denied (publickey) ” issue in Gitlab

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

    1. Run CMD/Powershell/Terminal with administrative (sudo) privilege. (In windows run cmd as administrator. In linux execute ‘sudo su’ to get root privilege).
    2. 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):
    3. 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):
    4. The public key will be created to the specific directory.
    5. Now go to the directory you have specified in Step 2 and open .ssh folder.
    6. You’ll see a file id_rsa.pub. Open it on notepad. Copy all text from it.
    7. Go to https://gitlab.com/profile/keys
      Here you can see all the SSH keys specified so far. Paste the copied key.
    8. Now click on the “Title” below. It will automatically get filled based on the value taken from the SHA Key.
    9. Then click “Add key” and that’s it. You have successfully configured SSH.
    10. 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.

Muhammed Afsal Villan
Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.

31 COMMENTS