DEV Community

Mạnh Đạt
Mạnh Đạt

Posted on

Fix Permission denied (public key) With Gitlab Windows

Recently I need to checkout a new repo from a self-hosted Gitlab server. I did the usual thing:

  • Generate a pair of ssh keys using ssh-keygen (since I'm on a new Windows machine). If you already have the ssh keys (try ls in your ~/.ssh and you see id_rsa and id_rsa.pub), then you don't need to generate the key pairs again.
  • Add it to the SSH key section of Gitlab

But still, I got the error:

Image description

Normally, it should work, right? Yes, I've done this countless time with GitHub, Gitlab, Bitbucket... and had no issue.

By the way, the account has 2FA enabled.

I don't know the reason for this but I have a workaround.

Access to Gitlab using personal access token

The workaround is to use Gitlab's personal access token. I haven't tried this on Github (since it works perfectly with SSH key) but I guess it should work too.

The first step is to generate a personal access token:

Image description

Then, enter the name of the token and check api

Image description

Then click on Create personal access token and you will get a token to use:

Image description

Now, head back to your repository and copy the standard https URL. It should look something like this:

https://git.some-server.com/group/repo/repo-name.git
Enter fullscreen mode Exit fullscreen mode

You need to modify that URL a bit using your newly created token:

https://<token_name>:<token>@git.some-server.com/group/repo/repo-name.git
Enter fullscreen mode Exit fullscreen mode

Now, I can use that url to clone the repo:

Image description

In case you already have the repo, you just need to update the remote url

git remote set-url origin https://<token_name>:<token>@git.some-server.com/group/repo/repo-name.git
Enter fullscreen mode Exit fullscreen mode

Then you can pull, push normally.

Conclusion

That's it! You can continue working on your repo again. I still don't know why adding ssh didn't work but using the personal access token is an acceptable work around.

One thing I don't like about this method is any one who can access to your machine can run:

git remote -v
Enter fullscreen mode Exit fullscreen mode

And see your access token. That person then can use the credentials to do what you can on your repo.

So, lock your screen when you are not at your computer :D


If you enjoy this post, make sure to check my blog out at https://datmt.com. I wrote about #programming stuffs there :)

Top comments (1)

Collapse
 
wahabachakzai profile image
Abdul Wahab Achakzai

Thanks, this solution helped fixing the issue.