Fix: Permission denied (publickey) in SSH on Linux
SSH rejects your key because permissions on ~/.ssh or its contents are too open, or the wrong key is offered. Here's how to fix it.
Quick answer
Run
chmod 700 ~/.ssh && chmod 600 ~/.ssh/id_rsa ~/.ssh/id_ed25519 ~/.ssh/authorized_keys 2>/dev/null then reconnect. If it still fails, check that ~/.ssh/config points to the right key file.Why this happens
OpenSSH is paranoid about file permissions on purpose. If your private key is readable by anyone else (group or other), or if the .ssh directory itself is writable by group/other, the SSH client refuses to use that key. The server side also checks that ~/.ssh and ~/.ssh/authorized_keys aren't too open. This catches you most often after you copy keys manually with cp or mv from a backup — those commands don't preserve safe permissions. I've seen it happen right after a fresh Ubuntu install if you created the key as root and moved it to your user folder.
Step-by-step fix
- Fix local key permissions
Run these commands on the client machine (the one you're SSHing from):
Thechmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa ~/.ssh/id_ed25519 ~/.ssh/authorized_keys 2>/dev/null2>/dev/nullswallows errors for files that don't exist — not all users have every key type. The 700 on the directory prevents anyone else from listing or writing to it; the 600 on key files makes them owner-read/write only. - Check the key identity
Run
This shows exactly which key file SSH is trying. If it's the wrong one, you'll see something likessh -v user@host 2>&1 | grep -i "Offering|identity"Offering public key: /home/you/.ssh/some_other_key. Fix by specifying the correct key with-i /path/to/correct_keyor edit~/.ssh/config(see below). - Fix server-side permissions (if you can still log in via other method)
If you have password or console access to the server, fix the remote~/.ssh:
Then double-check that the public key inchmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys ~/.ssh/id_rsa.pubauthorized_keysmatches your private key. Runssh-keygen -lf ~/.ssh/id_rsa.pubon client andssh-keygen -lf ~/.ssh/authorized_keyson server — the fingerprints must match. - Test connection
Look forssh -v user@host 2>&1 | tail -20Authentication succeeded. If you still seePermission denied (publickey), go to alternative fixes below.
Alternative fixes
Wrong key file being offered
Sometimes SSH tries the default key (id_rsa) but you need a different one. Create or edit ~/.ssh/config with this block:
Host myserver
HostName 192.168.1.100
User myuser
IdentityFile ~/.ssh/my_custom_keyThen just run ssh myserver. The IdentityFile directive forces SSH to use that key. This is cleaner than remembering -i flags.Server's known_hosts mismatch
If you rebuilt a server and its host key changed, SSH will silently fail with Permission denied instead of the usual warning. Fix by removing the old host key:
ssh-keygen -R hostname_or_ipThen reconnect.Agent forwarding issues
If you're hopping through a bastion host and the agent isn't forwarded, your keys won't be available. On the bastion, verify with ssh-add -l. If it shows The agent has no identities, you need to either forward the agent (ssh -A) or copy your key to the bastion temporarily (bad practice — don't do it).
Prevention tip
Never copy SSH keys with cp or scp and assume permissions are correct. Always run chmod 600 ~/.ssh/id_* && chmod 700 ~/.ssh after any key transfer. A one-liner you can alias: alias sshfix='chmod 700 ~/.ssh && chmod 600 ~/.ssh/id_* ~/.ssh/authorized_keys 2>/dev/null'. Run it before every SSH session for the next week — you'll build the muscle memory.
Was this solution helpful?