SSH "Permission denied (publickey)": how to find the real cause
Fix SSH "Permission denied (publickey)": read the server log, fix ~/.ssh permissions, check the key and sshd_config, and handle SELinux on RHEL.
by OhSudo ·
You try to log in with your SSH key and get the shortest error message in Linux:
guest@203.0.113.10: Permission denied (publickey).(publickey) means the server only accepts key authentication — and none of the keys your client offered was accepted. The message doesn’t say why, on purpose: telling a stranger the reason would help attackers. The reason is written on the server, in the SSH daemon’s log. Start there whenever you can; guessing wastes a lot more time.
TL;DR
ssh -v user@server # client: which keys are offered?sudo journalctl -u ssh -n 20 # server log (Ubuntu/Debian)sudo journalctl -u sshd -n 20 # server log (RHEL/Rocky/Alma)chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys # server: the usual permission fixchmod go-w ~ # server: home not writable by others1. Read what the server says
You need another way into the server — a console from your cloud provider, another user with access, or a session that’s still open. Then read the SSH log:
# Ubuntu / Debiansudo journalctl -u ssh -n 30sudo grep sshd /var/log/auth.log | tail -n 20
# RHEL / Rocky / AlmaLinuxsudo journalctl -u sshd -n 30sudo grep sshd /var/log/secure | tail -n 20Each section below starts with the log line that points to it.
From the client side, ssh -v (or -vvv for more) shows which keys it tries and how the server answers. Look for Offering public key: … followed by Authentications that can continue: publickey — the server saw the key and refused it.
2. Bad permissions on ~/.ssh
Authentication refused: bad ownership or modes for directory /home/guest/.sshAuthentication refused: bad ownership or modes for file /home/guest/.ssh/authorized_keysThe most common cause on servers. With the default StrictModes yes, sshd ignores your keys if other users could modify them — a group- or world-writable ~/.ssh, authorized_keys, or even the home directory itself. Check:
ls -ld ~ ~/.ssh ~/.ssh/authorized_keysdrwxr-x--- 6 guest guest 4096 Nov 1 10:00 /home/guestdrwxrwxr-x 2 guest guest 4096 Nov 1 10:00 /home/guest/.ssh-rw-rw-r-- 1 guest guest 102 Nov 1 10:00 /home/guest/.ssh/authorized_keysFix it — on the server, as the user who logs in:
chmod go-w ~chmod 700 ~/.sshchmod 600 ~/.ssh/authorized_keysIf the files were created by root (for example with sudo), fix the owner too: sudo chown -R guest:guest /home/guest/.ssh. No restart is needed — try logging in again.
3. The server doesn’t have your key
If the log shows nothing about modes, the key the client offers is probably not in authorized_keys. Compare fingerprints:
# on your computerssh-keygen -lf ~/.ssh/id_ed25519.pub
# on the serverssh-keygen -lf ~/.ssh/authorized_keysIf your fingerprint isn’t in the server’s list, add the public key. From your computer, while you still have another way to log in:
ssh-copy-id -i ~/.ssh/id_ed25519.pub guest@203.0.113.10Or paste the content of id_ed25519.pub as one line at the end of ~/.ssh/authorized_keys on the server — a key broken over several lines doesn’t work.
If you have several keys, tell SSH which one to use, so it doesn’t offer the wrong ones first:
ssh -i ~/.ssh/id_ed25519 -o IdentitiesOnly=yes guest@203.0.113.10To make it permanent, put IdentityFile and IdentitiesOnly yes in a Host block of your ~/.ssh/config.
4. Your private key is too open (client side)
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ WARNING: UNPROTECTED PRIVATE KEY FILE! @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@Permissions 0644 for '/home/guest/.ssh/id_ed25519' are too open.This time the client refuses to use your key, so it never reaches the server and you end up with “Permission denied (publickey)”. Private keys must be readable only by you:
chmod 600 ~/.ssh/id_ed255195. sshd is configured to refuse it
Check the settings the server is actually using (this includes files in /etc/ssh/sshd_config.d/):
sudo sshd -T | grep -Ei 'pubkeyauthentication|authorizedkeysfile|allowusers|denyusers|permitrootlogin'Things to look for:
pubkeyauthentication no— key logins are disabled.authorizedkeysfilepointing somewhere unexpected.allowusers/denyusersthat don’t include your user.- Logging in as
rootwithpermitrootlogin no.
After changing /etc/ssh/sshd_config, check the syntax and reload — keep your current session open until you’ve tested a new login:
sudo sshd -t && sudo systemctl reload ssh # Ubuntu / Debiansudo sshd -t && sudo systemctl reload sshd # RHEL / Rocky / AlmaLinuxNote
RHEL / Rocky / AlmaLinux — SELinux: if permissions and keys look perfect but logins still fail, ~/.ssh may have the wrong SELinux label — typical after restoring a home directory from a backup or moving it. Check sudo ausearch -m avc -ts recent | grep sshd and restore the labels with restorecon -Rv ~/.ssh.
6. Simply the wrong user
Cloud images log you in with a default user that’s not root — ubuntu on Ubuntu images, ec2-user on Amazon Linux, others on other distributions. The key is installed for that user only. Check your provider’s documentation for the image, and use ssh ubuntu@server instead of ssh root@server.
Shell, we have a problem!
Similar, but different: Received disconnect … Too many authentication failures means your client offered too many keys before the right one — use -i with IdentitiesOnly=yes (section 3). Permission denied (publickey,password) means password logins are allowed too, and both failed.
$ ./practice.sh
Practice this in the terminal
Open the Terminal Playground with this article’s scenario loaded. It is safe and runs in your browser.
Found a mistake? Let us know at contact@ohsudo.com.