Fix the Error: Permission denied

Fix the error: permission denied

If you encounter an error: permission denied it means your server rejected your connection. Let’s first see what causes this error and then see how to fix it. Unless and until you have a valid un-avoidable reason to use sudo command you can use but you shouldn’t be using it with Git. Similarly, if you are generating the SSH keys without sudo and then try to use a command like ‘sudo git push’, you will not be able to use the same keys you generated.

Ensure Server Connection:

To ensure you are connecting to the right domain you can use the below, we at times make typos. Also, note that you connect to port 22 unless you are overriding settings to use SSH over HTTPS.

$ ssh -vT git@github.com
> OpenSSH_8.1p1, LibreSSL 2.7.3
> debug1: Reading configuration data /Users/you/.ssh/config
> debug1: Reading configuration data /etc/ssh/ssh_config
> debug1: /etc/ssh/ssh_config line 47: Applying options for *
> debug1: Connecting to github.com port 22.

Use the “git” user:

Make sure all the connection is made as a “git” user. You will encounter an error: permission denied when you try to connect with the GitHub username.

$ ssh -T GITHUB-USERNAME@github.com
> Permission denied (publickey).

In case you are using a remote URL and the connection failed, you can simply change the remote URL to use the “git” user and also verify the connection.

$ ssh -T git@github.com
> Hi username! You've successfully authenticated...

Check that you have a working key

You can use the installed GitHub Desktop to clone repositories without dealing with SSH keys.

  • Enable ssh-agent, if you are using Git Bash:

# start the ssh-agent in the background

$ eval "$(ssh-agent -s)"
> Agent pid 59566

Enable ssh-agent, if you are using a different terminal like git for windows:

# start the ssh-agent in the background

$ eval $(ssh-agent -s)
> Agent pid 59566
  • Check that you have generated and loaded a private key into SSH.
$ ssh-add -l -E sha256

> 2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ/Users/USERNAME/.ssh/id_rsa (RSA)

A long string of numbers and letters should be printed by ssh-add command. If it fails to print then you must generate a new SSH key and link it to GitHub.

Reminder: The default private keys (/.ssh/id rsa and /.ssh/identity) are added to the SSH authentication agent on most systems. Unless you override the file name when generating a key, you should not need to run ssh-add path/to/key.

More information:

Try connecting to git@github.com to see if the key is being used there as well:

$ ssh -vT git@github.com
> ...
> debug1: identity file /Users/you/.ssh/id_rsa type -1
> debug1: identity file /Users/you/.ssh/id_rsa-cert type -1
> debug1: identity file /Users/you/.ssh/id_dsa type -1
> debug1: identity file /Users/you/.ssh/id_dsa-cert type -1
> ...
> debug1: Authentications that can continue: publickey
> debug1: Next authentication method: publickey
> debug1: Trying private key: /Users/you/.ssh/id_rsa
> debug1: Trying private key: /Users/you/.ssh/id_dsa
> debug1: No more authentication methods to try.
> Permission denied (publickey).

In the above-explained example, we didn’t use keys for SSH.The “-1” at the end of the “identity file” lines means SSH failed to locate a file to use. Later, the “Trying private key” lines also state that no file was discovered. When the file is found the lines would read “1” and “Offering public key,” respectively.

$ ssh -vT git@github.com
> ...
> debug1: identity file /Users/you/.ssh/id_rsa type 1
> ...
> debug1: Authentications that can continue: publickey
> debug1: Next authentication method: publickey
> debug1: Offering RSA public key: /Users/you/.ssh/id_rsa

Check that your public key is linked to your account:

To establish a secure connection with GitHub, you must provide your public key.

  1. Start the command line.
  2. Begin the SSH agent in the background.
$ ssh-agent -s

> Agent pid 59566
  • Find your public key fingerprint and make a note of it.
$ ssh-add -l -E sha256

> 2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ /Users/USERNAME/.ssh/id_rsa (RSA)
  • Select your profile picture, then click Settings in the top-right corner of any page.
  • Click SSH and GPG keys in the sidebar’s “Access” section.
  • Compare the output from the ssh-add command with the list of SSH keys.

If your public key isn’t available in GitHub, you’ll need to add your SSH key there in order to link it to your computer.

To get more updates you can follow us on Facebook, Twitter, LinkedIn

Subscribe to get free blog content to your Inbox