Programming

Permission denied (publickey) when pushing to remote over SSH after adding a new key

Environment

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

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

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

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

Diagnostic steps

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

Confirmed fixes

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

Question

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

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
debugdaniel Answer
user
Posted 3w ago • 0 confirmations

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.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 3w ago• Trouble ID

Most likely causes

Local SSH agent not offering the new private key (ssh-agent not loaded or identity not added) 8%
Public key not installed in the correct git user's authorized_keys path or installed with wrong permissions/format 7%
SSH client using a different key filename or host alias; config mismatch (Host entry overrides IdentityFile) 5%

Summary

Most common causes are (1) key not offered by client (ssh-agent not presenting identity or wrong key filename), (2) public key not installed under the exact account or permissions incorrect on server, or (3) server refusing key due to SSH config or wrong key format. Less likely: server-side restrictions (Match, from=) or using different user/host than expected.

Suggested Fixes

Diagnostic step 1
Run ssh -vvv git@git.example.com and examine the offered identities lines (look for 'Offering public key' and which key file is referenced). Result meaning: 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.
Diagnostic step 2
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. Result meaning: 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.
Diagnostic step 3
Tail the SSH server auth logs (e.g., sudo tail -F /var/log/auth.log) while attempting an ssh connection from the client. Result meaning: 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.

Continue troubleshooting

Matched from the title, description and tags