Getting permission denied (public key) on gitlab

I found this after searching a lot. It will work perfectly fine for me.

  1. Go to "Git Bash" just like cmd. Right click and "Run as Administrator".
  2. Type ssh-keygen
  3. Press enter.
  4. It will ask you to save the key to the specific directory.
  5. Press enter. It will prompt you to type password or enter without password.
  6. The public key will be created to the specific directory.
  7. Now go to the directory and open .ssh folder.
  8. You'll see a file id_rsa.pub. Open it on notepad. Copy all text from it.
  9. Go to https://gitlab.com/-/profile/keys or
  10. Paste here in the "key" textfield.
  11. Now click on the "Title" below. It will automatically get filled.
  12. Then click "Add key".

Now give it a shot and it will work for sure.


Step 1: Added a config file in ~/.ssh/config file which looks like

   User git
   Hostname gitlab.com
   IdentityFile ~/.ssh/id_rsa_gitlab
   TCPKeepAlive yes
   IdentitiesOnly yes

Step 2: Just clone the git repo WITHOUT sudo.
Documentation: https://gitlab.com/help/ssh/README#working-with-non-default-ssh-key-pair-paths

** sometimes you have the config in your ~/.ssh/config, but, IdentityFile path is not correct. you can check the file name like this ls ~/.ssh/. the file is normally is id_ed25519 for gitlab. thus the correct config is IdentityFile ~/.ssh/id_ed25519


I think the simple solution is to add private key to authentication agent (if your key is not ~/.ssh/id_rsa),

ssh-add ~/.ssh/<your private key>

You basically let the ssh-agent take care of it.

Additionally, you can add it permanently.


There is a very simple solution to this: instead of working with ssh - move to https. to do this: in your project folder you have a .git folder in there - you have a config file - open it in a text editor and change the line

url [email protected]:yourname/yourproject.git

to

url = https://gitlab.com/yourname/yourproject.git


make sure you are not running sudo git clone [email protected]:project/somethiing.git, otherwise ssh will look in /root/.ssh instead of the key you uploaded ~/.ssh/id_rsa