Home › DevOps › Git Permission denied (publickey): The Definitive Fix Guide
Beginner 3 min · December 06, 2026

Git Permission denied (publickey): The Definitive Fix Guide

Fix Git Permission denied (publickey) fast.

N
Naren Founder & Principal Engineer

20+ years shipping production infrastructure and CI/CD at scale. Written from production experience, not tutorials.

Follow
✓ Production
production tested
December 06, 2026
last updated
2,151
articles · all by Naren
Before you start⏱ 20 min
  • ✓Basic command-line familiarity (cd, ls, cat)
  • ✓Git installed on your machine
  • ✓An account on a Git hosting service (GitHub, GitLab, Bitbucket)
 ● Production Incident 🔎 Debug Guide ⚙ Triage Commands
⚡Quick Answer

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.

✦ Definition~90s read
What is Git Permission denied (publickey) Fix?

Git Permission denied (publickey) is an SSH authentication failure that occurs when your local machine cannot prove its identity to a remote Git server using the SSH key pair it presented. The server rejects the connection before any Git operation (clone, push, pull) can happen.

★
Imagine you're trying to enter a members-only warehouse.
Plain-English First

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_handshake_debug.shBASH
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env bash
# io.thecodeforge — DevOps tutorial
# Verbose SSH connection to GitHub to see the exact handshake steps

# -v: verbose, -vv: more verbose, -vvv: maximum debug output
# -T: disable pseudo-terminal allocation (GitHub doesn't need a shell)
ssh -vvv -T git@github.com 2>&1 | grep -E 'debug1|Offering|Authentications|Permission'

# Expected output when key is missing:
# debug1: Offering public key: /home/user/.ssh/id_rsa RSA SHA256:...
# debug1: Authentications that can continue: publickey
# debug1: No more authentication methods to try.
# git@github.com: Permission denied (publickey).
Output
debug1: Offering public key: /home/user/.ssh/id_rsa RSA SHA256:abc123...
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey).
💡Senior Shortcut:
Always run 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.

add_public_key.shBASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env bash
# io.thecodeforge — DevOps tutorial
# Step 1: Generate an SSH key pair if you don't have one
# -t ed25519: modern, secure algorithm (preferred over RSA)
# -C: comment to identify the key (use your email)
ssh-keygen -t ed25519 -C "your.email@example.com"

# Step 2: Copy the public key to your clipboard
# On macOS:
pbcopy < ~/.ssh/id_ed25519.pub
# On Linux (requires xclip):
xclip -sel clip < ~/.ssh/id_ed25519.pub
# On Windows (Git Bash):
cat ~/.ssh/id_ed25519.pub | clip

# Step 3: Paste into GitHub → Settings → SSH and GPG keys → New SSH key
# Step 4: Test the connection
ssh -T git@github.com

# Expected output:
# Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Output
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Do You Have a Key?
Ifls ~/.ssh/id_* returns no files
→
UseGenerate a new key with ssh-keygen -t ed25519 -C "email"
Ifls ~/.ssh/id_* shows id_rsa and id_rsa.pub
→
UseCopy id_rsa.pub to your Git host
IfYou have a key but still get denied
→
UseCheck if the key is loaded in the SSH agent: ssh-add -l

Cause #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.

ssh_config_multiple_keys.shBASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env bash
# io.thecodeforge — DevOps tutorial
# ~/.ssh/config — map hosts to specific keys

# Personal GitHub account
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal
  IdentitiesOnly yes  # Only use the specified key, ignore agent

# Work GitHub account (using a host alias)
Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work
  IdentitiesOnly yes

# Then clone work repos with:
# git clone git@github-work:company/repo.git
# And personal repos with:
# git clone git@github.com:username/repo.git
Output
Cloning into 'repo'...
remote: Enumerating objects: 100, done.
remote: Counting objects: 100% (100/100), done.
remote: Total 100 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (100/100), done.
⚠ Production Trap:
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.

ssh_agent_setup.shBASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env bash
# io.thecodeforge — DevOps tutorial
# Start the SSH agent and add your key

# Start agent (prints environment variables)
eval "$(ssh-agent -s)"

# Add your private key (will prompt for passphrase if set)
ssh-add ~/.ssh/id_ed25519

# List loaded keys to verify
ssh-add -l

# Expected output:
# 256 SHA256:abc123... your.email@example.com (ED25519)

# To make this automatic, add to ~/.bashrc or ~/.zshrc:
# if [ -z "$SSH_AUTH_SOCK" ]; then
#   eval "$(ssh-agent -s)"
#   ssh-add ~/.ssh/id_ed25519
# fi
Output
Agent pid 12345
Identity added: /home/user/.ssh/id_ed25519 (your.email@example.com)
256 SHA256:abc123... your.email@example.com (ED25519)

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.

fix_ssh_permissions.shBASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env bash
# io.thecodeforge — DevOps tutorial
# Fix SSH key permissions

# Check current permissions
ls -l ~/.ssh/id_ed25519
# Bad output: -rw-r--r-- 1 user user 411 Jan 1 00:00 id_ed25519
# Good output: -rw------- 1 user user 411 Jan 1 00:00 id_ed25519

# Fix private key permissions (owner read/write only)
chmod 600 ~/.ssh/id_ed25519

# Fix .ssh directory permissions (owner read/write/execute only)
chmod 700 ~/.ssh

# Fix public key permissions (owner read/write, others read)
chmod 644 ~/.ssh/id_ed25519.pub

# Verify
ls -l ~/.ssh/id_ed25519
# Should show: -rw------- 1 user user 411 Jan 1 00:00 id_ed25519
Output
-rw------- 1 user user 411 Jan 1 00:00 /home/user/.ssh/id_ed25519
⚠ Never Do This:
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.

fix_remote_url.shBASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env bash
# io.thecodeforge — DevOps tutorial
# Check and fix remote URL

# Check current remote
git remote -v
# Bad: origin https://github.com/user/repo.git (fetch)
# Good: origin git@github.com:user/repo.git (fetch)

# Switch to SSH
git remote set-url origin git@github.com:user/repo.git

# Verify
ssh -T git@github.com
# Should show: Hi username! You've successfully authenticated...
Output
origin git@github.com:user/repo.git (fetch)
origin git@github.com:user/repo.git (push)
Hi username! You've successfully authenticated, but GitHub does not provide shell access.

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_debug_full.shBASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env bash
# io.thecodeforge — DevOps tutorial
# Full SSH debug output

ssh -vvv -T git@github.com 2>&1 | tee /tmp/ssh_debug.log

# Key lines to look for:
# 1. "Offering public key: /home/user/.ssh/id_ed25519" — key is being offered
# 2. "Server accepts key: ..." — server accepted it (success)
# 3. "Authentications that can continue: publickey" — server wants publickey auth
# 4. "No more authentication methods to try" — all keys rejected
# 5. "key ignored because of bad permissions" — chmod issue

# If you see "Offering" but no "Server accepts", the key isn't on the server.
# If you see "key ignored", fix permissions with chmod 600.
Output
debug1: Offering public key: /home/user/.ssh/id_ed25519 ED25519 SHA256:abc123...
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey).
💡Interview Gold:
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.

auto_ssh_agent.shBASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env bash
# io.thecodeforge — DevOps tutorial
# Add to ~/.bashrc or ~/.zshrc

# Start SSH agent if not running
if [ -z "$SSH_AUTH_SOCK" ]; then
  eval "$(ssh-agent -s)" > /dev/null
fi

# Add key if not already loaded
if ! ssh-add -l | grep -q "id_ed25519"; then
  ssh-add ~/.ssh/id_ed25519 2>/dev/null
fi

# macOS: store passphrase in Keychain
# ssh-add --apple-use-keychain ~/.ssh/id_ed25519
Output
Agent pid 12345
Identity added: /home/user/.ssh/id_ed25519 (your.email@example.com)
● Production incidentPOST-MORTEMseverity: high

The Deploy Key That Vanished at 3AM

Symptom
CI/CD pipeline failed on every deploy with Permission denied (publickey). Manual git push from developer laptops worked fine.
Assumption
The team assumed GitHub had an outage or the deploy key was revoked by a security scan.
Root cause
The CI runner container was rebuilt from a base image that reset ~/.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).
Fix
Add 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.
Key lesson
  • SSH keys are only as good as their file permissions.
  • If ssh -vvv says 'key ignored because of bad permissions', that's your answer.
