84

I´m having trouble with GitHub. I´ve playing around with a remote repository of Git.

When I now try to make any changes to the remote directory, i.e.

git remote show origin

or

git push -u origin master

I get this error

Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists. Ulrichs-MacBook-Pro:coredatatest ulrichheinelt$ git push -u origin master Permission denied (publickey). fatal: Could not read from remote repository.

I would be happy, when I could start again with a new (empty) remote directory. Or is there a way, to fix this error?

This are my first steps with GitHub, started yesterday...

Edit 1

my settings at https://github.com/UlliH/CoreDataTest/settings

...

Edit 2

too early happy :-(

After setting the SSH and GPG keys, the errors are still the same. :-/

Edit 3

I think that's right so, but still the same...

enter image description here

1

18 Answers 18

84
  1. Generate SSH key using ssh-keygen -t rsa -b 4096 -C "your email".
  2. Copy the output of cat ~/.ssh/id_rsa.pub to your clipboard
  3. Paste the above-copied output to the form at https://github.com/settings/ssh/new
  4. Then go ahead to retry the operation that generated the initial fatal error.

Update: If you are still facing "ssh: connect to host github.com port 22: Connection timed out", then follow the below steps.

Check the connection, mostly it will time out

$ ssh -T [email protected]
ssh: connect to host github.com port 22: Connection timed out

Check the same by providing optional param port

$ ssh -T -p 443 [email protected]
Hi <user_name>! You've successfully authenticated, but GitHub does not provide shell access.

Update the SSH settings

$ vim ~/.ssh/config
# Add github in the know hosts
Host github.com
  Hostname ssh.github.com
  Port 443

Check the connection, mostly it will connect this time

$ ssh -T [email protected]
Hi <user_name>! You've successfully authenticated, but GitHub does not
provide shell access.
0
36
  • On your GitHub profile there is an Edit Profile button.
    It is located on top-right corner of the webpage.
  • Press it and you will see left Personal Settings menu.
  • Inside that menu find SSH and GPG keys option and press it.
  • You will see an option New SSH key to add new key.
2
  • Please find time to go through the answers and comments on this thread - stackoverflow.com/questions/12940626/… Jul 25, 2016 at 15:42
  • after updating/resetting ssh keys problem still same freezed on : debug3: send packet: type 30 debug1: expecting SSH2_MSG_KEX_ECDH_REPLY Apr 19, 2023 at 17:58
34
  1. generate your key

    ssh-keygen

  2. Visualize your keys

    ls ~/.ssh

    id_rsa id_rsa.pub

  3. Start the agent

    eval `ssh-agent`

  4. Add your key to the agent

    ssh-add ~/.ssh/id_rsa

0
10

For me I had to set what host to use what SSH key. In your local machine SSH folder, usually under ~/.ssh create/edit the file called config using your preferred editor like vim or gedit

vim ~/.ssh/config

and add the following with your git Host, HostName, and ssh IdentityFile (your ssh private key file path):

Host gitlab.example.com
    HostName gitlab.example.com
    IdentityFile /home/YOURUSERNAME/.ssh/id_rsa
2
  • after setting ssh keys, + set same in config, still same problem debug1: expecting SSH2_MSG_KEX_ECDH_REPLY Apr 19, 2023 at 17:59
  • This worked, while nothing else did. Apr 29, 2023 at 9:31
8

I was having the same problem with my ssh connection. I tried to work it through ssh, but couldn't find a working solution for it. So, in that case, I changed my remote URL from SSH to HTTPS. I used the command: $ git remote set-url origin https://github.com/USERNAME/REPOSITORY.git. You can see your remote url changed using: $ git remote -v.

You can find more detail on Here

This will change your remote URL to HTTPS so you will now have to type your GitHub username and password to push your project to the remote repo. I know ssh is easier than HTTPS meaning that you don't have to type out your username and password, but this might be helpful if you didn't find any solution for fixing it through ssh, and you are in a rush to push your code to your repo.

1
  • This would be a fine suggestion if Microsoft wasn't planning on removing that functionality.....
    – DemiImp
    Jun 18, 2021 at 21:41
8

macOS Ventura no longer supports RSA. So if your key is RSA, you will need to generate another key e.g.

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

I solved the same issue before by adding the file below in the key path.

I have created the keys using git bash (ssh-keygen) and this automatically saved the key into C:\Users\{username}\.ssh folder.

I then created a file "config" without extension and added the code below:

Host *
     PubkeyAcceptedKeyTypes +ssh-rsa

Now, try to clone again using git clone command.

4
+200

For those on Windows and still cannot get this figured out even though following the solutions above. I followed these to make it work.

  1. Open a terminal and CD to C:\Users\"your_username"
  2. Generate SSH key using ssh-keygen -t rsa -b 4096 -C "your email" in the command prompt
  3. Important aspect in the second step is that leave everything to default and add no passphrase too. Keep pressing enter untill the key is generated. I was changing the name of the file and adding passphrase and it wasn't working for me that way. Two files namely id_rsa and id_rsa.pub will be created
  4. Cd to C:\Users\"your_username"\.ssh>
  5. Copy the output of type id_rsa.pub or cat id_rsa.pub for linux to your clipboard
  6. Paste this key in the SSH keys under git or bitbucket account
  7. Close the CMD and try using git clone now and it should work

*I didn't use ssh-add id_rsa as it is done to add this private key(id_rsa) into a vault

0
2

On Windows I'd the problem when using git in powershell or command prompt. Using Git Bash on Windows solved it for me as it allows for ssh-agent to properly run in the background and allow the SSH credentials forwarding necessary to make this work.

Following the instructions at this link in Git Bash worked for me: https://docs.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

1

In my short experience using git with linux, I found there were two simple answers to this error.

run these commands in this order

git remote set-url --add origin <https://github.com/username/repo>
git remote set-url --delete origin <[email protected]:username/repo>

This will reconfigure your config file to use HTTPS origin instead of SSH.

now try running push or pull commands.

OR

Reboot your linux VM (if you're using one) and/or host machine. Rebooting has resolved the issue for me more than once.

0
  1. make sure you have named the "public key" and "private key" files properly; precisely like "id_rsa" and "id_rsa.pub". This is something that you can find in your users/.ssh folder.

  2. add the public key in GitHub

  3. Restart your terminal ( bash supported) and try to clone again

if you have the write access to the repo, you should be good to go after these changes.

Talking from experience (after spending an hour), I could not find any info on any forum that stated that we have to explicitly keep the name of the private and public file as mentioned above.

Happy coding!

1
  • Can we name these files github_rsa? Jan 11, 2021 at 16:05
0

If any of you facing same kind of issue on Bitbucket then here is the solution:

Issue:
------

Demo@L90TQCLQ MINGW64 /u/works (master) $ git clone ssh://[email protected]:5449/rem/jenkinspipeline.git Cloning into 'jenkinspipeline'... [email protected]: Permission denied (publickey). fatal: Could not read from remote repository.

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

Solution:
Demo@L90TQCLQ MINGW64 /u/works (master) $ cat < ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC99aqMXtBpVvCQb6mezTHsftC3CFY9VOpGaNmckmcTzXoOOWOheeM9V2NTrOWxpbE3UNdL/6ZnmPyv5EI2zPMPstVIP5jAqcmxOvGc2zxy4wHeGTgrP6UaXs2nLScV4E7+rmdaVtSsfOa1i+eU2eg4UnIJpRLtGD6N+hMKJXaXWpGfQ79USiPhPQKDGOz3PeEDggyvHs7HUzaKZpwEeIKYOSDXsvDwTJ6s5uQ30YfX3eoQbAis8TJeQejAmkuu62oSOs5zFJMSTAzakiyXW/xCUsLrnUSzxmBKO2BIA/tSTrqW/Gj0VhDniDFGwGz0K1NfLzfEJLWKvdB2EJWVFjEd [email protected]

Goto: https://bitbucket.internal.abc.com/plugins/servlet/ssh/projects/REM/repos/jenkinspipeline/keys 1) Add keys Copy/paste the id_rsa.pub key value there:

enter image description here
Done! Now you can able to clone the git repository

KDemo@L90TQCLQ MINGW64 /u/works (master) $ git clone ssh://[email protected]:5449/rem/jenkinspipeline.git Cloning into 'jenkinspipeline'... remote: Enumerating objects: 1146, done. remote: Counting objects: 100% (1146/1146), done. remote: Compressing objects: 100% (987/987), done. remote: Total 1146 (delta 465), reused 0 (delta 0) Receiving objects: 100% (1146/1146), 149.53 KiB | 172.00 KiB/s, done. Resolving deltas: 100% (465/465), done.

0

It is also possible that your organization blocks you from accessing it. Some organizations use "Single sign-on organizations". You need go to SSH keys page and click on "Configure SSO" to authorize the machine to clone.

0

Please run below command that works

   $ eval $(ssh-agent)
   $ ssh-add
0

Nothing was working in my case, I have another work around that is:

  1. Go to your /Users/"username"/.ssh
  2. remove all the files there in the file. (not the folder)
  3. open terminal and type ssh-keygen -t ecdsa -b 521 hit enter and enter.
  4. Provide your pub key to your server person.
  5. Start using git access. No need to make any other changes.

Happy Coding:

0
  1. You have to create a new SSH key, please access this link and adjust it to your OS : Create New SSH Key
  1. After success,you should run command like this : cat ~/.ssh/id_ed25519.pub

  2. Copy paste result above command, in the profile section -> setting -> SSH and GPG Keys -> New SSH Key (copy here)

0

To solve the following problem:

fatal: Could not read from remote repository

  1. First, remove the last origin
    git remote remove origin
    
  2. You must then change under code

Notice! Replace with your info!

git remote add origin 
[email protected]:yourGithubUsername/yourGithubRepositoryName.git

Change to

git remote add origin https://github.com/yourGithubUsername/yourGithubRepositoryName.git

For example in under code,

Change

git remote add origin [email protected]:mahdi-moazeni-artisan/reactjs-mini-projects.git

To

git remote add origin https://github.com/mahdi-moazeni-artisan/reactjs-mini-projects.git

Notice! You must replace your GitHub username and your GitHub repository name.

Then run in cmd

0

Go to .ssh folder from terminal:

   cd  /Users/YourUserName/.ssh

If there is no .ssh folder, make it by command:

mkdir /Users/YourUserName/.ssh

Then go to .ssh folder and generate private and public key with command:

ssh-keygen -b 4096

source: Generating an SSH public key

Now you should see keys are generated. with ls command, list of files are id_rsa and id_rsa.pub . Copy the public one:

cat ~/.ssh/id_rsa.pub | pbcopy

On GitLab/GitHub -> Setting/Preferences -> SSH Key -> find Add new key button and add it there.

Now close the terminal and open it again. Happy Coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.