[Fixed] Git Clone git@github.com: Permission denied (publickey) Could not read from remote repository Fatal error

GitHub clone error:
➜  / git clone git@github.com:github-username/repo.git

Cloning into 'c2c-2022'...

The authenticity of host 'github.com (' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wxyV6TuJJhbpZisF/zLDA0zX2A4KZJHY2.
This key is not known by any other names

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.

git@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.

Fix: Solution 1

The most common reason is you are trying to clone the GitHub repo locally and you have not set the access tocken

Steps to set github access token

  1. Click on your profile icon on github.com,
  2. Now click on Settings,
  3. Scroll down and select Developer settings from the left side menu options,
  4. Now click on Personal access tokens,
  5. Now click on the button Generate new token
    Github generate new personal access token
    Github generate new personal access token
  6. You will be prompted to enter your github.com password again,
  7. Now you will need to add a note to tell what the token will be used for (this token acts like an OAuth token and can be used for API authentication using basic auth. You can also set the expiration for the token from 7 days to 30 days or never expire.
  8. Now you can select the scope of the token, I have set repo: "Full control of private repositories"
  9. Now click on Generate Token button
  10. You should be able to see your token now, copy it.

    Do make sure to copy your personal access token on the page as you will not be able to see it again!

% git clone https://<Personal-token>@github.com/<user-name>/<repo-name>.git

Cloning into 'git-repo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

Fix: Solution 2

The better way is to generate an SSH key locally and add it to the GitHub Settings,

  1. Open local terminal and run the below command with the email setup with your GitHub account,
    ssh-keygen -t ed25519 -C "name@domain.com"
    Generating public/private ed25519 key pair.
    Enter file in which to save the key (/root/.ssh/id_ed25519): githubkey
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in githubkey
    Your public key has been saved in githubkey.pub
    The key fingerprint is:
    SHA256:uxABXYZOK4grihAz7B3Afhv3/tanAb7axtjh2BRo4H+Y name@domain.com
    The key's randomart image is:
    +--[ED25519 256]--+
    |.o.o++.o  ..     |
    |o.o+oo=  .  o.   |
    |+o. +o.   .o. .  |
    |+. o.. .   ..E   |
    | .. .S  o . ...  |
    |    . +  + ...   |
    |     o .. o..    |
    |      .  oBoo.   |
    |       ..ooB=    |
  2. Add the SH key to the ssh-agent:
    % eval "$(ssh-agent -s)"
    % ssh-add ~/.ssh/githubkey
  3. Now add the ssh key to your GitHub account: cat ~/.ssh/githubkey.pub copy the text,
  4. Go to GitHub.com account, click on your profile picture and select Settings,
  5. Now click on SSH and GPG keys,
  6. Click on button New SSH key
  7. Add the copied .pub file content into key text area.
  8. Click Add SSH key

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap