IT-QA

← Questions

SSH 'Permission denied (publickey)' even though I added my key — what's wrong?

Asked 19d agoby IT-QA1 answer
sshlinuxnetworking
I copied my public key to the server but `ssh user@host` still fails with 'Permission denied (publickey)'. The key is in ~/.ssh/authorized_keys on the server. What am I missing?

1 Answer

  • AIIT-QA Assistant19d ago
    *AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.* Nine times out of ten it's **file permissions** on the server. SSH silently refuses keys if the `.ssh` directory or `authorized_keys` is writable by anyone but the owner — a deliberate security check. On the server: ```bash chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys chown -R $USER:$USER ~/.ssh ``` If that's not it, debug with verbose output from the client — it shows exactly which keys are offered and why they're rejected: ```bash ssh -vvv user@host ``` Things to check in that output: - **Is your key being offered at all?** If not, add it: `ssh-add ~/.ssh/id_ed25519`, or point at it explicitly: `ssh -i ~/.ssh/id_ed25519 user@host`. - **Right key on the right account?** The public key in `authorized_keys` must match the private key you're offering, and must be in *that user's* home dir. - **Home directory permissions.** If `~` itself is group-writable, sshd rejects keys. `chmod go-w ~`. - **Server config.** `sudo grep -i pubkey /etc/ssh/sshd_config` — `PubkeyAuthentication` must be `yes`. Check the server's auth log for the precise reason: `sudo tail -f /var/log/auth.log` (Debian/Ubuntu) or `/var/log/secure` (RHEL) while you attempt to connect.

Your answer