Production debug guideSystematic recovery paths for the failure modes engineers actually hit.4 entries
Symptom · 01
git@github.com: Permission denied (publickey). on every Git operation
→
Fix
1. Run ssh -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.
Symptom · 02
Worked before reboot, now fails with Permission denied (publickey)
→
Fix
1. Check if SSH agent is running: 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.
Symptom · 03
CI/CD pipeline fails with Permission denied (publickey) but manual push works
→
Fix
1. Check CI logs for key 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.
Symptom · 04
Multiple GitHub accounts, wrong one authenticates
→
Fix
1. Run 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.
★ Git Permission denied (publickey) Fix Triage Cheat SheetFirst-response commands for when things go wrong — copy-paste ready.
`git@github.com: Permission denied (publickey).`−
Immediate action
Check if SSH auth works at all, separate from Git.
Commands
ssh -T git@github.com
ssh -vvv -T git@github.com 2>&1 | grep -E 'Offering|Authentications|Permission'
Fix now
If no key offered: 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.
`Permission denied (publickey)` after reboot+
Immediate action
Check if SSH agent is running and key is loaded.
Commands
ssh-add -l
eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_ed25519
Fix now
Add auto-start snippet to ~/.bashrc or ~/.zshrc.
`Permission denied (publickey)` but `ssh -T` works+
Immediate action
Check remote URL — it's probably HTTPS.
Commands
git remote -v
git remote set-url origin git@github.com:user/repo.git
Fix now
Switch to SSH URL and test with git push.
`key ignored because of bad permissions` in `ssh -vvv`+
Immediate action
Fix file permissions on private key.
Commands
ls -l ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519 && chmod 700 ~/.ssh
Fix now
Verify with ssh -T git@github.com.
Feature / AspectSSH URLHTTPS URL
Authentication methodSSH key pairUsername + password or token
Port22 (or custom)443
Firewall friendlinessOften blockedUsually allowed
Setup complexityGenerate key, add to serverJust enter credentials
Best forDevelopers with multiple reposCI/CD, quick clones
⚙ Quick Reference
8 commands from this guide
FileCommand / CodePurpose
ssh_handshake_debug.shssh -vvv -T git@github.com 2>&1 | grep -E 'debug1|Offering|Authentications|Permi...What SSH Actually Does When You Push Code
add_public_key.shssh-keygen -t ed25519 -C "your.email@example.com"Cause #1
ssh_config_multiple_keys.shHost github.comCause #2
ssh_agent_setup.sheval "$(ssh-agent -s)"Cause #3
fix_ssh_permissions.shls -l ~/.ssh/id_ed25519Cause #4
fix_remote_url.shgit remote -vThe Remote URL Mistake
ssh_debug_full.shssh -vvv -T git@github.com 2>&1 | tee /tmp/ssh_debug.logDiagnosing with ssh -vvv
auto_ssh_agent.shif [ -z "$SSH_AUTH_SOCK" ]; thenPermanent Fix

Key takeaways

1
Permission denied (publickey) is an SSH error, not a Git error. Debug SSH first with ssh -T git@github.com.
2
The four causes are
missing public key on server, wrong key offered, SSH agent lost the key, or bad file permissions. Check them in that order.
3
ssh -vvv is the ground truth. Look for 'Offering public key' and 'Server accepts key' to see exactly what's happening.
4
File permissions matter
chmod 600 on private keys, chmod 700 on ~/.ssh. SSH silently ignores keys with open permissions.
5
For multiple accounts, use ~/.ssh/config with Host aliases and IdentitiesOnly yes to avoid 'too many authentication failures'.
INTERVIEW PREP · PRACTICE MODE

Interview Questions on This Topic

Q01SENIOR
How does SSH key authentication actually work under the hood, and why do...
Q02SENIOR
When would you choose SSH over HTTPS for Git remotes in a production CI/...
Q03SENIOR
What happens when you have multiple SSH keys and the server has a MaxAut...
Q04JUNIOR
What's the difference between a deploy key and a user SSH key?
Q05SENIOR
A developer's push fails with `Permission denied (publickey)` but `ssh -...
Q06SENIOR
How would you manage SSH keys across a fleet of 100 CI runners without c...
Q01 of 06SENIOR

How does SSH key authentication actually work under the hood, and why does the server never see your private key?

ANSWER
SSH uses public-key cryptography. The server stores your public key. During authentication, the server sends a challenge (random data). Your SSH client signs the challenge with your private key and sends the signature. The server verifies the signature using the public key. The private key never leaves your machine. This is why you can safely share your public key.
FAQ · 4 QUESTIONS

Frequently Asked Questions

01
Why do I get 'Permission denied (publickey)' when I try to push to GitHub?
02
What's the difference between HTTPS and SSH for Git remotes?
03
How do I fix 'Permission denied (publickey)' after rebooting my machine?
04
Why does SSH ignore my key even though it's in the right place?
RESOURCES

Keep going

AutoSys Full Course for Beginners▶
WatchAutoSys Full Course for BeginnersJIL, job statuses, monitoring and failed jobs, start to finish.Watch on YouTube →
ReadThe complete DevOps guideOne deep guide that walks the whole DevOps track in order.Start reading →
Q
PracticeDevOps Interview QuestionsReal interview questions with answers, by topic.Practice now →
N
Naren Founder & Principal Engineer

20+ years shipping production infrastructure and CI/CD at scale. Written from production experience, not tutorials.

Follow
✓ Verified
production tested
December 06, 2026
last updated
2,151
articles · all by Naren
🔥

That's Git. Mark it forged?

3 min read · try the examples if you haven't

←
Previous
AutoSys Windows Job Fails After Password Change
54 / 54 · Git
Next
Docker bind: address already in use
→