Photo by Joan Gamell on Unsplash
How to Resolve 'Permission Denied (publickey)' Errors with GitHub: A Comprehensive Guide for Mac, Linux, and Windows
Problem Statement
SSH authentication failures when interacting with Github repos can be frustrating. Which is something I recently encountered and had to solve. So hopefully this will be of help to someone .This article aims to provide a step-by-step guide to diagnose and solve this issue across Mac, Linux, and Windows platforms.
Introduction
SSH is a secure way to interact with repositories on GitHub. However, setting it up might sometimes result in the infamous 'Permission denied (publickey)' error
. This article aims to be a one-stop solution for resolving this issue.
Solutions
For Mac Users
Generating New SSH Key
Open Terminal.
Run
ssh-keygen -t rsa -b 4096 -C "
your_email@example.com
"
Adding SSH Key to SSH Agent
Run
eval "$(ssh-agent -s)"
Run
ssh-add ~/.ssh/id_rsa
Adding SSH Key to GitHub Account
Copy the SSH key:
pbcopy < ~/.ssh/id_rsa.pub
Paste it in GitHub -> Settings -> SSH Keys
For Linux Users
The steps are largely similar to Mac, with the key difference being in copying the SSH key to the clipboard. You can use xclip
for that purpose:
Install
xclip
:sudo apt install xclip
Run
xclip -sel clip < ~/.ssh/id_rsa.pub
For Windows Users
Windows users can use Git Bash to generate SSH keys and then add them to the SSH agent.
Open Git Bash.
Generate an SSH key:
ssh-keygen -t rsa -b 4096 -C "
your_email@example.com
"
To add the key to the SSH agent, you can use:
// Start the ssh-agent in the background
eval $(ssh-agent -s)
// Add your SSH key to the ssh-agent
ssh-add ~/.ssh/id_rsa
Conclusion
Encountering 'Permission denied (publickey)'
errors while working with GitHub can disrupt your workflow. However, with the guidelines provided in this article for Mac, Linux, and Windows users, you can resolve this issue and get back to coding in no time!