rescueshell Start trial

SSH "Permission denied (publickey)": how to fix it

The server answered, and it refused the way you tried to log in. That is good news of a kind: the network path works and sshd is running. What is left is a mismatch between the key you offered and what the account on the device will accept. Check the client side first, then the device.

This page describes standard OpenSSH behaviour. The commands are the usual ones from the OpenSSH tools, and wording and log text can differ a little between versions and distributions.

What the message means

user@server.example.com: Permission denied (publickey).

The text in the parentheses lists the methods the server was still willing to try when it gave up, so you may see (publickey,password) or (publickey,keyboard-interactive). It does not say which part failed. In every case sshd was reached, the connection was set up, and your authentication was refused.

That tells it apart from the other common failures. A timeout means nothing answered. Connection refused means the host answered and nothing listens on that port; the timed out and refused how-to covers both. A changed host key warning stops before any login is tried. If you are not sure which of these you have, the first list in the lockout guide separates them.

1. Check which key you are offering

Run the same command again with -v. Add more vs for more detail.

ssh -v user@server.example.com

The ssh -v output reader can count and name the keys offered in a pasted log. To see which key files ssh would try before connecting, ssh -G prints them, and the ssh -G output reader picks them out of a paste. Both run in your browser and send nothing.

Read the lines about identities and authentication. In OpenSSH's usual output, look for these:

Point ssh at the right key

ssh -i ~/.ssh/work_ed25519 user@server.example.com

The same choice can live in ~/.ssh/config, so it applies every time:

Host server.example.com
    User user
    IdentityFile ~/.ssh/work_ed25519
    IdentitiesOnly yes

IdentitiesOnly yes tells ssh to offer only the keys named in the configuration or on the command line, and not every key the agent holds. It matters because servers limit how many attempts one connection may make. With many keys in the agent, the right one can come after the server has stopped listening, and the result is the same message.

If the line you got is Too many authentication failures instead, the server stopped the attempt before every key was tried. That line has its own page, SSH "Too many authentication failures". This page stays about the denied login and how to offer only the key you name.

Check the agent

ssh-add -l

This lists the keys the agent holds. The agent has no identities means it has none, and Could not open a connection to your authentication agent means no agent is running in this shell. A key with a passphrase that the agent has not loaded is asked for in the terminal, or skipped when ssh cannot ask. Load it with ssh-add ~/.ssh/work_ed25519. If the passphrase is not accepted, ssh prints a Load key line that ends in incorrect passphrase; see SSH Load key "incorrect passphrase". A line that says signing failed or Agent admitted failure to sign using the key is another case: ssh had a key and asked for a signature, and did not get one. See SSH "signing failed" and "Agent admitted failure to sign".

Also check that the file is the private key and that only you can read it. ssh ignores a private key that other users can read, and says so in the -v output with an UNPROTECTED PRIVATE KEY FILE warning. chmod 600 on the key file fixes it.

That warning is about the key file. A different message, Bad owner or permissions on followed by the path of a config file such as .ssh/config, means ssh would not read its client config. See SSH "Bad owner or permissions on .ssh/config".

Two other lines are about the path of a key rather than its permissions: no such identity (nothing exists at that path) and Load key ... invalid format (ssh could not read the file as a private key). They are often warnings before this error. See SSH "no such identity" and Load key "invalid format".

2. Check the account and the key on the device

If ssh offered the key you meant and it was still refused, the fault is on the device. You need another way onto it for this part: a console, someone on site, or a session you already have open. Where you can get to the device, check these.

The right user

The login name in user@host picks which account's key list is read. A key added for pi does not let you in as root, and a key added to your own account does not help when the command says admin@. Compare the name in your command, in your config's User line and in ~/.ssh/authorized_keys of the account you mean.

The public key is in the file

OpenSSH reads ~/.ssh/authorized_keys of that account by default. Each line holds one public key. Print the fingerprint of every line on the device, and of your own key on your machine, and compare:

# on the device, as the account you log in as
ssh-keygen -l -f ~/.ssh/authorized_keys

# on your machine
ssh-keygen -l -f ~/.ssh/work_ed25519.pub

The SHA256: text has to match one line. The SSH key fingerprint checker compares two in your browser and sends nothing. The usual finds are that the key was never added, that the wrong half was pasted (the file without .pub is the private key and does not belong here), or that a line was broken in two by a paste. Each key has to sit on one line, and the file has to keep the type, such as ssh-ed25519, at the start of it.

The modes on the directories and the file

With its default StrictModes yes, sshd ignores authorized_keys when the home directory, ~/.ssh or the file can be written by anyone other than the owner or root. The login then fails as if the key were missing. On the device, as the account that logs in:

ls -ld ~ ~/.ssh ~/.ssh/authorized_keys
chmod go-w ~
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

The owner of all three should be that account too. The server's own log says what it decided: on many systems sudo journalctl -u ssh (the service is sshd on some distributions), or the auth log under /var/log. A line about Authentication refused: bad ownership or modes settles it.

A rule or a recent change on the server

If it worked before and stopped, think about what changed. PasswordAuthentication no set before a key was in place, an AllowUsers or DenyUsers line that leaves your account out, or a different PermitRootLogin value for root all end in this same message. Check them with sudo sshd -T, which prints the settings sshd is using. For the case where you just changed a firewall or sshd and need a way back in, see locked out of SSH after a firewall change.

If the machine is a RescueShell device

A relay does not fix a missing or untrusted key. When you open a shell, the session still ends at the device's own SSH server, so the device has to trust the key you use. If the shell says the device did not accept your key, see the device did not accept your key. How keys are added and why registering one in the web app does not add it to an installed device is in SSH keys.

Try it on one device

The first device is free to try with no card, for 30 days from when it connects, not from when you sign up. Start a trial.