Programming

Git push fails with 'Permission denied (publickey)' after adding SSH key to remote account

Environment

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

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

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

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

Diagnostic steps

  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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

Confirmed fixes

No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.

Question

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)?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 3w ago• Trouble ID

Most likely causes

Local SSH client is not offering the key (ssh-agent not loaded or ssh_config forcing IdentityFile not used). Marked as hypothesis. 7%
Remote hosting service did not register the exact public key or key was malformed when uploaded. Marked as hypothesis. 6%
Server-side access restriction (IP allowlist, user blocked, or forced command) preventing key-based login. Marked as hypothesis. 4%

Summary

Permission denied (publickey) means the server and client did not successfully authenticate with any key offered. Typical causes: wrong key used (multiple keys and ssh_config selects different one), agent not offering key, key not added correctly to the remote account, server-side account restrictions (e.g., forced command, deny/allowlist), or file-permission/SELinux preventing ssh from reading key. Focus on reproducing the SSH handshake with verbose logging to see which keys are offered and which are rejected.

Suggested Fixes

Diagnostic step 1
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. Result meaning: 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.
Diagnostic step 2
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. Result meaning: 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.
Diagnostic step 3
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. Result meaning: 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.

Continue troubleshooting

Matched from the title, description and tags