Skip to content
|By Muhammad Wilayat Shah|29 views • 28 unique IPs

SSH Permission Denied (Publickey): How to Fix It

After a server rebuild our CI deploy failed with Permission denied (publickey). Use ssh -v and the server's auth log to find the cause: wrong key, wrong user, key permissions, bad ownership, AllowUsers or too many agent

ShareLinkedInX
Four steps to fix SSH Permission denied publickey: run ssh -v to see which key the client offered, read the server auth log to see why it was refused, fix the key, user, permissions or sshd config, then pin the key with a Host block and IdentitiesOnly
Four steps to fix SSH Permission denied publickey: run ssh -v to see which key the client offered, read the server auth log to see why it was refused, fix the key, user, permissions or sshd config, then pin the key with a Host block and IdentitiesOnly

Last reviewed: 23 Nov 2026

We rebuilt the production server for one of our ERP products, and the first deploy from CI failed with one line: deploy@erp-server: Permission denied (publickey). The same key had worked for months. A teammate had already generated a new key pair and pasted it in, and it still failed. When I opened the server's auth log, the reason was right there: Authentication refused: bad ownership or modes for file /home/deploy/.ssh/authorized_keys. He had created the file with sudo, so root owned it, and SSH refused to trust it.

That is the lesson of this error. The client message is the same for every cause. The server log tells you which cause it is, and ssh -v tells you what the client actually offered. I reproduced each cause below against OpenSSH 10.0p2 in a throwaway lab server on the box, with OpenSSH 10.0p2 as the client. The log lines and messages are real; I only replaced the lab's host address and temporary key paths with normal-looking ones (erp-server, ~/.ssh).

Quick diagnosis: two commands

On your machine (or in the CI job), run the connection with -v and keep only the useful lines:

Code
ssh -v -i ~/.ssh/id_deploy deploy@erp-server 2>&1 | grep -E "Offering|Will attempt|Authentications|denied|Load key"

When the wrong key is offered, it looks like this:

Code
debug1: Authentications that can continue: publickey
debug1: Will attempt key: /home/wilayat/.ssh/id_other ED25519 SHA256:RpQcXxelkTzfsqEz7edIL/0e8486tHT8k0XCLMH9mIA explicit
debug1: Offering public key: /home/wilayat/.ssh/id_other ED25519 SHA256:RpQcXxelkTzfsqEz7edIL/0e8486tHT8k0XCLMH9mIA explicit
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
deploy@erp-server: Permission denied (publickey).

If you can reach the server another way (cloud console, another admin account), read its log at the same moment:

Code
sudo tail -f /var/log/auth.log          # Ubuntu/Debian
sudo journalctl -u ssh -f               # systemd; the unit is "sshd" on RHEL-family systems

Each cause below has its own log line. Match yours and jump to the fix.

Animated checklist of six causes of SSH Permission denied publickey: the wrong key offered, the key missing from authorized_keys, the wrong user, a private key with permissions that are too open, server directories with bad ownership or modes, and sshd_config rules such as AllowUsers or a locked account
Six causes, each with the message that identifies it.

Fixes by cause

1. The wrong key is offered

Server log:

Code
Failed publickey for deploy from 127.0.0.1 port 49748 ssh2: ED25519 SHA256:RpQcXxelkTzfsqEz7edIL/0e8486tHT8k0XCLMH9mIA

Compare fingerprints. The key the client offered must match a line in the server's authorized_keys:

Code
$ ssh-keygen -lf ~/.ssh/id_other.pub
256 SHA256:RpQcXxelkTzfsqEz7edIL/0e8486tHT8k0XCLMH9mIA other@laptop (ED25519)
$ ssh-keygen -lf ~/.ssh/id_deploy.pub
256 SHA256:EK2BDjrcuo0P9slJ4anzrZHvOH7Va/Kk43WhjfB/OF0 deploy@laptop (ED25519)
# on the server:
$ ssh-keygen -lf /home/deploy/.ssh/authorized_keys
256 SHA256:EK2BDjrcuo0P9slJ4anzrZHvOH7Va/Kk43WhjfB/OF0 deploy@laptop (ED25519)

The server trusts id_deploy, and the client offered id_other. Point SSH at the right file and tell it to use only that file:

Code
$ ssh -i ~/.ssh/id_deploy -o IdentitiesOnly=yes deploy@erp-server whoami
deploy

Make it permanent in ~/.ssh/config so you and your scripts stop guessing (standard OpenSSH options; this exact block was written for the post, not run):

