Skip to content
Loopedin Labs

Permission denied (publickey)

The server heard you, and refused every key you offered.

Mini Rush · practice

Three questions before you read

Guessing first makes the answers stick. No sign-in.

This one is about which key, which user and which file modes.

What it means

The client connected and the server offered only public-key authentication, but none of the keys your client tried were accepted. The network is fine: a firewall problem looks like a timeout, and a stopped sshd looks like Connection refused. This is an identity problem, either on your side or on the server's.

Usual causes

  • Wrong username. On cloud images it is often ubuntu, ec2-user or azureuser, not root.
  • Your public key is not in that user's ~/.ssh/authorized_keys, or it was pasted with a line break in it.
  • The client is not offering the right key: wrong -i path, key not loaded in the agent, or ~/.ssh/config points elsewhere.
  • Private key readable by others: the client prints UNPROTECTED PRIVATE KEY FILE and skips it.
  • Server-side modes: with StrictModes yes (the default), sshd ignores keys if the home directory, ~/.ssh or authorized_keys is writable by group or others, or owned by someone other than the user or root.
  • authorized_keys unreadable by the user, for example created with sudo and left owned by root.
  • Algorithm mismatch: OpenSSH 8.8+ clients no longer sign with SHA-1 ssh-rsa, so old servers fail with no mutual signature algorithm.

Debug commands

Client side, see what is actually offered:

ssh -v -i ~/.ssh/id_ed25519 -o IdentitiesOnly=yes user@host
ssh -G host | grep -E '^(user|hostname|identityfile) '   # effective config
ssh-add -l                                              # keys in the agent

In the -v output, look for Offering public key: and then either Server accepts key or nothing.

Server side (via console or another session), the log names the exact reason:

sudo journalctl -u ssh -u sshd --since '10 min ago'   # unit is ssh on Ubuntu/Debian, sshd on RHEL
sudo tail -n 50 /var/log/auth.log                   # /var/log/secure on RHEL family
sudo sshd -T | grep -i -E 'pubkeyauth|authorizedkeysfile|strictmodes'

Fixes

Set ownership and modes on the server for the target user:

sudo chown -R deploy:deploy /home/deploy/.ssh
chmod go-w /home/deploy
chmod 700 /home/deploy/.ssh
chmod 600 /home/deploy/.ssh/authorized_keys

On your machine, chmod 600 the private key. To install a key correctly, use ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host. For new keys, use ssh-keygen -t ed25519. Changes to authorized_keys need no sshd restart.

Common wrong fix

chmod 777 ~/.ssh or chmod 777 ~/.ssh/authorized_keys to "make sure sshd can read it". It does the opposite. StrictModes treats a group- or world-writable path as tampered with and refuses the key, because any local user could append their own. On the client, a world-readable private key is skipped for the same reason. Setting StrictModes no or re-enabling password login to get in are the same mistake at server scale. Tighten permissions and read the server log.