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.

Introduction & Quick Summary
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-agentis 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/configblock forgithub.compinsIdentityFileorIdentitiesOnly 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.
| Message | Meaning | First check |
|---|---|---|
Permission denied (publickey) | No offered key accepted | ssh -vT git@github.com |
Permission denied (publickey,password) | Server also allows passwords | Same, key still missing |
Too many authentication failures | Six bad offers exhausted | IdentitiesOnly yes |
Host key verification failed | Different problem entirely | ssh-keyscan and known_hosts |
Repository not found after auth succeeds | Authorization, not authentication | Repo 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:
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:
cat >> ~/.ssh/config <<'CONF'
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_ci
IdentitiesOnly yes
CONF
ssh -T git@github.comIdentitiesOnly 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:
GIT_SSH_COMMAND='ssh -i /run/secrets/deploy_key -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new' \
git clone git@github.com:org/repo.gitKey Comparison Table
| Key location | Offered by default | Survives reboot | Notes |
|---|---|---|---|
~/.ssh/id_ed25519 | Yes | Yes | Preferred default |
~/.ssh/id_rsa | Yes | Yes | RSA still works, weaker |
Custom name in ~/.ssh | No | Yes | Needs IdentityFile |
Loaded in ssh-agent | Yes, all of them | Until reboot unless persisted | Can exhaust attempts |
GIT_SSH_COMMAND -i | Yes, for that command | No | Best for CI |
| Deploy key on the repo | Yes | Server side | Read-only unless allowed |
| Host setup | Auth works | Typical failure | Fix |
|---|---|---|---|
| Single personal key | Yes | None | n/a |
| Multiple accounts | Only with config | Wrong account key offered | Host alias per account |
| Organization with SSO | Only after authorization | Repository not found | Authorize the key for the org |
| Deploy key | Yes, one repo | Key reused on another repo | One key per repo |
| CI runner | Only with the secret mounted | Empty agent | GIT_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:
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.gitCase 2: The agent forgot the key after a reboot
eval "$(ssh-agent -s)"
ssh-add --apple-use-keychain ~/.ssh/id_ed25519 # macOS
ssh-add ~/.ssh/id_ed25519 # Linux
ssh-add -lOn 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_hostsA 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:
ssh -o PubkeyAcceptedAlgorithms=+ssh-rsa -T git@github.comPrefer generating an ed25519 key over re-enabling ssh-rsa, which is deprecated for signing.
Common Compilation/Runtime Mistakes & Version Gotchas
ssh -Tsucceeds butgitfails. Aurl.<base>.insteadOfrule,core.sshCommand, or aGIT_SSHvariable is overriding the client. Check withgit config --show-origin --get-all core.sshCommandandgit config --get-regexp '^url\.'.~/.ssh/configis ignored. Older clients require the file to be owned by you and not writable by others, andIncludedirectives only work from OpenSSH 7.3 onward.- Agent forwarding leaks keys.
ForwardAgent yesmakes every host you hop through able to use your key. PreferProxyJump, which carries the connection without exposing identities. IdentitiesOnlywithout a matchingIdentityFile. The client then offers nothing at all, which looks identical to a missing key.- Container images that reset
$HOME. AHOMEof/rootwhile keys were mounted at/home/app/.sshmeans the client reads an empty directory. SetHOMEexplicitly or copy the keys. - Case and line endings. A
.pubkey 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 documentationThe Git documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Git guides
All Git guidesGit: fatal: not a git repository (or any parent): .git
The command ran outside a working tree or the .git metadata is gone. Confirm the root, restore a deleted .git, and repair worktree pointers.
Git: fatal: could not read from remote repository
Git could not authenticate or reach the remote. Check the remote URL, test SSH with -T, verify the key is offered, and use HTTPS on 443 when 22 is blocked.
Git: fatal: Need to specify how to reconcile divergent branches
Git 2.34 and later refuse to guess between merge, rebase and fast-forward. Set pull.rebase or pull.ff once, and know what each strategy does to history.
Git: fatal: unable to auto-detect email address
A commit needs an author identity. Containers and fresh installs have no user.email, so Git guesses from the OS user and refuses to continue.
More in Git
All Git guidesThe guides filed next to this one in Git, in library order.