I formatted my laptop after a long time. After installing all the necessary software and setting up my coding environment, I was ready to begin coding. However, when I attempted to clone this website’s repository, I encountered the following fatal error:
***@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists
Although I installed Git correctly, I forgot to set up an SSH key to enable pulling from or pushing to any GitHub repository. Since it had been a while, I had to search online for guidance. To save time in the future, I am documenting the steps I followed to resolve the issue.
note
This article is intended for users of the Windows operating system.
What is SSH?
SSH (Secure Shell) is a network protocol that enables encrypted and secure remote communication between two computers. It uses robust encryption mechanisms to protect data transmission and prevent any middle-man attacks, ensuring privacy and security.
For example, if we have two computers, A and B, and we want A to have remote access to B, SSH is the tool to use. Once a computer is connected to the internet, it might receive numerous access requests from various devices on the network—often attempts by hackers. But how does a computer distinguish legitimate access requests from malicious ones? This is where SSH keys
come into play.
What are SSH keys?
SSH keys serve as unique identifiers for a computer, ensuring secure authentication. These keys are generated in pairs: a private key, which remains confidential to the user and must be encrypted and securely stored, and a public key, which can be freely shared with any SSH server the user intends to access.
In the example above, if computer A wants access to computer B, an SSH key must be generated on A. A’s public key is then stored on B to recognize A’s identity. When an SSH request is initiated, the following conversation like interaction has to happen before B grants access to A:
Computer A: Hey B! I'd like to access you.
Computer B: Who are you?
Computer A: I am ....
Computer B: Show me your SSH key.
Computer A: Here it is
Computer B: Let me verify.... Ah, you're on my access list. Access granted!
This secure handshake process ensures that only trusted devices can establish a connection. For this reason, we need to generate an SSH key on our local machine and add its public key to our GitHub profile to obtain file access permissions from GitHub’s servers. With that in mind, let’s move on to the steps to resolve this issue.
Steps to add SSH Key
Before proceeding with the steps below, ensure that Git is installed on your system. If it is not yet installed, you can download and install it from the official git download page .
Generating an SSH key
To generate an SSH key for your GitHub profile on your local machine, you’ll use Git Bash.
- Open Git Bash
- Copy and paste the following command into Git Bash, replacing
[email protected]
with the email address associated with your GitHub account:
ssh-keygen -t ed25519 -C '[email protected]'
When prompted to “Enter a file in which to save the key,” you can press ENTER to save the key in the default location. On Windows, the default location is C:\Users\[YOUR_USERNAME]\.ssh
. If you choose to specify a different location, make sure to note it, as you’ll need it later.
You can also choose to either omit or provide a passphrase when prompted to “Enter passphrase.”
Adding SSH key to SSH-agent
To add the SSH key to the agent and ensure the agent runs as a service, execute the following commands after running your terminal as administrator.
# start the ssh-agent in the background
Get-Service -Name ssh-agent | Set-Service -StartupType Manual
Start-Service ssh-agent
# adding the key to ssh-agent
ssh-add C:/Users/[YOUR_USERNAME]/.ssh/id_ed25519
If you specified a different location when generating your SSH keys, be sure to replace C:/Users/[YOUR_USERNAME]
with your specific directory path.
Storing generated key on GitHub
While in your terminal, use the following command to copy your SSH key to the clipboard. This will allow you to easily paste the key into your GitHub account.
cat C:Usersmblon.sshid_ed25519.pub | clip
Once the key is copied, head to your GitHub account and follow the below steps, which are directly taken from GitHub’s documentation page.
- In the upper-right corner of any page on GitHub, click your profile photo, then click Settings.
- In the “Access” section of the sidebar, click SSH and GPG keys.
- Click New SSH key or Add SSH key.
- In the “Title” field, add a descriptive label for the new key. For example, if you’re using a personal laptop, you might call this key “Personal laptop”.
- Select the type of key, either authentication or signing. For more information about commit signing, see “ About commit signature verification .”
- In the “Key” field, paste your public key.
- Click Add SSH key.
- If prompted, confirm access to your account on GitHub.
After completing the above steps, you will be ready to clone repositories or push and pull from GitHub.