64

When I want to push to github with this command

git push origin master

I got this

Permission denied (publickey).
fatal: The remote end hung up unexpectedly

So, what's wrong?

Anthony Mastrean
  • 20,318
  • 20
  • 92
  • 173

15 Answers15

77

EUREKA!

Apparently, you can use plink as the main ssh client and just load your keys in pageant (if you're like me, you already do):

You can do that by setting the GIT_SSH env variable to plink.exe path like so:

set GIT_SSH=C:\Program Files\PuTTY\plink.exe

or, you can use plink from TortoiseGit:

set GIT_SSH=c:\Program Files\TortoiseGit\bin\TortoisePLink.exe

Credit: Original solution taken from this blog post

tutuDajuju
  • 8,633
  • 3
  • 55
  • 83
  • 2
    Bingo, this is what I was looking for. :-) You can just set these values as permanent environment variables through Control Panel > System > Advanced System Settings > Environment Variables. – Simon East Feb 22 '14 at 05:11
  • After a couple of weeks of pain came up with this solution, too ) Actually copied the plink.exe to the c:\windows, because setting env variable did not really help. – Alex Buznik Nov 23 '14 at 15:02
  • 1
    The permanent version of the commandline for Putty on a 64-bit system is: `setx GIT_SSH "C:\Program Files (x86)\PuTTY\plink.exe"` – karel_evzen Nov 18 '16 at 21:21
  • Fantastico! Still in the year of grace 2020 this post is very helpful! I'm running Win7 (yeah, I know) and using PuTTY. One important thing to add here (that is in the linked blog post): I had a lot of problem with adding the fingerprint to known host from the command line. The solution is to open PuTTY and just enter e.g. `github.com` in `host` and enter, then you can accept the host. – 244an Mar 29 '20 at 01:43
45

I was able to resolve this issue as follows:

When you do:

ssh-keygen -t rsa

it prompts you to (optionally) enter a filename for saving the generated keys. Specifying a filename wasted my whole day! Next day I let it use the default filename and the problem(s) disappeared! Imagine!!

Platform was Win7 and msysgit.

G Shah
  • 1,725
  • 2
  • 14
  • 16
  • 15
    For Windows and msysgit: if using a custom filename for your keys (instead of the default `id_rsa`), then make sure to add this to `~/.ssh/config`: `IdentityFile ~/.ssh/customname` (replacing `customname` with the name of your key, without `.pub`). This doesn't appear necessary in a Linux environment. – matb33 Jun 20 '12 at 14:28
  • I have been having issues with this for a week. Thx. – Brandon Poole Sep 05 '12 at 14:42
  • In what way you can leave default name ? Because it's mandatory to specify some file name ? – user1459144 Nov 26 '13 at 10:00
  • @user1459144 When it prompts you to enter a filename, a default filename is displayed. From what I can recall, if you don't enter any filename (i.e. just press the Enter key), it will proceed with the default filename. – G Shah Nov 27 '13 at 18:35
  • you saved my day man, i generated the key then added the .pub one to my github account then it worked fine. – Mahmoud Felfel Jun 25 '15 at 18:54
40

Here is a step-by-step guide that I used to get this to work.

Platform: Windows 7

Install msysgit from http://msysgit.github.io/

During installation, accept all of the default options, except when the 'Select Components' option appears. When this appears, select 'Git Bash Here' option. Although this isn't necessary, it adds a nice context menu when working in Windows Explorer that I found to be very helpful.

enter image description here

Once msysgit is installed Git Bash will also be installed. Open Git Bash in one of 2 ways:

  • Click the Windows Start key and start typing Git Bash
  • Or, right click somewhere (e.g. your Desktop) and select Git Bash Here. This option is only available if 'Git Bash Here' context menu was installed.

In Git Bash's command window, enter this:

$ ssh-keygen -t rsa

When asked to enter a file name, just accept the default. Choose a strong passphrase when prompted, and your public key should now be saved. Your screen should look like this:

enter image description here

Go open the public key file in Notepad. The file should reside here:

C:\Users\{username}\.ssh\id_rsa.pub

Copy all of the content in the file to your clipboard, then go to GitHub's SSH settings page:

https://github.com/settings/ssh

Choose 'Add SSH key', enter a useful 'Title' and paste the content into the 'Key' textarea.

To simplify your life, you can use the SSH agent to save your passphrase so that you don't need to remember it. To do so, type this into Git Bash:

$ eval `ssh-agent -s`
$ ssh-add ~/.ssh/id_rsa

You'll be prompted to enter your passsphrase. If everything succeeds, your identity will have been added. Note: this passphrase will be forgotten as soon as you close your shell. I'm not sure how to make this persist across sessions, but maybe someone can help?

To test that everything works, enter this into Git Bash:

$ ssh -T git@github.com

You should see a 'success' meesage.

Sources:

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

https://help.github.com/articles/working-with-ssh-key-passphrases/

explanation on why eval `ssh-agent -s` should be used instead of just ssh-agent -s

https://stackoverflow.com/a/17848593/188740

Community
  • 1
  • 1
Johnny Oshika
  • 45,610
  • 33
  • 151
  • 234
  • 4
    I did everything described here. Except password saving. But it doesn't work. I still get "Permission denied (publickey)." after command "ssh -T git@github.com". I don't know where there could be a problem. – Sergey Dirin Jan 09 '15 at 10:05
  • That's very strange. I went through this process on 2 machines (Windows 7 and Windows 8) and they both worked for me. Can you try saving the password? – Johnny Oshika Jan 09 '15 at 14:54
  • Turned out it doesn't work only for console. Any app works fine. – Sergey Dirin Jan 13 '15 at 11:27
  • Actually, the command: `eval \`ssh-agent -s\`` is the key. I use this oneline command to solve the problem. – Ni Xiaoni Aug 25 '16 at 05:02
  • 1
    @GabrielDevillers here, directly input: `$ eval \`ssh-agent -s\`` – Ni Xiaoni Aug 12 '18 at 15:33
11

Have you generated an SSH key for yourself and added it to your Github account? They have a guide for this here.

Veeti
  • 5,149
  • 3
  • 29
  • 37
  • Hi i have tried steps the above url.. but after executing ssh-add ~/.ssh/id_rsa this it throwing error "could not open a connection to your authentication part" . Can you plz help me – AnNaMaLaI Dec 03 '14 at 09:05
7

