GitHub Error Message - Permission denied (publickey)
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When you encounter the "Permission denied (publickey)" error on GitHub, it signifies an issue with SSH authentication. SSH, or Secure Shell, is a protocol used to securely connect to remote systems, like GitHub repos. It uses cryptographic keys to authenticate users, ensuring secure data transmission over unsecured networks.
Understanding the Error
The full error message typically looks like this:
This error occurs when the SSH key associated with your GitHub account is either missing, incorrect, or not accepted due to various possible issues. Here’s what could typically cause this error:
- No SSH Key Linked: You might not have an SSH key linked to your GitHub account.
- Wrong SSH Key Linked: The SSH key on your local machine doesn’t match any of the keys stored in your GitHub account.
- SSH Key Misconfigured: An error might have occurred while setting up the SSH key.
- SSH Agent not Running: The SSH agent, which manages private keys, might not be running or may not be configured to use the correct private key.
Step-by-Step Troubleshooting
Follow these steps to identify and resolve the issue:
Check SSH Keys
- List Your SSH Keys: Use the command
ls -al ~/.sshto list all files in your .ssh directory. Typically, your keys will be named something like id_rsa (private key) and id_rsa.pub (public key). - Ensure Key Existence in GitHub: Log into GitHub, navigate to Settings → SSH and GPG keys, and ensure your public key is listed.
Validate SSH Configuration
- Verify SSH Configuration: Ensure your SSH configuration is correct. The config file is located at
~/.ssh/config. Check if it includes necessary configurations such asHostName,User, and references to your private SSH key.Example of a good SSH config:
- Ensure Proper Permissions: SSH keys need specific permissions set correctly to be considered valid and secure:
Test SSH Connection
Use the command ssh -T [email protected] to test your SSH connection to GitHub. If successful, you’ll see a welcome message from GitHub.
Check the SSH Agent
- Start the SSH Agent: If not running, start it using:
- Add SSH Key to the Agent: Add your private SSH key to the ssh-agent using:
Common Solutions
- Generate a New SSH Key & Add to GitHub: If you don’t have an SSH key or it’s corrupted, generate a new one using
ssh-keygen -t rsa -b 4096 -C "[email protected]"and add it to GitHub. - Switch to HTTPS: An alternative method if SSH issues persist is to switch to cloning using HTTPS.
Helpful Table Summary
Here's an overview table to assist in debugging:
| Step | Command/Instruction | Expected Outcome |
| Check SSH Keys Existence | ls -al ~/.ssh | Lists existing SSH keys |
| Verify Key in GitHub | GitHub Settings → SSH and GPG keys | Public key should be listed |
| Test SSH Connection | ssh -T [email protected] | Successful authentication message |
| Ensure SSH Config | Check ~/.ssh/config file | Config should contain proper GitHub SSH settings |
| Permissions for SSH Files | chmod 400 for private and public keys | Sets correct permissions for SSH keys |
| Start and Use SSH Agent | ssh-add ~/.ssh/id_rsa | Private key added to SSH agent |
Conclusion
The "Permission denied (publickey)" error is a common SSH-related issue that can prevent access to GitHub repositories. By following the steps outlined above, you can diagnose and often rectify the problem, restoring your SSH connectivity to GitHub. If issues persist, considering GitHub’s documentation or contacting support for your specific environment and configuration might provide additional insights.
Related reading
- GitHub Error Message - Permission denied publickey
- GitHub fatal remote origin already exists
- GitHub How to make a fork of public repository private?
- GitHub make fork an own project
- .gitignore is not ignoring directories
- Gitignore not working
- GitHub OAuth2 Token How to restrict access to read a single private repo
- GitHub pull request showing commits that are already in target branch
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.