Chapter 5 · Lesson 1 of 10
Fix it

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.

By the end you will

Find which key, user or file permission makes the server refuse your SSH login, and fix it so the key is accepted.

Time
15 min
Level
Beginner
Check
4 questions
You need
  • 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:

bash
ssh -v deploy@203.0.113.10

Look at these lines in the output:

  • Offering public key: /home/you/.ssh/id_ed25519 shows which key files the client tried. If your key is not listed, the client never offered it.
  • Authentications that can continue: publickey confirms the server wants keys only.
  • no more authentication methods to try means 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:

bash
ssh -i ~/.ssh/id_ed25519 -o IdentitiesOnly=yes deploy@203.0.113.10

IdentitiesOnly=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:

bash
chmod 600 ~/.ssh/id_ed25519

If 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:

bash
sudo cat /home/deploy/.ssh/authorized_keys

Compare 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:

bash
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_keys

Only 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:

bash
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:

bash
echo 'ssh-ed25519 AAAA...your-public-key... you@laptop' | sudo tee -a /home/deploy/.ssh/authorized_keys

Replace 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:

bash
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.10 while 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 ~/.ssh resets 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 ubuntu or root, not for the user you created.

Troubleshooting#

SymptomLikely causeFix
Works as root, fails as deployKey only in root’s authorized_keysAdd the key to deploy’s file
Offering public key never lists your keyClient does not see that key fileUse -i with the full path
Log says bad ownership or modes.ssh or home writable by othersSet 700 on .ssh, 600 on the file
Fails right after a server rebuildNew machine, new keysRe-add the key and clear the old host key
Log says user not allowedAllowUsers or AllowGroups excludes youAdd the user, run sudo sshd -t, restart the service
Fails only from one laptopDifferent key on that laptopAdd that laptop’s public key

Quick reference#

bash
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"

Next#

Continue with Fix a full disk on a Linux server.

Check what you learned4 questions
1Which flag shows which keys your SSH client offers and why the server answers no?
2Your key is in /root/.ssh/authorized_keys but you log in as deploy. What happens?
3Which permission setup makes sshd ignore authorized_keys by default?
4What should you do before closing your only SSH session after changing sshd_config?

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

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios