Fix SSH permission denied (publickey)
Permission denied (publickey) means the server refused every key your SSH client offered, usually because of the wrong user, key or file permissions.
- Updated Nov 27, 2026
- Markdown
Find which key, user or file permission makes the server refuse your SSH login, and fix it so the key is accepted.
- A server you cannot log in to over SSH
- Some other way in, such as a provider web console
You run ssh deploy@203.0.113.10 and the client prints Permission denied (publickey). The server was reachable and spoke SSH, but it refused every key you offered, and it allows no password fallback. The fix is nearly always one of three things: you used the wrong user, your client offered the wrong key, or the server is ignoring the key file because of its permissions.
How it works#
SSH key login is a challenge. Your client offers a public key, the server looks for it in ~/.ssh/authorized_keys of the user you asked to become, and if it finds a match it sends a challenge that only the private key can answer. The full flow is in log in with SSH keys.
Every step of that can fail without a helpful message. The server does not tell you which part was wrong, because that would help an attacker. So the error is the same whether the user does not exist, the key is not listed, or the file is unreadable. Your job is to remove the possibilities one at a time, and the client side is the cheapest place to start.
Step 1: Read the verbose output#
Run the same command with -v:
ssh -v deploy@203.0.113.10Look at these lines in the output:
Offering public key: /home/you/.ssh/id_ed25519shows which key files the client tried. If your key is not listed, the client never offered it.Authentications that can continue: publickeyconfirms the server wants keys only.no more authentication methods to trymeans every offered key was refused.
If the list of offered keys is empty or wrong, the problem is on your side. If the right key was offered and refused, the problem is on the server.
Step 2: Check the user name#
The user must exist on the server and must be the one holding your key. The most common slip is logging in as root or ubuntu when your key was installed for deploy. Try the user your provider or setup notes name, and compare with ssh -v output, which prints Authenticating to 203.0.113.10:22 as 'deploy'.
Step 3: Offer the right key#
If you have several keys, point the client at one explicitly:
ssh -i ~/.ssh/id_ed25519 -o IdentitiesOnly=yes deploy@203.0.113.10IdentitiesOnly=yes stops the client from trying every key in your agent first. Some servers close the connection after a few failed attempts, so a crowded agent can produce this error even when the right key is in it. If this command works, put the same settings in your SSH config file so you never type them again.
Also check the private key file itself. It must be readable only by you:
chmod 600 ~/.ssh/id_ed25519If the mode is too open, the client refuses to use the key and says so with a UNPROTECTED PRIVATE KEY FILE warning.
Step 4: Check authorized_keys on the server#
Get onto the server through your provider’s web console or rescue mode, then look at the file for the user:
sudo cat /home/deploy/.ssh/authorized_keysCompare it with your public key from your own machine (cat ~/.ssh/id_ed25519.pub). The whole key must be on one line, starting with the key type and ending with an optional comment. A key that was pasted with a line break in the middle is a common cause, and so is a key copied from the wrong file (the private key, or a key from another laptop).
Step 5: Fix the permissions#
By default sshd runs with StrictModes yes, which makes it ignore key files that other users could modify. The home directory, the .ssh folder and the file all matter. These are the usual safe settings, run as root or with sudo:
sudo chown deploy:deploy /home/deploy/.ssh /home/deploy/.ssh/authorized_keys
sudo chmod 700 /home/deploy/.ssh
sudo chmod 600 /home/deploy/.ssh/authorized_keysOnly run chown on those two exact paths. Do not run a recursive chown -R or chmod -R on a home directory, since that can break other files. Make sure the home directory itself is not writable by group or others: ls -ld /home/deploy should not show a w in the group or other columns.
Step 6: Read the server log#
If everything looks right, the server’s own log gives the reason. On Ubuntu and Debian:
sudo journalctl -u ssh --since "10 minutes ago"On other distributions the unit may be called sshd. Lines such as Authentication refused: bad ownership or modes for directory point straight at Step 5. User deploy not allowed because not listed in AllowUsers means the AllowUsers line in /etc/ssh/sshd_config excludes you. See read logs with journalctl for more filters.
Step 7: Add the key if it is missing#
With a working session or the console, append your public key to the right user’s file:
echo 'ssh-ed25519 AAAA...your-public-key... you@laptop' | sudo tee -a /home/deploy/.ssh/authorized_keysReplace the placeholder with your real public key. Use -a so you add to the file and do not overwrite keys that are already there.
Check it worked#
From your own machine, open a new connection:
ssh deploy@203.0.113.10 'whoami'It should print deploy with no password prompt. If you changed sshd_config, validate it first with sudo sshd -t and keep your existing session open until a second one logs in successfully.
Prevent it#
- Keep a second way in, such as the provider console, before you touch SSH settings.
- Add keys with
ssh-copy-id deploy@203.0.113.10while password login still works, then follow disable SSH password login. - Record user names and key choices in your SSH config file.
Edge cases and gotchas#
- Agent forwarding mix-ups: An agent with many keys can exhaust the server’s allowed attempts before your key is tried. Use
IdentitiesOnly=yes. - SELinux contexts: On Fedora or RHEL hosts, a file created in the wrong context can be refused.
restorecon -R -v ~/.sshresets the labels. - Encrypted home directories: If the home directory is encrypted and unlocked only at login, sshd cannot read the key file before you are in.
- Key type rejected: Very old RSA with SHA-1 signatures is disabled on recent servers. Use an ed25519 key.
- Wrong server: After a rebuild, the same IP may belong to a new machine with none of your keys. Check the host with the provider dashboard.
- Provider keys: Some providers inject keys only for a default user, such as
ubuntuorroot, not for the user you created.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
| Works as root, fails as deploy | Key only in root’s authorized_keys | Add the key to deploy’s file |
Offering public key never lists your key | Client does not see that key file | Use -i with the full path |
| Log says bad ownership or modes | .ssh or home writable by others | Set 700 on .ssh, 600 on the file |
| Fails right after a server rebuild | New machine, new keys | Re-add the key and clear the old host key |
| Log says user not allowed | AllowUsers or AllowGroups excludes you | Add the user, run sudo sshd -t, restart the service |
| Fails only from one laptop | Different key on that laptop | Add that laptop’s public key |
Quick reference#
ssh -v deploy@203.0.113.10
ssh -i ~/.ssh/id_ed25519 -o IdentitiesOnly=yes deploy@203.0.113.10
sudo chmod 700 /home/deploy/.ssh
sudo chmod 600 /home/deploy/.ssh/authorized_keys
sudo sshd -t
sudo journalctl -u ssh --since "10 minutes ago"Related lessons#
- Log in with SSH keys: how the key challenge works and how to install a key properly.
- SSH config file shortcuts: pin the user and key per host.
- Read logs with journalctl: find the server’s reason for refusing you.
- Disable SSH password login: the setting that makes this error appear, done safely.
- Protect SSH with fail2ban: why repeated failures can get your own IP banned.
Next#
Continue with Fix a full disk on a Linux server.
Check what you learned
Frequently asked questions
What does Permission denied (publickey) mean?
The server accepts only key login and none of the keys your client offered matched an entry in the authorized_keys file of the user you tried to log in as. The fault is the user name, the key, or the file permissions on the server.
Why does SSH work as root but not as my deploy user?
The key was probably added to root's authorized_keys only. Each user has their own ~/.ssh/authorized_keys, so the public key must be in the file of the user you log in as.
Why does ssh say Permission denied (publickey) when my key is correct?
The server may be ignoring the file because ~/.ssh or authorized_keys is writable by other users, or because your client offered a different key than you expect. Run ssh -v to see which keys were offered.
How do I fix SSH if I am locked out of the server?
Use the provider web console or a rescue mode to log in, then add your public key to the right user's authorized_keys and fix the permissions.
Sources
Keep going: guides and tutorials
- Docker Container Restarting in a Loop: 5 Causes, Fixed Five real reasons a Docker container keeps restarting, each reproduced with its exit code, the command that finds it and the fix. Then the same crash on Levelrail.
- Our Health Check Failed and the Deploy Went Live Anyway A postmortem from our own dogfood run: a failed readiness probe still received traffic. What broke, why two correct parts added up to a bug, and the fix.
- Roll Back a Failed Docker Deploy Without Downtime Ship a broken release on purpose, watch your users stay on the old version, then roll back with one command. Real commands, real output, self-hosted.