Permission denied (publickey) when pushing to remote over SSH after adding a new key
Environment
Ubuntu 22.04 laptop, OpenSSH 8.9p1, Git 2.38, remote is a self-hosted Git server over SSH (git@git.example.com). New key pair created with ssh-keygen -t ed25519.
Symptoms
git push returns 'Permission denied (publickey)'. ssh -v git@git.example.com shows no keyboard-interactive prompt and immediately fails. Other SSH accounts still work from same machine.
Observations
Public key was appended to ~/.ssh/authorized_keys on server under git user's .ssh. Local ~/.ssh/id_ed25519 has 600 perms. ssh-agent shows no identities unless manually added. Known_hosts entry present for host.
Already attempted
Copied public key to server via scp and appended to authorized_keys. Ran ssh-add ~/.ssh/id_ed25519 then attempted push. Restarted ssh-agent and reloaded shell. Tried git push with GIT_SSH_COMMAND='ssh -vvv' to increase verbosity.
Diagnostic steps
- Run ssh -vvv git@git.example.com and examine the offered identities lines (look for 'Offering public key' and which key file is referenced).
- What the result means: If the client does not show 'Offering public key' for your new key, it isn't being offered — add it with ssh-add or configure IdentityFile in ~/.ssh/config. If it is offered but server rejects, the server either lacks that public key or rejects it.
- On the server, check ~git/.ssh/authorized_keys contains the exact public key text (no extra whitespace) and ensure ~git/.ssh is 700 and authorized_keys 600; also check ownership is git:git.
- What the result means: If authorized_keys missing the key or permissions/ownership incorrect, SSH will ignore it. Fixing ownership/permissions and re-testing should allow authentication. If key is present and perms OK, move to server-side logs.
- Tail the SSH server auth logs (e.g., sudo tail -F /var/log/auth.log) while attempting an ssh connection from the client.
- What the result means: If the server log shows 'authentication refused' and indicates which key or why, that narrows the issue: 'key type mismatch', 'user not found', or 'authorized_keys invalid'. If logs show no attempt for that user/host, client may be connecting to different host or user.
- Check ~/.ssh/config for Host entries that may override IdentityFile or User; explicitly run ssh -i ~/.ssh/id_ed25519 -vvv git@git.example.com to force the key.
- What the result means: If forcing -i makes it work, then your SSH config prevented the key from being used before — update config. If forcing -i still fails, problem likely server-side or key pair mismatch.
Confirmed fixes
No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.
Question
Why does git push still fail with 'Permission denied (publickey)' and how to verify which step is broken (local key usage, agent, or remote key registration)?
Verified community troubleshooting
First verify the client actually offers the key: run ssh -vvv git@git.example.com and look for 'Offering public key' lines and the filename. If your key is not offered, add it to agent with ssh-add or set IdentityFile in ~/.ssh/config. If it is offered but server rejects it, check the server's ~git/.ssh/authorized_keys for the exact public-key line and file permissions (dir 700, file 600, owner git). Tail server auth logs during a failed attempt to see rejection reason. For temporary testing force the key with ssh -i ~/.ssh/id_ed25519 -vvv git@git.example.com; if that succeeds, fix client config. If logs show server-side restrictions (from= options, forced command), involve the server admin to adjust authorized_keys safely.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags