DEV Community

Cover image for Quick way to solve Git problem: Permission denied
boolfalse
boolfalse

Posted on

Quick way to solve Git problem: Permission denied

This was tested with non-root user and works for GitHub & GitLab (for 2FA enabled as well).

You may encounter the following problem while your development:

git@gitlab.com: Permission denied (publickey,keyboard-interactive).
Enter fullscreen mode Exit fullscreen mode

In this article I will provide a way to solve this, and not only.

If you already have setup SSH key on your remote platform (such as GitHub), just skip this part.

Adding SSH to your Git account.

Run this command to generate public & private key files:

# during creation of SSH files you can leave passphrase empty
# here you can set the private-key & public-key file names/paths
ssh-keygen -t ed25519 -C "<GIT_ACCOUNT_EMAIL>"
Enter fullscreen mode Exit fullscreen mode

Print the output of public SSH file and copy the output content.

# let's assume we have setup default "id_ed25519.pub"
cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

We will do this for GitHub as an instance. But practically you can do the same for the other Git platforms as well.

Login to your account and go to the “Add SSH key” section. Use the copied public-key content for adding as a new SSH key into your account.

Test the SSH authentication for GitHub:

# for GitHub
ssh -T git@github.com

# for GitLab
ssh -T git@gitlab.com
Enter fullscreen mode Exit fullscreen mode

Fixing the issue:

  • Local cause:

Sometimes SSH authentication test fails, and you might get something like this:

git@gitlab.com: Permission denied (publickey,keyboard-interactive).
Enter fullscreen mode Exit fullscreen mode

For avoiding this, you can follow to these steps:

# cd into ".ssh" directory
cd ~/.ssh

# make sure you have private-key file octal permission as 600
stat -c "%a %n" *

# if not, then you can change the permission like this
# in our case we might have this file: "id_ed25519-gitlab"
sudo chmod 600 id_ed25519-gitlab

# [FREQUENTLY USED] if the permissions are correct, start your SSH agent
eval `ssh-agent -s`
# this will output something like this:
"Agent pid 21590"

# [FREQUENTLY USED] now add your SSH private-key as the authorized user
ssh-add ~/.ssh/id_ed25519-gitlab
# you should get something like this
"Identity added: /home/<LOCAL_USER>/.ssh/id_ed25519-gitlab (<ACCOUNT_EMAIL>)"

# now test the SSH authentication again
# you should get some success message, something like this:
"Welcome to GitLab, @<USERNAME>!"
Enter fullscreen mode Exit fullscreen mode
  • Connection cause:

Sometimes connection is not working between your machine and the Git server, but you may want to push your code somewhere remotely.
In this kind of cases you can add the alternative/another remote and push the project there:

git remote rename origin upstream
git remote add origin git@gitlab.com:<USERNAME>/<REPO_NAME>.git
git push origin <BRANCH>
Enter fullscreen mode Exit fullscreen mode

For existing repos (with HTTPS usage) change remote origin as SSH if needed:

git remote set-url origin git@github.com:<USERNAME>/<REPO_NAME>.git
Enter fullscreen mode Exit fullscreen mode

That’s it.

In the end, just in case here’s some useful resources which you may use:


If you liked this article, feel free to follow me here. 😇

To explore projects working with various modern technologies, you can follow me on GitHub, where I actively publicize much of my work.

For more info you can visit my website boolfalse.com

Thank you !!!

Top comments (0)