Code
Host erp-prod
    HostName erp-server.example.com
    User deploy
    IdentityFile ~/.ssh/id_deploy
    IdentitiesOnly yes

2. The public key is not on the server, or it was pasted broken

If no fingerprint in authorized_keys matches yours, add the .pub line. With password login disabled, ssh-copy-id cannot log in, so add it through an account that already has access:

Code
cat ~/.ssh/id_deploy.pub     # copy this single line
# on the server, as the deploy user (not with sudo):
mkdir -p ~/.ssh && chmod 700 ~/.ssh
echo 'ssh-ed25519 AAAA... deploy@laptop' >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Each key must be on one line. A key pasted from a chat app or a narrow terminal often gets split. In the lab, cat -A showed the broken line ($ marks each line end) and the server just reported a normal failure:

Code
$ cat -A /home/deploy/.ssh/authorized_keys
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOr4CO$
jsuCVKMgyKiuCYpCXAeNh8oLOtUMmmCaVbdvws deploy@laptop$
# server log:
Failed publickey for deploy from 127.0.0.1 port 52448 ssh2: ED25519 SHA256:EK2BDjrcuo0P9slJ4anzrZHvOH7Va/Kk43WhjfB/OF0

The fingerprint in that log line is the right key, which is the clue: the key is correct but the file does not contain it in a form SSH can read.

3. You are connecting as the wrong user

Keys belong to one account. Cloud images use different default users (ubuntu, ec2-user, admin, root), and your deploy user is different again. The server log is clear about it:

Code
$ ssh -i ~/.ssh/id_deploy ubuntu@erp-server
ubuntu@erp-server: Permission denied (publickey).
# server log:
Invalid user ubuntu from 127.0.0.1 port 52394

For Git hosts, the user is always git ([email protected]), never your own username.

4. Your private key file is too open

This one is visible on the client, before the server is even asked:

Code
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/home/wilayat/.ssh/id_deploy' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/home/wilayat/.ssh/id_deploy": bad permissions
deploy@erp-server: Permission denied (publickey).

Fix: chmod 600 ~/.ssh/id_deploy. In CI, this happens when a key from a secret is written with the default file mode; add the chmod right after writing it.

5. Server-side ownership or permissions are wrong

This was our rebuild problem. With the default StrictModes yes, sshd ignores authorized_keys if the file, the .ssh directory or the home directory can be written by anyone other than the user. The lab produced all three messages:

Code
Authentication refused: bad ownership or modes for directory /home/deploy/.ssh     # .ssh was 777
Authentication refused: bad ownership or modes for directory /home/deploy          # home was 775
Authentication refused: bad ownership or modes for file /home/deploy/.ssh/authorized_keys   # root-owned, 666

Fix ownership and modes, then the same key works:

Code
sudo chown -R deploy:deploy /home/deploy/.ssh
sudo chmod 755 /home/deploy          # or 750; never group- or world-writable
sudo chmod 700 /home/deploy/.ssh
sudo chmod 600 /home/deploy/.ssh/authorized_keys

$ ls -ld /home/deploy /home/deploy/.ssh /home/deploy/.ssh/authorized_keys
drwxr-xr-x deploy deploy /home/deploy
drwx------ deploy deploy /home/deploy/.ssh
-rw------- deploy deploy /home/deploy/.ssh/authorized_keys

Do not turn StrictModes off to get around this. It exists so another user cannot add their key to your account.

Animated terminal from the SSH lab: ssh -v shows the client offering the wrong ED25519 key and ending with Permission denied publickey, the server log shows Failed publickey, bad ownership or modes for the .ssh directory and not listed in AllowUsers, then ssh-keygen -lf confirms the right fingerprint and ssh with IdentitiesOnly logs in as deploy
Client output, then the server log lines that name the cause (OpenSSH 10.0p2 lab).

6. sshd_config or the account blocks the login

The key and permissions can be perfect and the login still fail because of server rules. Two I reproduced:

Code
# AllowUsers admin   in sshd_config
User deploy from 127.0.0.1 not allowed because not listed in AllowUsers

# account created without a password on Alpine (shadow field is "!")
User deploy not allowed because account is locked

Check the effective settings with sudo sshd -T | grep -Ei 'allowusers|allowgroups|pubkeyauthentication|authorizedkeysfile', fix the config, test it with sudo sshd -t, then reload with sudo systemctl reload ssh. For the locked account, sudo usermod -p '*' deploy unlocks key login without setting a password; that fixed it in the lab. On Ubuntu with the default PAM setup, a user with no password can usually still log in with a key, so this one mostly bites on Alpine and minimal images.

