Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Git: Permission denied (publickey) on clone and push

SSH refused every key you offered. Pin the right identity, check file modes and the agent, and confirm the key is authorized for the account.

~5 min readGit
#Git#SSH#Authentication#GitHub
A Git remote gate refusing an amber key crate while a cyan identity card waits outside

Introduction & Quick Summary

text
git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.

The SSH handshake completed and the server rejected every key you offered. That is why the message does not mention repositories or access rights in a useful way: authentication failed before authorization was ever considered.

The causes, ordered by how often they are the real one:

  • The key is not offered. ssh-agent is empty, or the key file is not the default name, so the client never sends it.
  • The key on disk is not the key on the account. A regenerated key, a different machine, or a key added to a personal account while the remote belongs to an organization that requires SSO.
  • File permissions. OpenSSH refuses a private key readable by group or others, and on some systems it silently falls back to another identity.
  • Host-specific config. A ~/.ssh/config block for github.com pins IdentityFile or IdentitiesOnly yes, and that file is the wrong one.
  • Too many keys. Servers allow a limited number of authentication attempts (six for GitHub over SSH), so the correct key can be skipped when five wrong ones are tried first.
MessageMeaningFirst check
Permission denied (publickey)No offered key acceptedssh -vT git@github.com
Permission denied (publickey,password)Server also allows passwordsSame, key still missing
Too many authentication failuresSix bad offers exhaustedIdentitiesOnly yes
Host key verification failedDifferent problem entirelyssh-keyscan and known_hosts
Repository not found after auth succeedsAuthorization, not authenticationRepo name, org access

Core Mechanics, Signatures & Reproducible Setup

OpenSSH offers identities in this order: explicit -i flags, IdentityFile entries from the effective config, then the default files (~/.ssh/id_rsa, id_ecdsa, id_ed25519, plus any agent keys). The server accepts or rejects each until it runs out of attempts. GitHub accepts the first key that matches a registered key, whether it is an account key or a deploy key.

Diagnose with the client's own trace:

bash
ssh -vT git@github.com 2>&1 | grep -E "Offering|Authentications that can continue|Server accepts|identity file"

A healthy trace ends with Hi <user>! You've successfully authenticated, but GitHub does not provide shell access. A failing one shows keys being offered and each reply containing publickey.

Reproduce the minimal case with a fresh key pair:

ssh-keygen -t ed25519 -C "ci@example.com" -f ~/.ssh/id_ed25519_ci -N "" chmod 600 ~/.ssh/id_ed25519_ci ssh -i ~/.ssh/id_ed25519_ci -T git@github.com # git@github.com: Permission denied (publickey).

Then pin the identity so nothing else is tried:

bash
cat >> ~/.ssh/config <<'CONF' Host github.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519_ci IdentitiesOnly yes CONF ssh -T git@github.com

IdentitiesOnly yes is the single most useful directive in multi-key setups, because it stops the agent from spraying every loaded key at the server.

For automation, use a per-command key without touching global config:

bash
GIT_SSH_COMMAND='ssh -i /run/secrets/deploy_key -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new' \ git clone git@github.com:org/repo.git

Key Comparison Table

Key locationOffered by defaultSurvives rebootNotes
~/.ssh/id_ed25519YesYesPreferred default
~/.ssh/id_rsaYesYesRSA still works, weaker
Custom name in ~/.sshNoYesNeeds IdentityFile
Loaded in ssh-agentYes, all of themUntil reboot unless persistedCan exhaust attempts
GIT_SSH_COMMAND -iYes, for that commandNoBest for CI
Deploy key on the repoYesServer sideRead-only unless allowed
Host setupAuth worksTypical failureFix
Single personal keyYesNonen/a
Multiple accountsOnly with configWrong account key offeredHost alias per account
Organization with SSOOnly after authorizationRepository not foundAuthorize the key for the org
Deploy keyYes, one repoKey reused on another repoOne key per repo
CI runnerOnly with the secret mountedEmpty agentGIT_SSH_COMMAND with -i

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

