We earn commissions when you shop through the links below, at no extra cost to you. Affiliate disclosure

SSH · Beginner · 12 min

Fix SSH 'Permission denied (publickey)'

Fix SSH "Permission denied (publickey)": see which keys ssh offers, compare fingerprints, fix the user name, key modes and server permissions.

The server answered, but accepted none of your keys. This runbook reads ssh -v on the client and the sshd log on the server to tell a wrong key or user from a key the server ignored because of file permissions, and explains the OpenSSH penalty that turns repeated failures into "Connection closed". Commands were run with OpenSSH 10 on Debian and OpenSSH 9.9 on AlmaLinux 10, including the RHEL crypto policy that refuses short RSA keys.

See it run

A short replay of real output from commands in this guide, captured on an Ubuntu test machine (OpenSSH 10.2p1 client; OpenSSH 10.0p2 server in a throwaway Debian 13 container). The typing is replayed, and the host name, account and MAC addresses are redacted. Your output will differ, and a test machine is not a production server.

Text version of this demo
ops@web-01:~$ ssh -n -F config -i old-key lab true
deploy@127.0.0.1: Permission denied (publickey).

ops@web-01:~$ ssh -n -v -F config -i old-key lab true 2>&1 | grep -E 'Offering|Authentications that can continue|No more'
debug1: Authentications that can continue: publickey
debug1: Offering public key: old-key ED25519 SHA256:GXPePsTXg/4XSrnwXecz0ICVehttQEWZtcjaaTT0Eqo explicit
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.

ops@web-01:~$ ssh-keygen -lf old-key.pub; docker exec ob-ssh ssh-keygen -lf /home/deploy/.ssh/authorized_keys
256 SHA256:GXPePsTXg/4XSrnwXecz0ICVehttQEWZtcjaaTT0Eqo old-key (ED25519)
256 SHA256:issZnv3tv7dPP7RYhfuaEuqy218tUTqT9Kkj3GzzC5g ops@laptop (ED25519)

ops@web-01:~$ ssh -n -F config -i ops-key lab true
deploy@127.0.0.1: Permission denied (publickey).

ops@web-01:~$ docker logs ob-ssh 2>&1 | grep -m1 'Authentication refused'
Authentication refused: bad ownership or modes for directory /home/deploy

ops@web-01:~$ docker exec ob-ssh chmod 750 /home/deploy && ssh -n -F config -i ops-key lab 'echo logged in as $(whoami)'
logged in as deploy

Permission denied (publickey) means the SSH connection worked and the server talked to you, but it accepted none of the keys you offered. The cause is on one of two sides. Either the client offered the wrong key, or no key at all, or the server ignored the right key because of the user name or the permissions on its files. The client message is the same in every case. On RHEL-family servers the list in brackets names every login method the server still allows, for example Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password); the causes and the fixes are the same. This runbook uses ssh -v on the client and the server's log to find which one it is.

Commands and output were captured with OpenSSH 10.2p1 (client, Ubuntu 26.04) against a throwaway OpenSSH 10.0p2 server in a Debian 13 container, with one user deploy and two test keys. The server ran with LogLevel VERBOSE, which shows the key fingerprints in its log. The RHEL-family variants were run on 2026-11-08 with the same client against OpenSSH 9.9p1 on AlmaLinux 10.2 (a RHEL 10 rebuild, crypto policy DEFAULT).

0. Rules of engagement

  1. Keep a working session open while you change anything on the server. A second terminal is your way back in.
  2. Never copy private keys to the server. Only the .pub line goes into authorized_keys.
  3. Do not switch on password logins to get around the error. Find out which key the server expects instead.

1. See what the client offers

