Permission denied (publickey)

Fixing 'Permission denied' (publickey) SSH errors on Linux

You're getting kicked out of SSH with 'Permission denied (publickey)'. Nine times out of ten it's wrong file permissions or the wrong key. Here's how to fix it fast.

1. Wrong permissions on ~/.ssh or authorized_keys

This is the #1 cause. SSH is paranoid about permissions. If your ~/.ssh directory or ~/.ssh/authorized_keys file has world-writable or group-writable permissions, OpenSSH flat-out refuses to use public key authentication. It's a security feature, not a bug.

# Fix permissions on the server (not client)
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 644 ~/.ssh/known_hosts  # this one's less critical but keep it sane

Make sure the .ssh directory and the key files are owned by you, not root or someone else. Run ls -la ~/.ssh to check. The owner column should show your username. If it's root, run chown -R youruser:youruser ~/.ssh.

Real-world trigger: You copied authorized_keys from a backup or another server using cp -r, and the permissions got mangled. SSH says nope.

2. Wrong key pasted into authorized_keys

People copy-paste public keys from emails, web UIs, or terminal output and accidentally grab extra whitespace, line breaks, or garbage characters. The key must be exactly one line, starting with ssh-rsa, ssh-ed25519, or ecdsa-sha2-nistp256.

Check the file with cat -A ~/.ssh/authorized_keys. You should see no ^M (carriage return) characters unless you're on a Mac. Each line should end with $. If you see ^M$, run dos2unix ~/.ssh/authorized_keys to strip the Windows line endings.

Quick test: Append the public key manually using:

echo 'ssh-ed25519 AAAAC3... your-comment' >> ~/.ssh/authorized_keys

Don't copy-paste from a web browser into a terminal — the formatting often breaks.

3. Server's sshd_config is blocking public key auth

Less common but still bites people. The SSH daemon on the server might have PubkeyAuthentication set to no or PasswordAuthentication set to yes when you're expecting key-only login.

Check the server's /etc/ssh/sshd_config for these lines:

PubkeyAuthentication yes
PasswordAuthentication no   # if you want key-only
AuthorizedKeysFile .ssh/authorized_keys

If you changed anything, restart sshd: sudo systemctl restart sshd (or sudo service ssh restart on older systems).

Note: Some cloud images (like Ubuntu on AWS) have PasswordAuthentication no by default but also disable root login with keys. Double-check PermitRootLogin if you're trying to log in as root.

Quick-reference summary table

CauseFixCheck command
Bad permissions on ~/.ssh or authorized_keyschmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keysls -la ~/.ssh
Corrupted or malformed public key in authorized_keysRe-add key with echo >> authorized_keyscat -A ~/.ssh/authorized_keys
sshd_config blocks publickey authSet PubkeyAuthentication yes, restart sshdgrep Pubkey /etc/ssh/sshd_config

If none of this works, run SSH in debug mode on the client: ssh -vvv user@host. Look for lines starting with debug1: that say things like Permission denied (publickey) or Offering public key. That tells you exactly which key the client is sending and where the server rejects it. Most of the time, the debug output shows the server complaining about bad permissions — that's your clue.

Related Errors in Linux & Unix
Fontconfig error: Cannot load default config file Fontconfig Error: Cannot Load Default Config File Fix Cannot Connect to X Server No Protocol Specified X Server Connection Error: No Protocol Specified Fix bash: command not found Fix 'bash: command not found' for ifconfig and other tools on Linux Oops: 0000 [#1] SMP Fix Kernel Oops on Driver Fault in Ubuntu 22.04 LTS

Was this solution helpful?

EP
Erropedia Team
Tech Support Editors
The Erropedia editorial team researches and documents real-world tech errors from across Windows, Linux, macOS, networking, databases, cloud platforms, and more. Every solution is reviewed for accuracy and updated as software and systems evolve.