How to fix "Permission denied (publickey)"

Tested: we reproduced this error and confirmed the fix on Ubuntu 24.04 with OpenSSH 9.6p1, client and server, November 29, 2026.

Permission denied (publickey) means the SSH server rejected every key your client offered, or you offered none. Start by seeing what SSH actually sends:

ssh -v user@example.com 2>&1 | grep -E "Offering|Authentications|No more"

In our test with the wrong key, that printed Offering public key: ./wrong ED25519 SHA256:... followed by No more authentication methods to try, then user@127.0.0.1: Permission denied (publickey). The word in brackets is the list of login methods the server allows. publickey alone means passwords are off, so only a key will get you in.

Find your cause

We set up an OpenSSH 9.6 server that only accepts keys and tried each of these. All of them produced the same Permission denied (publickey) message:

What was wrong How to check Fix
The server doesn’t have your public key ssh -v shows your key offered, then rejected Add it to ~/.ssh/authorized_keys on the server
SSH offered a different key ssh -v names a key you didn’t expect ssh -i ~/.ssh/your_key or IdentityFile in your config
Wrong username The user before @ Use the account the key was added to (ubuntu, ec2-user, git for GitHub)
Your key isn’t in a default file or loaded in the agent ssh-add -l doesn’t list it and ssh -v offers nothing ssh-add ~/.ssh/your_key
Your private key is readable by others SSH prints Permissions 0644 ... are too open chmod 600 ~/.ssh/your_key
Server’s ~/.ssh is writable by others Only visible in the server’s log chmod 700 ~/.ssh on the server
Server’s authorized_keys is owned by another user Server log chown youruser ~/.ssh/authorized_keys

Two things that did not cause the error in our test: an authorized_keys file at 664 and a home folder at 775 both still worked, because the group was the user’s own private group. SSH rejects them when other users share that group.

Fix: the server doesn’t have your key

Copy your public key to the server. If you can still log in another way (a password or a cloud console):

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@example.com

Or paste the contents of id_ed25519.pub as one new line in ~/.ssh/authorized_keys on the server. Cloud providers usually let you add a key in their dashboard instead.

Fix: SSH is offering the wrong key

Name the key explicitly:

ssh -i ~/.ssh/id_ed25519 user@example.com

To make it stick, add a block to ~/.ssh/config:

Host myserver
  HostName example.com
  User ubuntu
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes

IdentitiesOnly yes stops SSH from trying every key in your agent first, which can also cause Too many authentication failures.

Fix: the key isn’t loaded in ssh-agent

SSH tries the default files (~/.ssh/id_ed25519, ~/.ssh/id_rsa and a few others) plus whatever the agent holds. A key with any other name is only used if you load it or name it. Check and load it:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/your_key
ssh-add -l

In our test ssh-add -l listed the key as a line ending in (ED25519) once it was added. If ssh-add -l says Could not open a connection to your authentication agent, the eval line wasn’t run in this terminal. On a Mac, ssh-add --apple-use-keychain ~/.ssh/your_key keeps it across restarts; on Windows, the OpenSSH Authentication Agent service has to be running (we didn’t test either of these).

Fix: git@github.com Permission denied (publickey)

For GitHub the checks are the same, with three GitHub-specific ones. We didn’t test against GitHub itself; these follow GitHub’s troubleshooting page.

  1. Test the connection on its own: ssh -T git@github.com. A working key answers Hi USERNAME! You've successfully authenticated. Add -v (ssh -vT git@github.com) to see which key is offered, or -vvv for every step.
  2. Make sure the key is on your account: GitHub Settings, SSH and GPG keys. Compare fingerprints with ssh-add -l if you’re unsure which key is which.
  3. Check the remote with git remote -v. An SSH remote starts with git@github.com:. Always connect as the user git, never your GitHub username, and don’t run Git with sudo.

If you’d rather not use SSH at all, switching the remote to HTTPS (git remote set-url origin https://github.com/you/repo.git) avoids keys entirely and uses a token instead.

Fix: file permissions

On your machine:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519

On the server:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chown -R "$USER" ~/.ssh

If you don’t have a key yet, create one with ssh-keygen -t ed25519 -C "you@example.com" and then copy it to the server as above. Our guide to generating an SSH key covers each step.

Confirm it worked

Connect again. With GitHub, ssh -T git@github.com should answer Hi USERNAME! You've successfully authenticated. With your own server you should get a shell prompt. If it still fails and you run the server, its log has the real reason: sudo journalctl -u ssh on Ubuntu, or /var/log/auth.log. If the message changes to Connection refused, the server isn’t accepting SSH connections at all; see SSH connection refused.

FAQ

How do I see which key SSH is offering?

Run ssh -v user@host and look for lines starting with Offering public key. Each one names the key file. If none appear, SSH has no key to offer.

What permissions should ~/.ssh have?

700 on ~/.ssh, 600 on private keys and authorized_keys, 644 on .pub files. On the server, the home folder must not be writable by other users.

Why does git push work without sudo but fail with it?

sudo git push runs as root, so SSH looks for keys in root's ~/.ssh, not yours. Run Git as your own user. GitHub's troubleshooting page gives the same advice.

Should I just turn password login back on?

Only as a short-term way back in, and only if you control the server. Keys are safer; fix the key and turn password login off again.