Error: Permission denied (publickey) – GitHub Docs

Should the sudo command or elevated privileges be used with Git?

You should not use the sudo command or elevated privileges, such as administrator permissions, with Git. If you have a very good reason why you should use sudo, make sure you use it with every command (it’s probably best to use su to get a shell as root at that point). If you generate SSH keys without sudo and then try to use a command like sudo git push, you won’t use the same keys you generated.

Check that you are connecting to the correct server

Writing is difficult, we all know that. Pay attention to what you write; You will not be able to connect to “githib.com” or “guthub.com”. In some cases, a corporate network can also cause problems resolving the DNS record.

To make sure you are connecting to the correct domain, you can enter the following command

: $ 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.

The connection must be made on port 22, unless ‘re overriding the configuration to use SSH over HTTPS.

Always use

the “git” user

All connections, including those from remote URLs, must be made as the “git” user. If you try to connect with your GitHub username, it will fail:

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

If your connection failed and you are using a remote URL with your GitHub username, you can change the remote URL to use the “git” user.

You need to verify your connection by typing:

$ssh -T git@github.com > Hello USERNAME! You’ve successfully authenticated…

Make sure you have a key that is being used

The ssh-add command should print a long string of numbers and letters. If you don’t print anything, you’ll need to generate a new SSH key and associate it with GitHub.

Get more details

You can also verify that the key is being used by trying to connect to git@github.com:

$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: Testing private key: /Users/YOU/.ssh/id_rsa > debug1: Testing private key: /Users/YOU/.ssh/id_dsa > debug1: No more authentication methods to test. > Permission denied (publickey).

In that example, we didn’t have any keys for SSH to use. The “-1” at the end of the “identity file” lines means that SSH could not find a file to use. Later, the lines “Testing private key” also indicate that no files were found. If a file existed, those lines would be “1” and “Offering public key”, respectively:

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

Verify that the public key is associated with

your account You must provide your public key to

GitHub to establish a secure connection.

If you don’t see your public key on GitHub, you’ll need to add your SSH key to GitHub to associate it with your computer.