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:
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:
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:
sudo tail -f /var/log/auth.log # Ubuntu/Debian
sudo journalctl -u ssh -f # systemd; the unit is "sshd" on RHEL-family systemsEach cause below has its own log line. Match yours and jump to the fix.
Fixes by cause
1. The wrong key is offered
Server log:
Failed publickey for deploy from 127.0.0.1 port 49748 ssh2: ED25519 SHA256:RpQcXxelkTzfsqEz7edIL/0e8486tHT8k0XCLMH9mIACompare fingerprints. The key the client offered must match a line in the server's authorized_keys:
$ 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:
$ ssh -i ~/.ssh/id_deploy -o IdentitiesOnly=yes deploy@erp-server whoami
deployMake 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):
Host erp-prod
HostName erp-server.example.com
User deploy
IdentityFile ~/.ssh/id_deploy
IdentitiesOnly yes2. 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:
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_keysEach 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:
$ 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/OF0The 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:
$ 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 52394For 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:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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:
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, 666Fix ownership and modes, then the same key works:
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_keysDo not turn StrictModes off to get around this. It exists so another user cannot add their key to your account.
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:
# 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 lockedCheck 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:
$ 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:
# 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 authenticationSo 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:
$ 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_keyswithsudo, leaving it owned by root. chmod 777on.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 noor 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




Comments
Join the discussion. Replies load on demand, up to 5 levels.
0 threads
No comments yet
Be the first to start the conversation.