Git push fails with 'Permission denied (publickey)' after adding SSH key to remote account
Environment
Developer workstation running OpenSSH client, using git+ssh to push to a hosted Git service. Keypair generated with default locations (~/.ssh/id_rsa or id_ed25519), added to remote account UI. Agent may or may not be running.
Symptoms
git push aborts with 'Permission denied (publickey)'. ssh -v git@host shows authentication attempts but ultimately fails. Pushing over HTTPS with username/password or PAT succeeds.
Observations
The public key listed in the remote account matches the local public key file contents. On the failing machine, ssh -i <key> git@host -v still shows publickey rejected. ssh-agent forwarding not in use. No obvious typos in remote URL.
Already attempted
Re-uploaded public key to remote account, confirmed key fingerprint matches, restarted ssh-agent, tried ssh -T git@host with -i explicitly, and verified file permissions (600 on private key). Not yet tested SELinux/AppArmor or SSH client config overrides.
Diagnostic steps
- Run ssh -vvv -i ~/.ssh/id_ed25519 git@host (use the explicit private key path) and capture the debug trace of key offers and server responses.
- What the result means: If the client does not attempt the key, check ssh_config for IdentityFile or IdentitiesOnly settings. If the client offers the key but server rejects, the public key may be missing or mismatched on the server side or the account blocked.
- Temporarily add the key to the agent with ssh-add -l to list keys and ssh-add ~/.ssh/id_ed25519 to ensure the key is offered.
- What the result means: If adding the key to the agent makes authentication succeed, agent/agent forwarding was the issue. If it still fails, the server likely rejects the key.
- Compare the exact public key string on the host (cat ~/.ssh/id_ed25519.pub) with the entry in the hosting service UI/API and regenerate/upload if any whitespace or newline issues exist.
- What the result means: If the keys differ (even by a single character), re-uploading the correct public key should allow authentication. If identical, the issue is not a bad upload.
- Check server-side account restrictions in hosting UI (deploy keys vs user keys, repository write access, IP restrictions) and confirm the key has write access to the target repo.
- What the result means: If the key is added as a deploy key with read-only access or to a different account, pushing will fail even if SSH auth succeeds. Adjust key placement or repository permissions.
- On machines with SELinux/AppArmor, check audit logs or temporarily set permissive mode to rule out local denial of private key access; verify key file permissions are 600 and directory 700.
- What the result means: If permissive mode or corrected perms allow ssh to read the key and authentication succeeds, adjust local security policy or permissions accordingly.
Confirmed fixes
No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.
Question
Which checks will reliably identify whether this is a local key/agent issue, an SSH config mismatch, or a remote account restriction (IP or user denied)?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags