Git Permission denied (publickey): The Definitive Fix Guide
Fix Git Permission denied (publickey) fast.
20+ years shipping production infrastructure and CI/CD at scale. Written from production experience, not tutorials.
- ✓Basic command-line familiarity (cd, ls, cat)
- ✓Git installed on your machine
- ✓An account on a Git hosting service (GitHub, GitLab, Bitbucket)
Run ssh -T git@github.com to see the exact error. Then check that your public key is added to your Git host account and that your SSH agent is running with the correct private key loaded. The fix is almost always one of: missing key on the server, wrong key loaded locally, or file permissions on your private key.
Imagine you're trying to enter a members-only warehouse. You have a physical key (your private key) and the warehouse has a lock that only opens for the matching key. The error means the warehouse bouncer looked at the key you handed over and said, 'This doesn't match any lock we have.' Either you gave them the wrong key, you never gave them a copy of your key to begin with, or the key you're holding is a cheap copy that doesn't turn properly.
You just cloned a repo, made a clean commit, and typed git push. Instead of your code landing on the remote, you get a wall of text ending in Permission denied (publickey). No hint about which key failed. No clue what to fix. Just a dead end. This error stops more developers cold than any other Git issue because it gives you almost nothing to work with. The good news: it has exactly four root causes, and you can diagnose all of them in under two minutes. By the end of this guide, you'll know how to read the SSH handshake, fix your key setup permanently, and never see this error again.
What SSH Actually Does When You Push Code
Before you can fix the error, you need to understand what's happening under the hood. Git doesn't have its own authentication system. When you use an SSH remote (like git@github.com:user/repo.git), Git hands the connection to your system's SSH client. SSH then performs a cryptographic handshake: it asks the server for its public key, verifies the server's identity, then offers your private key to prove who you are. The server checks if the corresponding public key is in its list of authorized keys. If not, it rejects the connection with Permission denied (publickey). The error comes from SSH, not Git. That's why Git gives you no useful context — it just relays the failure.
ssh -T git@github.com first. It tells you if SSH auth works at all, separate from Git. If it succeeds, your problem is the remote URL, not your keys.Cause #1: You Never Added Your Public Key to the Server
This is the most common cause for beginners. You generated an SSH key pair, but you never copied the public key to GitHub, GitLab, or Bitbucket. The server has no record of your key, so it rejects you. The fix is simple: copy the contents of your public key file (.pub extension) and paste it into your Git host's SSH settings. Never share your private key (the file without .pub). The public key is safe to share — it's like a padlock you give to the server; only your private key can open it.
ls ~/.ssh/id_* returns no filesssh-keygen -t ed25519 -C "email"ls ~/.ssh/id_* shows id_rsa and id_rsa.pubid_rsa.pub to your Git hostssh-add -lCause #2: The Wrong Key Is Being Offered
SSH doesn't try every key on your system. It offers keys in a specific order: first from the SSH agent, then from default filenames (id_rsa, id_ed25519, etc.). If you have multiple keys (e.g., one for work, one for personal), SSH might offer the wrong one first. The server rejects it and SSH gives up. You need to tell SSH which key to use for which host. The fix is an SSH config file that maps hosts to specific keys. This is also how you handle multiple GitHub accounts on one machine.
IdentitiesOnly yes is critical. Without it, SSH will still offer keys from the agent, which can cause 'too many authentication failures' on servers with strict limits.Cause #3: SSH Agent Lost Your Key After Reboot
The SSH agent is a background process that holds your decrypted private keys in memory so you don't have to type your passphrase every time. When you reboot, the agent restarts empty. If your key has a passphrase and you don't re-add it, SSH can't use it. The symptom is confusing: it worked before you rebooted, now it doesn't. The fix is to add your key to the agent after each reboot, or configure your shell to do it automatically.
Cause #4: File Permissions Are Too Open
OpenSSH is paranoid about key security. If your private key is readable by anyone other than you, SSH refuses to use it. This is a security feature: if your key is world-readable, anyone on the system could steal it. The error message is subtle — SSH doesn't say 'permissions are wrong', it just silently ignores the key and fails with Permission denied (publickey). You need to check permissions with ls -l and fix them with chmod. The private key must be 600 (read/write for owner only), and the .ssh directory should be 700.
chmod 777 ~/.ssh/id_ed25519 will make SSH refuse the key entirely. The error is Permissions 0777 for '/home/user/.ssh/id_ed25519' are too open.The Remote URL Mistake: HTTPS vs SSH
Sometimes the error isn't about keys at all. If your remote URL uses HTTPS (https://github.com/user/repo.git), Git uses username/password authentication, not SSH. But if you have a credential helper misconfigured or you're using an SSH URL in one place and HTTPS in another, you can get confusing errors. The Permission denied (publickey) error specifically means SSH was attempted. Check your remote URL with git remote -v. If it starts with https://, switch to SSH with git remote set-url.
Diagnosing with ssh -vvv: The Ultimate Debug Tool
When nothing else works, ssh -vvv is your best friend. It prints every step of the SSH handshake: which keys are offered, which are rejected, and why. Look for lines like Offering public key, Authentications that can continue, and No more authentication methods to try. If you see key ignored because of bad permissions, you know it's a permissions issue. If you see Offering public key but no Server accepts key, the server doesn't have your key. This output is the ground truth for SSH debugging.
ssh -vvv output is the first thing a senior engineer asks for when debugging SSH. Learn to read it and you'll solve 90% of these issues in seconds.Permanent Fix: Automate SSH Agent and Key Loading
The best fix is the one you never have to think about again. Configure your shell to start the SSH agent and load your key automatically on login. This eliminates the 'worked yesterday, broken today' problem after reboots. Add a few lines to your ~/.bashrc or ~/.zshrc that check if the agent is running and add your key if not. For macOS, you can also store the passphrase in the Keychain with ssh-add --apple-use-keychain.
The Deploy Key That Vanished at 3AM
Permission denied (publickey). Manual git push from developer laptops worked fine.~/.ssh permissions to 0777. OpenSSH refuses to use private keys with group or world permissions, silently ignoring the key and falling back to password auth (which GitHub disables).chmod 600 ~/.ssh/deploy_key and chmod 700 ~/.ssh to the CI setup script. Verify with ssh -vvv git@github.com that the key is actually offered.- SSH keys are only as good as their file permissions.
- If
ssh -vvvsays 'key ignored because of bad permissions', that's your answer.
git@github.com: Permission denied (publickey). on every Git operationssh -T git@github.com to confirm SSH auth fails. 2. Run ssh -vvv -T git@github.com 2>&1 | grep -E 'Offering|Authentications|Permission' to see which key is offered. 3. If no key is offered, check ls ~/.ssh/id_* and generate a key if missing. 4. If a key is offered but rejected, verify the public key is added to GitHub. 5. If permissions are wrong, run chmod 600 ~/.ssh/id_ed25519 && chmod 700 ~/.ssh.Permission denied (publickey)echo $SSH_AUTH_SOCK. 2. If empty, start agent: eval "$(ssh-agent -s)". 3. Add key: ssh-add ~/.ssh/id_ed25519. 4. Verify: ssh-add -l. 5. Add the auto-start snippet to ~/.bashrc or ~/.zshrc to prevent recurrence.Permission denied (publickey) but manual push workskey ignored because of bad permissions. 2. Add chmod 600 ~/.ssh/deploy_key and chmod 700 ~/.ssh to the CI setup script. 3. Verify the deploy key is added to the repository (not the user account). 4. Test with ssh -vvv -T git@github.com in the CI environment.ssh -T git@github.com to see which user authenticates. 2. Create ~/.ssh/config with Host aliases (e.g., github-work). 3. Set IdentityFile and IdentitiesOnly yes for each alias. 4. Update remote URL: git remote set-url origin git@github-work:company/repo.git.ssh -T git@github.comssh -vvv -T git@github.com 2>&1 | grep -E 'Offering|Authentications|Permission'ssh-keygen -t ed25519 -C "email" and add .pub to GitHub. If key rejected: verify public key is on GitHub. If permissions: chmod 600 ~/.ssh/id_ed25519.| File | Command / Code | Purpose |
|---|---|---|
| ssh_handshake_debug.sh | ssh -vvv -T git@github.com 2>&1 | grep -E 'debug1|Offering|Authentications|Permi... | What SSH Actually Does When You Push Code |
| add_public_key.sh | ssh-keygen -t ed25519 -C "your.email@example.com" | Cause #1 |
| ssh_config_multiple_keys.sh | Host github.com | Cause #2 |
| ssh_agent_setup.sh | eval "$(ssh-agent -s)" | Cause #3 |
| fix_ssh_permissions.sh | ls -l ~/.ssh/id_ed25519 | Cause #4 |
| fix_remote_url.sh | git remote -v | The Remote URL Mistake |
| ssh_debug_full.sh | ssh -vvv -T git@github.com 2>&1 | tee /tmp/ssh_debug.log | Diagnosing with ssh -vvv |
| auto_ssh_agent.sh | if [ -z "$SSH_AUTH_SOCK" ]; then | Permanent Fix |
Key takeaways
Permission denied (publickey) is an SSH error, not a Git error. Debug SSH first with ssh -T git@github.com.ssh -vvv is the ground truth. Look for 'Offering public key' and 'Server accepts key' to see exactly what's happening.chmod 600 on private keys, chmod 700 on ~/.ssh. SSH silently ignores keys with open permissions.~/.ssh/config with Host aliases and IdentitiesOnly yes to avoid 'too many authentication failures'.Interview Questions on This Topic
How does SSH key authentication actually work under the hood, and why does the server never see your private key?
Frequently Asked Questions
Keep going
20+ years shipping production infrastructure and CI/CD at scale. Written from production experience, not tutorials.
That's Git. Mark it forged?
3 min read · try the examples if you haven't
