Fix SSH Permission Denied with Public Key Authentication
SSH public key authentication fails with 'Permission denied' due to incorrect file permissions or configuration. This guide covers root causes and step-by-step fixes for Linux/Unix systems.
Symptoms
When attempting to connect via SSH using public key authentication, the connection fails with a 'Permission denied (publickey)' error. The user may have correctly generated and placed the public key on the server, but authentication still fails. Debug output shows the server rejecting the offered key.
Root Causes
The most common root causes are:
- Incorrect file permissions on the
~/.sshdirectory orauthorized_keysfile. - The
authorized_keysfile is owned by the wrong user or group. - The
sshd_configfile has strict settings likePubkeyAuthentication noorAuthorizedKeysFilepointing to the wrong location. - SELinux or AppArmor blocking access.
- Key format issues (e.g., old key format not supported).
Step-by-Step Fix
1. Check SSH Debug Output
Run SSH with verbose logging to see where the failure occurs:
ssh -vvv user@serverLook for lines like 'debug1: Authentication refused: bad permissions' or 'debug1: Offering public key'.
2. Verify File Permissions on Server
On the server, ensure the following permissions are set:
- The user's home directory should not be writable by group or others (e.g.,
chmod go-w ~). - The
~/.sshdirectory must have permissions700(drwx------). - The
~/.ssh/authorized_keysfile must have permissions600(-rw-------). - The
~/.sshdirectory and its contents must be owned by the user.
Fix with:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chown -R $USER:$USER ~/.ssh3. Check sshd_config
On the server, edit /etc/ssh/sshd_config and ensure these lines are present and uncommented:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no (optional, for key-only access)Then restart the SSH service:
sudo systemctl restart sshd4. Verify Key Format
Ensure the public key in authorized_keys is a single line starting with 'ssh-rsa', 'ssh-ed25519', etc. It should match the private key. Check with:
ssh-keygen -l -f ~/.ssh/id_rsa.pub5. Check SELinux/AppArmor
If SELinux is enforcing, restore contexts:
restorecon -Rv ~/.sshFor AppArmor, check logs in /var/log/syslog for denials.
Alternative Fixes
- If the key is in an old format, regenerate with
ssh-keygen -t ed25519. - If using a non-standard port, ensure the client specifies it with
-p. - Check if the server has
AllowUsersorDenyUsersdirectives that may block the user. - Try connecting from a different client to isolate client-side issues.
Prevention
- Always set correct permissions immediately after creating
.sshdirectory. - Use
ssh-copy-idto copy keys automatically with proper permissions. - Regularly audit
sshd_configand permissions on production servers. - Enable logging with
LogLevel VERBOSEin sshd_config for easier troubleshooting.
Additional Resources
Refer to the OpenSSH manual pages: man sshd_config, man ssh. For SELinux issues, see man restorecon.
Was this solution helpful?