Using Windows 8 to setup your ssh and Github

  1. If it says "Permission denied (publickey)" you will have to put in a passphrase for your key. Do not be tempted to just press enter...this was what worked for me...it took me five hours to realize that pressing enter made OpenSSH feel that your key was too public so that is why it is denying you from going to the next step.

  2. If it says "The authenticity of host 'github.com (203.232.175.90)' can't be established." Then you will have to go through the process shown here: [Google] (https://help.github.com/articles/error-permission-denied-publickey.)

Go to the part that says "Verify the public key is attached to your GitHub account". You want to start with the part that says account. (I am assuming you already have an account.

Also, you want to be in C:\RailsInstaller\Git.ssh and when I did 'dir' I noticed that I had '.', '..', 'known_hosts'. I had to get a new key by doing the following (I found this on the Github site):

"ssh-keygen -t rsa -C "your_email@example.com" # Creates a new ssh key, using the provided email as a label # Generating public/private rsa key pair. < # Enter file in which to save the key(/c/Users/you/.ssh/id_rsa): [Press enter]"

And pay attention to the file where the key will be saved. Do not just press enter.. my one showed "(//.ssh/id_rsa)" I had to type "/.ssh/id_rsa" to make sure it was in the right directory.

And you should be able to type ssh in the command line. If you cannot do that you will have to add it to your path.

Tasha
  • 153
  • 2
  • 9
4

I solved a similar problem by adding a System Environment Variable. The key was that I am pointing to the git.exe inside the cmd folder

name: GIT
value: C:\Program Files (x86)\Git\cmd\git.exe

mynkow
  • 3,790
  • 4
  • 31
  • 59
3

there could be something wrong with your heroku keys. try:

heroku keys:add

similar question here: Heroku Git - fatal: The remote end hung up unexpectedly

Community
  • 1
  • 1
Alexander Taylor
  • 13,171
  • 10
  • 56
  • 75
3

If you have already generated the key, and it has a default name, the problem might be in the absent environment variable.

On Windows 7 right click "My computer" and go to properties. There click on advanced properties and click the button Environment Variables. There add a user environment variable

Name: HOME 
Value: %USERPROFILE%
Vladimir Beletskiy
  • 188
  • 2
  • 2
  • 10
2

The problem may be related to the order in which the ssh.exe are in the PATH environmental variable.

In my case there was a ssh.exe in both the C:\Program Files(x86)\git and also in my C:\cwgwin directory. When I was in my Git Bash everything worked fine (it was using the ssh in the git directory) but when I was in my Command Prompt it was using the ssh in my cygwin directory.

When I tried to access github it was not able to find the .ssh folder with my private key because the key was not attached to the ssh the command prompt was using (ie the C:\cygwin one) and that is why I was getting the Permission Denied error.

What I did was rename (or delete) the git.exe and ssh.exe in my C:\cygwin directory and make sure that C:\Program Files(x86)\git appears before the C:\cwgwin in the PATH order.

Leo Moore
  • 1,982
  • 2
  • 18
  • 20
  • what if you actually prefer using cygwin git instead of msysgit? it's a lot more up to date... – eis May 07 '15 at 04:40
1

Here is a solution to a very specific problem that has the same error signature. This was the mistake I made and it is very easy to make. Basically, instead of doing this

  git remote add origin git@github.com:myusername/myrepo.git

I did this (note typo)

  git remote add origin get@github.com:myusername/myrepo.git

http://www.celticwolf.com/blog/2011/02/08/git-permission-denied-publickey/

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
Alan Berezin
  • 101
  • 1
  • 1
  • 4
0

I had a similar problem:

I created "redssh" and "redssh.pub" in some directory using git bash. Permission denied...

however, "id_rsa.pub" and "id_rsa" had appeared in: C:/Users/myName/.ssh/

copy id_rsa.pub contents to github

xxjjnn
  • 12,522
  • 19
  • 52
  • 77
0

I'm a total newb with git and was following some installation instruction on a website and was getting the permission denied (publickey) error.

I followed the instructions to generate the RSA key pairs (ssh-keygen -t rsa). This worked fine but I was still getting the error. What I did not realize was that you have to actually go the the GIT website (github.com), register and enter that key on their website.

I hope this helps out another poor newb out there.

Cheers!

0

I had similar Prob in win 8.

Very Simple mistake I did: I have already created public key in git hub account so that y keys was not getting match.

Solution:

  • delete ssh folder and key from github account and do it again the process

1) ssh-keygen -t rsa -C "your mail address"

2) ssh -T git@github.com

PiotrWolkowski
  • 7,550
  • 5
  • 38
  • 58
0

Here is the default output for Windows 7.

c:\test\app>ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (//.ssh/id_rsa):
Could not create directory '//.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
open //.ssh/id_rsa failed: No such host or network path.
Saving the key failed: //.ssh/id_rsa.

Instead of command prompt git is to be used as per http://help.github.com/win-set-up-git/ ??

uhbl92
  • 31
  • 8
  • Just want to add that the portable version of GitHub will give you loads of pain, go ahead and install it in the first place. – cracked_all May 06 '12 at 09:41
-1

I discovered that my problem was that whatever version of ssh-keygen that I used created the files with the wrong filenames... The files initially created where %USER_HOME%.ssh\ida_rsa and ida_rsa.pub, but git expected them to be id_rsa and id_rsa.pub.

I solved the problem by running git bash, THEN running ssh-keygen

KbCb
  • 19
  • 3