Where Permission denied (publickey) comes fromClient side: No Offering line (no key to offer) then Key in another file, or refused (use -i, or fix its permissions) Server has another key or user: Offering, no Server accepts key then Compare fingerprints (and check the user name) Server ignores the right key: StrictModes yes (home, .ssh or file writable by others) then Read the server log (it names the reason)Client sideNo Offering lineno key to offerKey in another file, or refuseduse -i, or fix its permissionsServer has another key or userOffering, no Server accepts keyCompare fingerprintsand check the user nameServer ignores the right keyStrictModes yeshome, .ssh or file writable by othersRead the server logit names the reason
The client message is the same in every case. ssh -v shows whether the client had a key to offer; the server log shows why a right key was still ignored.
# Verbose client: the user, the keys offered, and the server's answer to each
ssh -v USER@HOST 2>&1 | grep -E 'Authenticating to|Will attempt|Offering|Server accepts|Authentications that can continue|No more authentication'
# Real output from the test setup: the only key offered is not in authorized_keys
debug1: Authentications that can continue: publickey
debug1: Will attempt key: other ED25519 SHA256:vDZ0uxAFLC7PO2PIJofWvpUKJXUpNm6I23sgEinOv9A explicit
debug1: Offering public key: other ED25519 SHA256:vDZ0uxAFLC7PO2PIJofWvpUKJXUpNm6I23sgEinOv9A explicit
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
  • Offering public key with no Server accepts key after it: the server does not accept that key for that user. Go to section 3 and compare fingerprints.
  • No Offering line at all: the client has no key to offer. The key is in a file SSH does not try by default (use -i FILE or IdentityFile in ~/.ssh/config), or SSH refused to load it (section 2).
  • Authenticating to HOST as 'USER' (higher up in the same output) shows the user name actually used. A User line in ~/.ssh/config can change it.

When it works, the line after the offer is Server accepts key, and SSH ends with Authenticated to HOST using "publickey".

2. The client refused its own key

SSH will not use a private key that other users can read:

# Real output from the test setup: the private key file had mode 0644
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'good' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "good": bad permissions
deploy@127.0.0.1: Permission denied (publickey).
# Private key readable only by you; the .ssh folder only accessible by you
chmod 600 ~/.ssh/KEYFILE
chmod 700 ~/.ssh

Copying keys from Windows or from a zip file often leaves them at 0644 or 0777. On a Windows client (OpenSSH for Windows), the same check uses the file's access list instead: remove access for other users in the file's Security settings.

3. The server has a different key, or a different user

Compare the fingerprint the client offers with the lines in the server's authorized_keys:

# Fingerprint of your public key (client) and of every key the server accepts for this user (server)
ssh-keygen -lf ~/.ssh/KEYFILE.pub
ssh-keygen -lf ~deploy/.ssh/authorized_keys
# Real output from the test setup: the client offered "old-key", the server only has "ops@laptop"
256 SHA256:yWDnim0N5Ngn/pbf0MW/FCO8WqnhdY0BIK8OGGdkKms ops@laptop (ED25519)
256 SHA256:vDZ0uxAFLC7PO2PIJofWvpUKJXUpNm6I23sgEinOv9A old-key (ED25519)
256 SHA256:yWDnim0N5Ngn/pbf0MW/FCO8WqnhdY0BIK8OGGdkKms ops@laptop (ED25519)

The server's log names the key that failed:

# Real output from the server log (LogLevel VERBOSE)
Failed publickey for deploy from 172.17.0.1 port 56794 ssh2: ED25519 SHA256:vDZ0uxAFLC7PO2PIJofWvpUKJXUpNm6I23sgEinOv9A
  • Wrong key: offer the right one (ssh -i ~/.ssh/KEYFILE), or add your public key with ssh-copy-id -i ~/.ssh/KEYFILE.pub USER@HOST from a machine that can still log in.
  • Wrong user: the key was added for deploy, but you log in as root or as your laptop user name. The log shows Failed publickey for root. Cloud images use fixed user names such as ubuntu, ec2-user or admin.
  • A broken line in authorized_keys: one key per line, no line breaks inside a key. ssh-keygen -lf prints an error for a line it cannot read.
  • An RSA key the system policy refuses (RHEL 10 and rebuilds): the default crypto policy sets RequiredRSASize 2048. A 1024-bit key in authorized_keys was offered and denied, and the server log said refusing RSA key: Invalid key length. A client forced to sign with SHA-1 (-o PubkeyAcceptedAlgorithms=ssh-rsa, as old libraries do) was denied without even offering the key. Check the policy with update-crypto-policies --show and sudo sshd -T | grep -i requiredrsasize, then create a new key (ssh-keygen -t ed25519) instead of weakening the policy.

