Permission Denied (publickey)

Hi @abhishek1110,

Thanks for the response and details.

The issue here is that you must create the project on GitLab before you can push to it as a remote origin. Without having a project created on GitLab.com, there is no “remote origin” available to push to.

make sure you have the correct access rights and the repository exists.

This error is basically Git saying “I tried to push to remote, but the remote origin does not exist”.

The solution is to create a blank project on your GitLab.com named work and then push to it. You can create a new project in the web interface, or you can use the Create Project API endpoint. Either way, if the project exists on GitLab.com before you push to it, you can get past this error.

Hi @gitlab-greg

Thanks for the solution.

Hi @gitlab-greg,

I am confused. You say above, one must create a project first using the portal or API, however, in your article here, you say one can run git push --set-upstream... to create a new project.

When you create a new repository locally, instead of going to GitLab to manually create a new project and then clone the repo locally, you can directly push it to GitLab to create the new project, all without leaving your terminal. If you have access rights to the associated namespace, GitLab will automatically create a new project under that GitLab namespace with its visibility set to Private by default (you can later change it in the project’s settings).
This can be done by using either SSH or HTTPS:

Does the referenced article relate to some other “version” or something?

TIA

Hi @woter324,

Thank you very much for pointing this out and asking an excellent question.

I honestly was not aware that this functionality exists, but I can confirm that following the steps here is a valid way to create a new project without having to use the UI or API:
https://docs.gitlab.com/ee/gitlab-basics/create-project.html#push-to-create-a-new-project

Knowing this, I believe the original poster’s (@abhishek1110) problem was actually related the result of not having the correct SSH public key added to the user’s GitLab.com account as indicated by Permission denied (publickey)

When seeing Permission denied (publickey) in errors that arise with git commands and GitLab.com, here are two options to help troubleshoot and identify the problem:

as Matt Hagemann said

All you do is move the SSH keys we’ve generated above to the root’s SSH directory located at /root/.ssh. If you try sudo git clone now

1 Like

Hi,

I have been experiencing the same issue with this type of setup. The public key is denied due to some permissions issue. I have tried the methods on the this post, but still experiencing issues. Any help would be greatly appreciated.

@Cybersecurelabs2020 If the public key is denied because of permissions issues, its likely that the permissions need to be changed on your local system.

https://help.ubuntu.com/community/SSH/OpenSSH/Keys#Permission_denied_.28publickey.29

1 Like

In my case I needed to switch from git remote add origin git@gitlab.com:user/the-repo.git to git remote add origin https://gitlab.com/user/the-repo and that worked for me, as the empty repository was already created in Gitlab.

The first try (which is the one the documentation shows) didn’t work for me even after changing my password in the git config --list since it was also wrong.

5 Likes

…same here - when using git@gitlab.com:user/the-repo.git I always get the error
git@gitlab.com: Permission denied (publickey)

when I change the remote origin from git@… to https://gitlab.com/user/the-repo.git everything works - why git@… doesn’t work? :worried:

1 Like

Hi,

please try the suggested steps from @gitlab-greg. SSH keys on your client need specific private permissions and the public key also needs to match your account’s key on GitLab.com. One way to verify the SSH connection only is to go for ssh -vvvv git@gitlab.com - the closed connection on success is intended, you won’t get shell access.

Cheers,
Michael

2 Likes

I think I got it!

Added this to my git config

Host gitlab.com
AddKeysToAgent yes
IdentityFile C:\Users\Hermann.ssh\id_ed25519

and some of that command(s) fixed it

  1. Set-Service ssh-agent -StartupType Manual
  2. git config --global core.sshCommand C:/Windows/System32/OpenSSH/ssh.exe
  3. ssh-agent.exe -s
  4. ssh-add -k C:\Users\Hermann.ssh\id_ed25519
    Enter passphrase for C:\Users\Hermann.ssh\id_ed25519:
    Identity added: C:\Users\Hermann.ssh\id_ed25519 (gitlab)
2 Likes

Yes this solution helped me a lot. Thank you @hsaenz! SSH is little complicated but HTTPS is quite easier.

2 Likes

tried everything but no success - ssh -T git@gitlab.com works fine but cant clone or push any repository
it has worked for years without problems but today it stopped working
copy all path so there are no errors in the address. tried couple of hours with different key, generated a lot new but same result. it looks like gitlab or microsoft broke something
what im suppose to do now ? we use it at work like almost every company and it just stopped working - cant do anything without git…
result of ssh -vvvv git@gitlab.com
EDIT: its working again for some repository but a few still have the same problem

Hi,

Can you share an example for a repository where this doesn’t work anymore? Any changes made to the user and SSH keys you are using in your clone request? Is there a pattern when these syncs do not work?

Cheers,
Michael

sure, but its a private repo -> this
its empty so nothing specific there but i cant understand why this doesnt work and others do

created a few new and the last one works for most of the company repositories,
its a ssh-rsa 4096 key, also tried a rsa 2048 and a ed25519
i removed all access tokens and all old ssh keys but nothing beside that ( maybe i should notice that the new windows update was installed a couple of days ago )

Hi! I’m sorry, could you help me? I’m trying to clone repositories from GitLab with SSH, but I receive this message: PTY allocation request failed on channel 0.}
I work on Windows.

1)win + r
2)control /name Microsoft.CredentialManager (copy and past,Enter) or Open credential Manager from startbar
3)remove git credential
4)now add your git remote
5)clone with url
6)automaticly pop-up open insert credential
7)Enjoy youy project

Had the same issue.

I had multiple public keys in my ~/.ssh/ directory: ~/.ssh/id_rsa.pub, ~/.ssh/id_rsa.gitlab.pub, ~/.ssh/id_rsa.planio.pub

I checked my public key in my SSH Keys settings section und gitlab.com and it matches my id_rsa.gitlab.pub key. So I thought everything should be fine. But still got the Permission denied error when try to push initial project.

Finally the problem was in my ~/.ssh/config file, where I wrongly put these lines:

    #
    # gitlab
    #

    Host gitlab.com
            Preferredauthentications publickey
            IdentityFile ~/.ssh/id_rsa

Changed the IdentityFile to ~/.ssh/id_rsa.gitlab and sure it worked!

If you’re running gitlab locally under the docker and run the container with port forwarding (e.g 2224:22) and tried every thing from internet, just try this:

Host gitlab.local
    Preferredauthentications publickey
    IdentityFile ~/.ssh/id_rsa_gitlab_local
    Port 2224

With Port option your git commands will be addressed right to the gitlab container
Instead of gitlab.local you can use any domain or IP address

1 Like

this already solve my problem for case like this, but can i expect the default gitlab creation is pointing to https by default?

This is not the first time i face push failure