Fix SSH "Permission denied (publickey)"
Permission denied (publickey)Permission denied (publickey,password)Server refused our keyThe "Permission denied (publickey)" error means the SSH server would not accept your key, or you offered the wrong one. It is almost always one of a handful of causes — here is how to find which, and fix it.
Run the connection with -v (verbose): ssh -v user@server. The output shows exactly which keys were offered and why each was rejected — the fastest way to diagnose this.
Cause 1: wrong username
A key installed for one user will not work for another. Cloud images often use a specific default user — for example root, ubuntu, debian or almalinux — not your own name. Try the correct user for your distro.
Cause 2: the key is not on the server (or is the wrong one)
Your public key must be in ~/.ssh/authorized_keys on the server, for the user you log in as. If it is missing, add it (via console access or your host's panel). Make sure your client offers the matching private key:
ssh -i ~/.ssh/id_ed25519 user@your-server-ip
Cause 3: wrong file permissions (very common)
SSH refuses keys if the permissions are too open — this is a security feature. On the server, the .ssh folder and authorized_keys file must be locked down:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chown -R $USER:$USER ~/.ssh
On your local machine, your private key should be chmod 600 too.
Cause 4: keys only, and yours is not working
If password login is disabled (good hardening) and your key fails, you cannot fall back to a password — you must fix the key or use your host's console/recovery to add a working one. This is why you should always test a new key before disabling passwords.
The quick checklist
- Run
ssh -vand read which key is rejected and why. - Confirm you are using the correct username for your image.
- Make sure your public key is in the server's
~/.ssh/authorized_keys. - Fix permissions:
700on.ssh,600onauthorized_keys. - Point to the right private key with
-i.
Nine times out of ten this is either the wrong username or too-open permissions on ~/.ssh. Check those two first.
Cause 5: the SSH agent is offering the wrong key (or too many)
If your agent holds several keys, SSH may offer the wrong ones first and get rejected before reaching the right one — which can even trip a "too many authentication failures" cutoff. Force the correct key with -i and IdentitiesOnly=yes, or clean up your agent. See too many authentication failures.
Cause 6: the server rejects your key type or a home-directory permission
Newer OpenSSH releases refuse old key algorithms (like legacy RSA-SHA1) by default, so an older key can be silently declined. A world-writable home directory also makes SSH ignore authorized_keys. Generate a modern ed25519 key, and make sure your home directory isn't group/other writable.
How to prevent SSH key lockouts
- Always keep a working session open when changing SSH or permissions, and test the new key in a second terminal.
- Never disable password login until key login is confirmed working.
- Use a modern ed25519 key and keep private keys at chmod 600.
- Know where your host's VNC/console recovery is before you need it.
Related errors
If SSH refuses the connection entirely rather than rejecting your key, see SSH connection refused. New to SSH? Start with connecting to your server with SSH. For the full lockdown approach, see how to secure a Linux VPS.
Root access, done right
Our VPS plans include console recovery and full SSH access on a protected network — never get permanently locked out.
Frequently asked questions
What does "Permission denied (publickey)" actually mean?
The SSH server is set to accept key authentication, but none of the keys your client offered were accepted — usually the wrong key, the wrong user, or bad permissions on the server's ~/.ssh files.
How do I fix it if I am completely locked out?
Use your host's web console, VNC or recovery mode to log in outside SSH, then add your public key to ~/.ssh/authorized_keys and fix the folder permissions.
Why do file permissions matter for SSH keys?
SSH deliberately ignores keys when the .ssh folder or authorized_keys file is readable by others, to stop a compromised account from leaking keys. They must be 700 and 600 respectively.
Why does my old SSH key suddenly stop working after a server upgrade?
Newer OpenSSH versions disable older key algorithms (such as legacy RSA-SHA1) by default. A key that worked for years can be refused after an OS upgrade. Generate a modern ed25519 key and add its public part to authorized_keys.
Can having too many SSH keys cause "Permission denied (publickey)"?
Yes — the SSH agent offers keys one by one, and if it offers the wrong ones first, the server can reject you (or hit a max-attempts cutoff) before the right key is tried. Use ssh -i with IdentitiesOnly=yes to force the correct key.
Does a wrong home directory permission block SSH keys?
It can — if your home directory is group- or world-writable, SSH refuses to trust authorized_keys inside it as a security measure. Set your home directory to 755 (or stricter) and retry.
Related articles
Fix SSH "Connection refused" / "Connection timed out"
SSH "connection refused" vs "timed out" — what each means and the exact steps to fix them.
Read fix Linux & VPSFix "Permission denied" on Linux (chmod & chown Explained)
"Permission denied" on a script or file? Understand chmod/chown and fix it the right way.
Read fix Linux & VPSHow to Check CPU, RAM & Disk Usage on a Linux Server
The essential commands to check CPU, RAM and disk on Linux — and find what's eating them.
Read fix