Krishna
Krishna

Reputation: 3167

Git: Permission denied (publickey) fatal - Could not read from remote repository. while cloning Git repository

I am unable to clone a Git repository, and getting this error:

krishna.soni@KRISHNACHANDRAS /c/Projects $ git clone http://stage.abc.com:10088/pqr
http://<url>/<repository> Cloning into '<repository>'... fatal: could not read Username for 'http://<url>': No such file or directory

I've read Bitbucket, Windows and “fatal: could not read Password for”, but still have the problem.

I proceeded further but am now getting this error

sh.exe": chown: command not found

krishna.soni@KRISHNACHANDRAS /c
$ git clone ssh://[email protected].
/projects
Cloning into 'C:/projects'...
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

> krishna.soni@KRISHNACHANDRAS /c
>     $ git clone ssh:<url>
>     ts
>     Cloning into 'C:/projects'...
>     Permission denied (publickey).
>     fatal: Could not read from remote repository.

Please make sure you have the correct access rights

and the repository exists.

Upvotes: 254

Views: 894137

Answers (30)

Alexander Ferreras
Alexander Ferreras

Reputation: 1718

  1. sudo ssh-keygen -t ed25519 -C "[email protected]"
  2. sudo cat /var/root/.ssh/{generated}.pub | pbcopy
  3. Add it to SSH keys
  4. Be sure your remote URL is https not ssh.

Upvotes: 2

Harry Bosh
Harry Bosh

Reputation: 3790

After logging into bitbucket through the browser, my push to origin starting working.

Upvotes: 0

Neeraj
Neeraj

Reputation: 41

I have been facing the same issue, suddenly it clicked to my mind what could go wrong.

Here is what I was doing wrong

While generating the ssh key was using the below command, but I wasn't using sudo and while cloning I was using "sudo git clone ssh_repo_url", because while cloning I need folder to be created which my user doesn't have the permission

My Wrong way to generate ssh

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

The correct way, if while cloning using sudo

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

  • Login your GitHub account: GitHub Login.
  • In your account, add the newly generated SSH Key: My account SSH keys.
  • Try again to git clone the project. (with sudo if key generated with sudo)

Upvotes: 0

Arjan
Arjan

Reputation: 17054

For people that come here that are just trying to get the repository but don't care about the protocol (ssh / https), you might just want to use https instead of ssh (if it's supported).

So for example you use

git clone https://github.com/%REPOSITORYFOLDER%/%REPOSITORYNAME%.git

instead of

git clone [email protected]:%REPOSITORYFOLDER%/%REPOSITORYNAME%.git

Note that GitHub deprecated password authentication (https login) for private repositories due to it not keeping up with the latest security standards, use a personal access token.

Note also that since March 1, 2022, Bitbucket users are no longer able to use their Atlassian account password with git over https as well, but instead need to use Bitbucket app passwords.

Upvotes: 308

Maxime
Maxime

Reputation: 8969

I faced the same problem with GitHub. Here's how to solve it!

  1. Generate a SSH key on your computer as described here : Generating SSH keys.
  2. Login your GitHub account: GitHub Login.
  3. In your account, add the newly generated SSH Key: My account SSH keys.
  4. Try again to git clone the project.

Upvotes: 46

lynxidngo
lynxidngo

Reputation: 149

For Fedora 33+ and Bitbucket users:

The issue happens because the hash algorithm is outdated. As mentioned on Bitbucket there are two options:

Option 1:

Add to the ~/.ssh/config file. (If the file doesn't exist, just create it.)

Host bitbucket.org
  PubkeyAcceptedKeyTypes +ssh-rsa

Option 2:

Using a more secure algorithm.

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

Upvotes: 8

Zaid Pathan
Zaid Pathan

Reputation: 16820

After changing permissions of folder in which I was cloning, it worked:

sudo chown -R ubuntu:ubuntu /var/projects

Upvotes: 5

RafiAlhamd
RafiAlhamd

Reputation: 337

I followed the steps detailed in Generating a new SSH key and adding it to the ssh-agent and

Auto-launching ssh-agent on Git for Windows.

In my case ~/.profile file was not present in my Windows. I had to create it, then added the script provided in the above link.

Upvotes: 0

pkbiswal321
pkbiswal321

Reputation: 29

Remove remote origin

git remote remove origin

Add HTTP remote origin

Upvotes: 1

GLPease
GLPease

Reputation: 591

if none of these answers help and

  1. you are using Windows
  2. you generate your key with Putty or you have installed Putty on your PC
  3. you generate your key with CMD or PowerShell

Try

  1. delete your keys
  2. search on windows for Git Bash, then generate a new key with Git Bash command line
  3. add public key to your online Repo

Then, you will find after git clone and type yes to confirm, it should begin to clone

Upvotes: 0

uldics
uldics

Reputation: 134

I see this errormessage has a lot of different reasons. I had the same error when trying to set up locally another repository alongside one already fully working with ssh etc. and connect it to same server, but other repository. No answers were here to be found, but I figured it out. So I post it. Hopefully helps someone.

git remote add origin [email protected]:teamalpha/repositorytwo.git

Upvotes: 0

bennie1
bennie1

Reputation: 187

solution: On the server you are trying to clone to or push from cat ~/.ssh/id_rsa.pub Go to GitHub, settings, SSH and GPG Keys , New SSH key paste key.

Upvotes: 0

Srikanth Kondaparthy
Srikanth Kondaparthy

Reputation: 3497

If there is still issue around access after creating SSH and adding it in your Bitbucket account or wherever, you need to do the following in terminal on GitBash, OSX or Linux:

# Lists already added identities (‘ssh keys’)
ssh-add -l

Then, if you don't see your key listed, add it with the following (replace identity with its real name):

# Add a new identity
ssh-add ~/.ssh/identity

This worked for me.

Upvotes: 173

Ankur Kedia
Ankur Kedia

Reputation: 3853

You can try adding your ssh key to your private keychain. It worked for me

ssh-add -K ~/.ssh/[your-private-key]

Upvotes: 3

nantitv
nantitv

Reputation: 3733

I had the similar issue. I change the ssh keys and restarted and tried all other 'n' solutions. But the actual issue for me was our gitlab default protocol changed from ssh to https.

check the remote url with

git remote -v

change the remote url

git remote set-url origin https://URL

Upvotes: 10

isomorphismes
isomorphismes

Reputation: 8403

fix for hub cli tool:

  • git config --global hub.protocol https for long term
  • git remote add OOPS https://github.com/isomorphisms/go.git && git push OOPS for immediate fix

This error occurs with the hub command line tool because of their wrong default hub.protocol git-config value. They set repos to

git://github.com/schacon/ticgit.git

instead of what github actually accepts, namely https://github.com/schacon/ticgit.git.


Reading LESS=+/"HTTPS instead" man hub will explain where the above "long-term fix" command comes from.

Upvotes: 2

kometen
kometen

Reputation: 7802

Useful thread and I don't bring much new to the table. The step I took is also part of the description Maxime helpfully points to but some may overlook it. The section is Adding your SSH key to the ssh-agent.

I had already generated my ssh-key but after a reboot I was unable to clone one of my own repositories.

I had to start the ssh-agent and add the private key to this agent in order to clone.

eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa

Upvotes: 6

BenKoshy
BenKoshy

Reputation: 35595

If you want to clone open source projects to submit a Pull Request:

I wanted to clone an open source project so I could submit some pull requests. The problem was that I didn't have permissions from the owner of the project. No matter, since my intention was to submit a PR, here is a viable alternative:

My solution:

  1. Fork Repository

Fork Repo

  1. Then clone from your forked repository. Clone from forked Repo

  2. Work on your features and then submit a pull request.

Upvotes: 1

Ishwar Rimal
Ishwar Rimal

Reputation: 1101

I was facing the same issue while setting up ssh for gitlab. I already have ssh for github and i could not overwrite that. The steps that worked for me are :

  1. Generate SSH with new path and add it to ssh list ssh-add /path/to/new/id_rsa.
  2. Create a file named config in ~/.ssh/ using. I used vi ~/.ssh/config/.
  3. Add this to the newly created file

# GitLab.com server Host gitlab.com RSAAuthentication yes IdentityFile /path/to/new/id_rsa

  1. Save and quit.

After that restart the terminal and try pushing, it should work

Upvotes: 0

ninjayoto
ninjayoto

Reputation: 703

In macOS / Linux (Ubuntu):

1. To Authenticate you need to add your public part of your SSH key pair to bitbucket from within your user settings: User Settings --> SSH keys

You will find the said public part in your ~/.ssh directory, usually id_rsa.pub . note the .pub part of the file name for Public. it will help you to generate one if you don't already have one

You are not done yet ...

2. You need to let your system know what key to use with which remote host, so add these lines to your ~/.ssh/config file

Host bitbucket.org
 IdentityFile ~/.ssh/PRIVATE_KEY_FILE_NAME

Where PRIVATE_KEY_FILE_NAME is the name of private part of your SSH key pair, if you haven't messed with it, usually its default name is : id_rsa in this case replace PRIVATE_KEY_FILE_NAME above with id_rsa(the private key DOES NOT have a .pub extension)

Upvotes: 6

Martlark
Martlark

Reputation: 14581

I had the same error. My solution was to logout and login again as my ssh-agent had become out of sync with the remote ssh key.

Adding this here just in case anyone has seen that the other answers don't seem to apply to their situation.

Upvotes: 0

eithed
eithed

Reputation: 4320

I've had this issue on a Mac - while I setup SSH correctly to access my Git repository, after restart (and some time the Mac was on a standoff), all my credentials were removed. Apparently, for some reason the pub key was set to 644 which caused it to be removed from the keychain. To readd:

  • chmod 600 the public key
  • ssh-add ~/.ssh/[your private key] - this should display that identity has been added. The key file you want is the one without the .pub extension.
  • ssh-add -l should show you newly added identity

edit: apparently MacOS has tendency of removing keys - after downloading the High Sierra update (but I've not installed it yet) my key has been removed and I've had to add it again via ssh-add

Upvotes: 25

nicolas asinovich
nicolas asinovich

Reputation: 3511

If you generate new public ssh key and inserted it to bitbucket or github and

it not helped - please try to restart your PC. It helped me!!

Upvotes: 7

Qingfeng
Qingfeng

Reputation: 720

Github(or Bitbucket) can't find your ssh key in their server.

Just add your key in your account setting.

Upvotes: 8

mustapha
mustapha

Reputation: 181

For me, when i wanted to clone from my repository, i had the same message noticed before "Permission denied (publickey) fatal: Could not read from remote repository". The solution for my case is to not use sudo before clone That's it.

Upvotes: 18

Ken A Collins
Ken A Collins

Reputation: 291

I got the error...

Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

...when I was trying to make contact from a Git Bash prompt to Bitbucket after my laptop died from a bad Windows 10 update and was returned to factory settings. I had restored all my ssh files from a backup.

The cause of the error turned out to be a mismatch in my Windows account name after the computer rebuild. I learned that the public key file id_rsa.pub ends with an easily readable string comprising my Windows account name followed by an @ sign and then the computer name.

When I originally set up my computer, I created my Windows account name including my middle initial but when my computer was reset to factory settings, our new DevOps guy created my account name without my middle initial.

To resolve the issue, I simply edited my public key file id_rsa.pub and removed the middle initial in my name at the end. I had stuck with the same computer name so that was okay. I copied the file contents to the Windows clipboard. I then signed into Bitbucket, went to Bitbucket Settings under my avatar, and added a new public key where I pasted in the new contents.

Back at the Git Bash prompt, I confirmed that it now worked by entering the command...

ssh -T [email protected]

...and I got back a message that I was signed in.

Upvotes: 5

Jorge de la Torre
Jorge de la Torre

Reputation: 39

While cloning, I had a similar issue [ my ERROR: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights .. etc ]

-- I was using bitBucket/UBUNTU14.04 in my case, but ALREADY had a set of key files that I had previously generated AND I had changed the name of the files. I simply COPIED the files to the standard id_rsa & id_rsa.pub name format. I then re-ran the command with out issue.

OBTW: I could have also used the password prompt by using the HTTP style clone.

Upvotes: 1

Ronak Agrawal
Ronak Agrawal

Reputation: 1066

If all fails, as was the case with me, Uninstall GIT, reinstall. For some reason this fixes.

P.S.

  1. I tried generating new Keys and Updating at SSH public keys
  2. Also tried deleting all keys and tried to create a fresh key.
  3. And NO, nothing wrong with my .gitconfig

Upvotes: 2

Mijail
Mijail

Reputation: 186

It may be stupid but it happened to us:

If you are using bitbucket and Sourcetree and you just copy paste the clone URL to the new repo dialog it will show the same error when pulling or pushing.

Make sure you delete the 'git clone' stuff before the URL.

Upvotes: -2

Jeff Silverman
Jeff Silverman

Reputation: 724

I had a similar problem on linux. I solved the problem by logging into the github server and creating a deploy key. That's under settings for the repository. Then, I copied and pasted my public key (which is usually in ~/.ssh/id_rsa.pub, but your configuration might be different). There is a check box to give this key write access. Click on it (unless you are using git to deploy only, in which case, don't click on it).

Upvotes: 0

Related Questions