7. Too many keys in your agent, and OpenSSH 10 penalties

If your agent holds many keys, the client offers them one by one. The server allows 6 attempts by default (MaxAuthTries), so the right key may never be tried. With 7 keys loaded and the right one last:

Code
$ ssh-add -l | wc -l
7
$ ssh deploy@erp-server whoami
Received disconnect from 127.0.0.1 port 2222:2: Too many authentication failures
# server log:
maximum authentication attempts exceeded for deploy from 127.0.0.1 port 52460 ssh2 [preauth]

The fix is the same IdentitiesOnly yes with an explicit IdentityFile from cause 1.

Newer servers also punish repeated failures. OpenSSH 9.8 and later have PerSourcePenalties on by default. While I was testing, the lab server did this after a few failed logins from the same address:

Code
# client:
kex_exchange_identification: read: Connection reset by peer
Connection reset by 127.0.0.1 port 2222
# server log:
srclimit_penalise: 127.0.0.1/32: activating ipv4 penalty of 16 seconds for penalty: failed authentication
drop connection #0 from [127.0.0.1]:35340 on [127.0.0.1]:2222 penalty: failed authentication

So if you suddenly get "Connection reset" while debugging, stop retrying for a minute; you have not broken anything. This matters for CI runners behind one NAT address, which can trip the penalty for each other.

Git over SSH: GitHub, GitLab and your own server

Git prints the same SSH error, plus its own line. From the lab, cloning from a bare repo on the server with the wrong key:

Code
$ GIT_SSH_COMMAND='ssh -i ~/.ssh/id_other -o IdentitiesOnly=yes' git clone ssh://deploy@erp-server/home/deploy/erp.git
deploy@erp-server: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

For GitHub, test with ssh -T [email protected]. A working key answers Hi <username>! You've successfully authenticated, but GitHub does not provide shell access. (from GitHub's docs; my box cannot reach GitHub on port 22). A deploy key works for one repository only, so a CI job that needs two private repos needs two keys or a machine user. If the remote is HTTPS rather than SSH, you get a different error; my Git authentication failed guide covers that side. For deploys that fail only in CI, my guide to debugging CI pipelines has the logging habits I use.

Common mistakes

  • Generating a new key every time instead of reading the server log.
  • Creating authorized_keys with sudo, leaving it owned by root.
  • chmod 777 on .ssh "to make it work". It makes SSH refuse it.
  • Pasting the private key into authorized_keys, or the public key split over two lines.
  • Logging in as your laptop username instead of the server account that holds the key.
  • Setting StrictModes no or enabling password login on a public server to get past the error.

Questions I get asked

Where is the SSH server log?

/var/log/auth.log on Ubuntu and Debian, /var/log/secure on RHEL-family systems, or journalctl -u ssh / -u sshd on systemd. For more detail, set LogLevel VERBOSE in sshd_config and reload.

Should I use RSA or Ed25519 keys?

Ed25519 for new keys. Old RSA keys still work with current servers as long as the client signs with SHA-2 (rsa-sha2-256/512), which current clients do by default.

I am locked out completely. What now?

Use your provider's web or serial console, or a rescue boot, and fix permissions or authorized_keys from there. Keep one session open whenever you change sshd_config.


About the author. Muhammad Wilayat Shah is a senior full-stack engineer and team lead based in Islamabad, with 13+ years shipping ERP, HMS, fleet and property-portal products on Laravel, PHP, Node.js, React and PostgreSQL. He sets up the servers and CI deploy keys for his team's products, and "read the auth log first" is now step one of their access runbook. More at wilayats.com/about.

Last reviewed: 23 Nov 2026

ShareLinkedInX
Back to blog

Keep reading

Fix Nginx 413 Request Entity Too Large Error

11/25/2026

Fix Nginx 413 Request Entity Too Large Error

Nginx 413 Request Entity Too Large means a body size limit was hit. Find which layer sent it, then fix client_max_body_size, PHP upload limits, Express and Kubernetes ingress.

Newsletter

Occasional notes on Laravel, React, and shipping production systems — no spam.

Comments

Join the discussion. Replies load on demand, up to 5 levels.

0 threads

Loading editor…
Reviewed before publishing

No comments yet

Be the first to start the conversation.