Learn how to fix GitHub’s “Permission Denied” error with our step-by-step guide. Check your SSH keys, update remotes, and adjust file permissions effortlessly.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Understand the “Permission Denied” Error
When you run a Git command that communicates with GitHub over SSH (for example git push
), you may see an error like:
Permission denied (publickey).
fatal: Could not read from remote repository.
This means GitHub didn’t accept your SSH key. The most common causes are:
Step 2: Check for Existing SSH Keys
Verify if you already have SSH keys in your local machine:
ls -al ~/.ssh
Common key file names are id_rsa
and id_rsa.pub
or id_ed25519
and id_ed25519.pub
. If you see at least one .pub
file, you have an existing public key.
Step 3: Generate a New SSH Key
Create a new SSH key using the ssh-keygen
command:
ssh-keygen -t ed25519 -C "your\[email protected]"
When prompted:
Enter
to accept the default file location.Enter
for no passphrase.This produces two files: your private key (~/.ssh/id_ed25519
) and your public key (~/.ssh/id_ed25519.pub
).
Step 4: Add Your SSH Key to the SSH Agent
Ensure the SSH agent is running and your key is loaded:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id\_ed25519
Step 5: Add Your SSH Key to Your GitHub Account
Copy the contents of your public key and add it to GitHub settings:
cat ~/.ssh/id\_ed25519.pub
Step 6: Update Your Git Remote to Use SSH
Verify your remote URL and switch to SSH if needed:
git remote -v
If you see an HTTPS URL (https://github.com/...
), change it to SSH:
git remote set-url origin [email protected]:username/repo.git
Replace username/repo.git
with your repository’s path.
Step 7: Test the SSH Connection
Run a test command to confirm authentication:
ssh -T [email protected]
Hi username! You've successfully authenticated...
Step 8: Fix File Permissions on Your Private Key
SSH requires strict permissions on your private key. Ensure only you can read it:
chmod 600 ~/.ssh/id\_ed25519
Then repeat Step 7 to test the connection again.
Step 9: Alternative—Use HTTPS or Personal Access Token
If SSH still fails, you can switch back to HTTPS and use a Personal Access Token (PAT):
git remote set-url origin https://github.com/username/repo.git
You have now resolved the “Permission denied” error and can push, pull, and clone repositories on GitHub without issues.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.