Case 1: Two GitHub accounts on one machine

The default host entry cannot serve both, so give each account its own alias and clone through it:

bash
cat >> ~/.ssh/config <<'CONF' Host github-work HostName github.com User git IdentityFile ~/.ssh/id_ed25519_work IdentitiesOnly yes CONF git remote set-url origin git@github-work:org/repo.git

Case 2: The agent forgot the key after a reboot

bash
eval "$(ssh-agent -s)" ssh-add --apple-use-keychain ~/.ssh/id_ed25519 # macOS ssh-add ~/.ssh/id_ed25519 # Linux ssh-add -l

On macOS, add the key to the login keychain with ssh-add --apple-use-keychain and set AddKeysToAgent yes plus UseKeychain yes in the host block. On Linux desktops, a systemd user unit for ssh-agent persists it across sessions.

Case 3: Permissions, including the parent directory

chmod 700 ~/.ssh chmod 600 ~/.ssh/id_ed25519 chmod 644 ~/.ssh/id_ed25519.pub ~/.ssh/config ~/.ssh/known_hosts

A group-writable home directory causes Bad owner or permissions on /home/user/.ssh/config on newer OpenSSH, which stops the client before it offers anything.

Access keys from Windows through WSL

Keys generated on Windows live under /mnt/c/Users/<name>/.ssh with mode 0777, which OpenSSH rejects. Copy them into the Linux home instead of pointing at the mount, or set chmod 600 after copying, knowing that the mount may reset modes.

Case 4: Deploy keys and the "already in use" trap

A deploy key can be attached to exactly one repository. Reusing the same public key on a second repo fails at Settings → Deploy keys with Key is already in use. Generate one key per repository and pin it per host alias.

Case 5: Organization SSO

A key that authenticates fine against a personal repository can still fail on an organization repository when the key is not authorized for that organization's SAML SSO. GitHub reports Repository not found to avoid leaking existence, so check the key's SSO status in settings before rewriting remotes.

Case 6: RSA keys and the SHA-1 signature algorithm

Older servers and clients defaulted to ssh-rsa, which many hosts disabled. If a very old key is the only one available:

bash
ssh -o PubkeyAcceptedAlgorithms=+ssh-rsa -T git@github.com

Prefer generating an ed25519 key over re-enabling ssh-rsa, which is deprecated for signing.

Common Compilation/Runtime Mistakes & Version Gotchas

  • ssh -T succeeds but git fails. A url.<base>.insteadOf rule, core.sshCommand, or a GIT_SSH variable is overriding the client. Check with git config --show-origin --get-all core.sshCommand and git config --get-regexp '^url\.'.
  • ~/.ssh/config is ignored. Older clients require the file to be owned by you and not writable by others, and Include directives only work from OpenSSH 7.3 onward.
  • Agent forwarding leaks keys. ForwardAgent yes makes every host you hop through able to use your key. Prefer ProxyJump, which carries the connection without exposing identities.
  • IdentitiesOnly without a matching IdentityFile. The client then offers nothing at all, which looks identical to a missing key.
  • Container images that reset $HOME. A HOME of /root while keys were mounted at /home/app/.ssh means the client reads an empty directory. Set HOME explicitly or copy the keys.
  • Case and line endings. A .pub key pasted with CRLF into a web form is stored with an invisible character and never matches.

When several remotes and configs are involved, line up the working and failing ~/.ssh/config blocks with the diff tool. To confirm the remote URL is really the one you think, dump git remote -v and inspect the structured output with the JSON formatter if a script generated it. For checking that outbound SSH is reachable before blaming keys, the port and CIDR reference helps verify the network path for host and bastion ranges.

Primary references: GitHub's SSH key documentation covers key generation and agent setup, and ssh_config(5) documents IdentityFile, IdentitiesOnly and Host matching.

Official references

primary documentation

The Git documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.