GitHub Permission Denied (publickey) Error
Quick answer
GitHub rejects the SSH connection because it cannot match a private key on your machine to a public key on your account. Load the right key into ssh-agent, make sure its public half is under Settings > SSH and GPG keys, and connect as the git user.
Most likely fix: Test the connection and read the verbose output
How to fix it
Try these in order — the most common fix is first.
1. Test the connection and read the verbose output
- Run ssh -vT git@github.com
- Confirm it connects to github.com on port 22 and see which keys SSH offers
- A success message reads 'Hi USERNAME! You've successfully authenticated'
Did this fix it?2. Load your key into the SSH agent
- Run eval "$(ssh-agent -s)"
- Run ssh-add -l -E sha256 to list loaded keys
- If the list is empty, run ssh-add ~/.ssh/YOUR_KEY (or generate a new key with ssh-keygen -t ed25519)
- To test one specific key without the agent: ssh -i ~/.ssh/KEY-FILE -vT git@github.com
Did this fix it?3. Make sure GitHub has the matching public key
- Open GitHub > Settings > SSH and GPG keys
- Compare the fingerprint from ssh-add -l -E sha256 with the keys listed
- If yours is missing, click New SSH key and paste the contents of the .pub file
- Delete any key you do not recognise and contact GitHub Support
Did this fix it?4. Use the git user in the remote URL
- Run git remote -v
- If the URL is ssh://YOURNAME@github.com/..., change it: git remote set-url origin git@github.com:OWNER/REPO.git
- Retest with ssh -T git@github.com
Did this fix it?5. Stop using sudo with git
- Keys generated without sudo are not used by sudo git
- Either run every git command without sudo or generate and register keys for the root user
Did this fix it?6. Fall back to SSH over port 443 on restricted networks
- Edit ~/.ssh/config and add Host github.com with HostName ssh.github.com, Port 443, User git
- Retest with ssh -T git@github.com
Did this fix it?
Symptoms
git clone, pull or push over SSH ends with 'git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.'
Why it happens
- No private key is loaded in ssh-agent
- The public key was never added to your GitHub account (or was removed)
- The key has a custom file name that SSH does not try by default
- The remote URL uses your GitHub username instead of the git user
- Git was run with sudo, so it looks in root's keys
- Connecting to the wrong host or port
This guide also covers
- permission denied publickey github
- git@github.com permission denied (publickey)
- fatal could not read from remote repository github
- github ssh key not working
Still not working?
If GitHub keeps failing, you're not alone. See every GitHub problem users report or the alternatives people switch to.
Need a custom web app or internal tool? Our small team builds it, on your domain or hosted by us. Price agreed together.
Get a quoteOther GitHub errors
- GitHub 'Password Authentication Was Removed' Error
- GitHub Push Rejected: Updates Were Rejected (Non-Fast-Forward)
- GitHub Push Blocked: Repository Rule Violations (GH013 / Secret Detected)
- GitHub Push Rejected: File Exceeds 100 MB Limit
- GitHub 'Permission Denied' or 403 When Pushing Over HTTPS
- GitHub Locked Out: Lost 2FA Device or Recovery Codes
- GitHub Actions Workflow Stuck in Queued / Waiting for a Runner
- GitHub Actions: 'Recent Account Payments Have Failed or Spending Limit Needs to Be Increased'