“Permission denied (publickey)”: fixing SSH and git key errors

The server turned down every key your computer offered. Usually the right key exists but wasn’t offered, was offered to the wrong user, or is sitting in a file the server refuses to read.

What the message means

user@host: Permission denied (publickey).
fatal: Could not read from remote repository.

SSH tried to log you in with a key pair. Your computer offered one or more public keys; the server checked each against the list of allowed keys for that account and accepted none. The word in brackets is the list of login methods the server still allows, so (publickey) also tells you password login is switched off. Typing a password again won’t help.

Step 1: see what ssh is actually doing

ssh -v [email protected]          # a server
ssh -vT [email protected]              # a git host (same idea for GitLab, Bitbucket)

Read the lines near the end:

You seeIt means
No Offering public key lines at allssh found no key to offer: wrong file name, key not created, or agent empty.
Offering public key: ~/.ssh/id_rsa then nothing about yoursA different key is being offered. Tell ssh which one to use.
Offering … Server accepts key then sign_and_send_pubkey: signing failedThe key has a passphrase and no agent is available, or a hardware key isn’t plugged in.
Your key is offered and the next line is Authentications that can continue: publickeyThe server doesn’t know this key for this user. Fix the server side.
Load key …: bad permissionsYour private key file is readable by others. See the key permissions guide.

Fixes on your computer

Host github.com
    User git
    IdentityFile ~/.ssh/work_ed25519
    IdentitiesOnly yes

Host myserver
    HostName 203.0.113.10
    User deploy
    IdentityFile ~/.ssh/deploy_ed25519

IdentitiesOnly yes stops ssh offering every key it knows. Servers give up after a handful of attempts (Too many authentication failures), so a full agent can lock you out before the right key is tried.

Fixes on the server

Git host checklist

Cannot even reach the host (Connection timed out)? See ssh Connection timed out port 22. Scary host-key warning instead of a publickey denial? See REMOTE HOST IDENTIFICATION HAS CHANGED.

Related

ssh Connection refused port 22 · fatal: Authentication failed for (Git HTTPS)

Questions

What does Permission denied (publickey) mean?

The SSH server rejected every public key your computer offered, and password login is turned off. Either the right key was not offered, or the server does not have that key listed for the user you are logging in as.

How do I fix Permission denied (publickey) for git push?

Run ssh -vT [email protected] (or your host) to see which key is offered. Make sure the remote uses the user git, add your key's .pub line to your account's SSH keys, load the key with ssh-add, and if you have several keys set IdentityFile and IdentitiesOnly yes in ~/.ssh/config.

Why does ssh not use my key?

ssh only tries default file names like id_ed25519 and id_rsa plus whatever the agent holds. A key with another name must be named with -i or IdentityFile in ~/.ssh/config, and a private key readable by others is ignored.

Can I just turn password login back on?

You can, but key login is safer. Fix the key instead: add the public key to ~/.ssh/authorized_keys with chmod 600 on the file and 700 on ~/.ssh.

Published .

Try it

Related guides

Newest guides