4. The server ignored the right key: ownership and modes

With the default StrictModes yes, sshd ignores authorized_keys when the home folder, .ssh or the file itself can be written by other users, or belongs to someone else. The client still only says Permission denied (publickey). The reason is in the server log:

What was wrong on the test serverServer log
/home/deploy mode 777Authentication refused: bad ownership or modes for directory /home/deploy
/home/deploy/.ssh mode 777Authentication refused: bad ownership or modes for directory /home/deploy/.ssh
authorized_keys mode 666Authentication refused: bad ownership or modes for file /home/deploy/.ssh/authorized_keys
authorized_keys owned by root, mode 600Could not open user 'deploy' authorized keys '/home/deploy/.ssh/authorized_keys': Permission denied
# On the server, as root: give the files back to the user and close write access for others
chown -R USER:USER ~USER/.ssh
chmod go-w ~USER
chmod 700 ~USER/.ssh
chmod 600 ~USER/.ssh/authorized_keys

Group write alone is not always fatal: on the test server, a home folder with mode 775 was accepted, because the user's own group had no other members. Do not rely on that.

Where to read the server log:

  • Debian and Ubuntu: sudo journalctl -u ssh -n 50 or /var/log/auth.log.
  • RHEL, AlmaLinux, Rocky, Fedora: sudo journalctl -u sshd -n 50, or sudo tail -n 50 /var/log/secure where rsyslog is installed. The unit is sshd; journalctl -u ssh prints -- No entries --. On the AlmaLinux test server the bad-modes messages above were the same, word for word.
  • Add LogLevel VERBOSE in /etc/ssh/sshd_config (then reload sshd) to see the fingerprint of each failed key. On RHEL, put it in a file in /etc/ssh/sshd_config.d/ whose name sorts before 50-redhat.conf, because the first value read wins.

On SELinux systems (RHEL family), a .ssh folder created or restored with the wrong label is also ignored. restorecon -Rv ~USER/.ssh fixes the label. This was not tested: SELinux is off on our test machines.

5. Suddenly only "Connection closed"

# Real output from the test setup: after several failed logins in a row
Connection closed by 127.0.0.1 port 2222
# Real output from the server log (OpenSSH 10.0)
drop connection #0 from [172.17.0.1]:50060 on [172.17.0.2]:22 penalty: failed authentication

Since OpenSSH 9.8, sshd penalises an address after repeated failed logins (PerSourcePenalties, on by default) and drops new connections from it for a while, without the usual message. While you test keys, a burst of failures can lock you out briefly. Wait, or test from the open session. fail2ban and similar tools can block the address for longer, which looks the same from the client.

6. Verify

# Only the key you mean, so a key from the agent cannot hide the problem
ssh -o IdentitiesOnly=yes -i ~/.ssh/KEYFILE USER@HOST 'echo logged in as $(whoami)'
# Real output from the test setup after the fix, and the matching server log line
logged in as deploy
Accepted publickey for deploy from 172.17.0.1 port 34092 ssh2: ED25519 SHA256:yWDnim0N5Ngn/pbf0MW/FCO8WqnhdY0BIK8OGGdkKms

7. Rollback and prevention

  • Rollback: changing modes and owners back is safe only if you recorded them. Before a bulk chown or chmod, run stat -c '%a %U:%G %n' ~USER ~USER/.ssh ~USER/.ssh/* and keep the output.
  • Add keys with ssh-copy-id. It creates .ssh and authorized_keys with the right modes.
  • Keep one key per person and purpose, with a clear comment (ssh-keygen -C "name@laptop 2026"), so a fingerprint in the log tells you whose key it is.
  • Use IdentitiesOnly yes for hosts in ~/.ssh/config when your agent holds many keys. It avoids Too many authentication failures, which is covered with host key errors in Fix SSH host key and too many auth failures.

References

Official reference

man.openbsd.org ↗

Technical steps are original explanations; consult the upstream documentation for version-specific details.

Behavior differs by distribution and tool version. Commands are examples for authorized systems; review output for secrets before sharing.

Found this useful?

Follow us on YouTube for video explainers, and find us on Facebook to keep up with what is new.