Solving “Permission Denied” error on GitLab and GitHub

Aditya Tyagi
3 min readMay 19, 2021
Originally published at adityatyagi.com

If you are landing on this article, I am sure you must have exhausted everything out there. From blogs to hitting random scripts from Stack Overflow. So, here is how I resolved this issue:

I did the same — hitting one script after the other to get it working, but as someone beautifully quoted:

6 hours of online surfing can save you 5 minutes of reading documentation

– Do not remember the author, but read it on Twitter

I was setting up multiple ssh keys on Windows 10 (which is a definite pain in the ass), as compared to setting it up on Linux/MacOS.

Stumbled upon this beautiful article by Megan Lo where she wrote detailed steps on: How To Set Up Multiple SSH Keys on Your Computer. Go through the article once just to check you have performed all the steps.

If you still aren’t able to get the solution, check Generating a new SSH key and adding it to the ssh-agent and ensure that your key (not the .pub file) but the key (private file) is added to the ssh agent.

Whenever you add your ssh key, be it the .rsa version or the .ed25519 version, both will generate two files:

  1. A public key — has the .pub extension
  2. A private key — without the .pub extension, but with the same name

You need to ensure that the private key is added to the ssh agent. To do that you need to first ensure that the ssh agent is running. In windows, you can do that by:

eval `ssh-agent -s`

Make sure you hit this in the git bash and not in powershell or cmd. It might give you error like “eval is not a identifiable term”.

If you get the ssh-agent pid like this Agent pid 1764, then you are good to go. The PID can be different than this one.

Next you need to add the private key to the ssh-agent. This step also has to be done in the git bash.

ssh-add ~/.ssh/id_ed25519_gitlab_nh

Use ssh-add along with complete path to your ssh private key. That’s it.

Try cloning the repo and you should be good to go!

(If by any chance you are still not able to resolve this, check this article from the official GitHub Docs: Error: Permission denied (publickey) ) and also this from the GitLab official docs

How to copy ssh key in windows?

This script in the git bash will copy the ssh key to clipboard so that you can paste it to GitHub or GitLab. Ensure you copy and paste the ssh key with .pub extension only, because that’s the “Public Key”. The other key has to be added to the ssh-agent, as seen above.

cat <complete_path_to_ssh_key>.pub | clip

For Linux

cat <complete_path_to_ssh_key>.pub | xclip

For MacOS

cat <complete_path_to_ssh_key>.pub | pbcopy

Originally published on adityatyagi.com

--

--

Aditya Tyagi

Full Stack Engineer | JS, TS | NextJS, ReactJS | TailwindCSS | GraphQL