sabari
sabari

Reputation: 849

ssh clone not working with github

I am new to Git and GitHub.

I created a new repository and tried to clone on my local machine.

It worked for https and git-readonly URLs. That is, the following worked fine:

But when I tried git clone [email protected]:npsabari/testrepo.git, it didn't work. It gave the following error message:

Cloning into 'testRepo'...
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

And then I tried ssh [email protected], but I got the error:

"Permission denied (publickey)."

instead of the welcome message.

What should I do to fix this? What is the reason for the error?

Upvotes: 31

Views: 123018

Answers (10)

Chetan Pangam
Chetan Pangam

Reputation: 370

You can either follow the below document to add your key to ssh-agent https://help.github.com/en/articles/connecting-to-github-with-ssh

or you can run the following command to execute it temporarily

ssh-agent bash -c 'ssh-add ~/.ssh/github_rsa; git clone git://github.com/npsabari/testrepo.git'

Upvotes: 8

Siddiqui Noor
Siddiqui Noor

Reputation: 8036

In 2023, it was a nightmare to me.

No reference can help me find a solution. After some late night research I found something that worked for me. So I shared if anyone needs and can save some time.

  1. In your terminal run the following command:

    ssh-keygen -t ed25519 -C "[email protected]"

  2. You can skip adding any name, just press enter.

  3. You can add passphrase or can skip (not recommended though)

  4. Now you need to copy the code from the newly generated .pub key, to do that run the following code.

    cat ~/.ssh/id_ed25519.pub

  5. Login to Github.com, go to the setting page and then navigate to SSH and GPG keys

  6. Click on New SSH key

  7. Give any name and paste the code from .pub file and Save it.

Now you should git clone :)

Upvotes: 2

Turhan Ergene
Turhan Ergene

Reputation: 577

Quick solution for linux:

  1. Run: find -al ~/.ssh

  2. Copy the text inside of your "id_rsa.pub"

(if you do not have a ssh file then create one)

  1. Go to your GitHub account and click on your profile picture in the top right corner. Select "Settings" from the drop-down menu.

  2. In the left sidebar, click on "SSH and GPG keys."

  3. Click on the "New SSH key" button.

  4. Paste your public SSH key into the "Key" field.

  5. Try to clone with SSH again

Upvotes: 2

fabda01
fabda01

Reputation: 3763

In my case, the problem was that the machine I was using was being authenticated by a repository rather than by my user, despite having a ssh key registered to my GitHub account.

You can check who is authenticated with

ssh -T [email protected]

This happened because I had a ssh key configured as Deploy Key to one of the repositories I was working on.

Upvotes: 7

surendrapanday
surendrapanday

Reputation: 548

There are two ways to clone.
1. SSH
2. HTTPS
In my case first one had given similar issue as you got, but then I tried using HTTPS way by running following commands.
git clone https://xyz
Before that run this command git remote -v. This should show
1. git-url(fetch)
2. git-url(push)
If you see these both upon running command, it tells you that you got push right as well fetch rights. After you have configured, and set proxy properly, if you still find problem, this solution I have explained should work. If your SSH public key and private key are wrong then, you must fix that first before anything else.

Upvotes: 2

hexYeah
hexYeah

Reputation: 1160

I would like to add that if you need to use sudo, it could interfere as the SSH key is not bound to that user. You might want to edit the rights before using git rather then after.

Upvotes: 3

I experienced this - only once ever - that the key generated by ssh-keygen was broken. Checking the private and public key-files, they have the correct syntax, length, et.c.

It. Just. Didn't. Work.

On the same machine, I re-generated a key-pair. This finally let me connect to github via ssh.

Upvotes: 2

Lusitanian
Lusitanian

Reputation: 11132

The reason for the error is that you don't have your public key (~/.ssh/id_rsa.pub) uploaded to GitHub. Add it to your account(you may do this through the web panel provided by github) and it will work.

Upvotes: 4

Mihai Maruseac
Mihai Maruseac

Reputation: 21460

As per GitHub help, the error you're getting is related to wrong SSH configuration. Please follow indications for setting up SSH for GitHub and check accordingly.

Upvotes: 26

Paritosh Singh
Paritosh Singh

Reputation: 6404

Actually first you should do generate ssh keys and then save them in your account.

See the following link for more details

https://help.github.com/articles/generating-ssh-keys

Upvotes: 1

Related Questions