76

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

starball
  • 20,030
  • 7
  • 43
  • 238
Ulli H
  • 1,748
  • 1
  • 19
  • 32

17 Answers17

74
  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 git@github.com
ssh: connect to host github.com port 22: Connection timed out

Check the same by providing optional param port

$ ssh -T -p 443 git@ssh.github.com
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 git@github.com
Hi <user_name>! You've successfully authenticated, but GitHub does not
provide shell access.
folaRin
  • 109
  • 8
Shravan40
  • 8,922
  • 6
  • 28
  • 48
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.
Evgeny Karkan
  • 8,782
  • 2
  • 32
  • 38
  • Please find time to go through the answers and comments on this thread - http://stackoverflow.com/questions/12940626/github-error-message-permission-denied-publickey – Evgeny Karkan Jul 25 '16 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` – Vladimir Ch Apr 19 '23 at 17:58
31
  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

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
Waqleh
  • 9,741
  • 8
  • 65
  • 103
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.

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

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 "your_email@example.com"
Flair
  • 2,609
  • 1
  • 29
  • 41
4

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

Rohit Singh
  • 401
  • 2
  • 6
3

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.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Sanooj
  • 47
  • 6
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

Vikash Madhow
  • 1,287
  • 11
  • 15
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 <git@github.com: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.

Blusteel408
  • 121
  • 1
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!

Vikas Pandey
  • 197
  • 7
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://git@bitbucket.internal.abc.com:5449/rem/jenkinspipeline.git Cloning into 'jenkinspipeline'... git@bitbucket.internal.abc.com: 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 demo.user@abc.com

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://git@bitbucket.internal.abc.com: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.

nk07
  • 161
  • 3
  • 12
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.

jtony
  • 101
  • 2
  • 4
0

Please run below command that works

   $ eval $(ssh-agent)
   $ ssh-add
Rameshwar Vyevhare
  • 2,699
  • 2
  • 28
  • 34
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:

MRizwan33
  • 2,723
  • 6
  • 31
  • 42
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)

Hari Agus W
  • 71
  • 1
  • 3
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 
git@github.com: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 git@github.com: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