Alex Pavlov
Alex Pavlov

Reputation: 581

Git clone. key_load_public: invalid format Permission denied (publickey)

I used puttygen.exe on Windows 10 to generate private and public keys. I saved that keys in C:\Users\Alexander\.ssh\

Public key was added to remote repo (Not by me. I don't have an access).

Then I used command in Git Bash:

git clone git@ipaddress:project_name.git

And I got an error:

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

Example keys (generated for just for example)

id_rsa.ppk

PuTTY-User-Key-File-2: ssh-rsa
Encryption: none
Comment: rsa-key-20170110
Public-Lines: 6
AAAAB3NzaC1yc2EAAAABJQAAAQEAx0UhtZcgUT5XpoNlcoVFGHbArEsARQVCv5m0
TRh90Xq15gxOvL+x7I0B29xOuOP054RtQaOzHqnKUzpMdrIoZFkYEYJ11p42kC05
PVR/CwtKBuONJZzoIveJlNG1IhbC3G8DMZD5j68T5OVbCqftHMIBe4CTr7TewJ9T
/lmSZPytWXk/Xtcvn1i1TQZS2ShtSNOwtx77fLzkVmC6F4uM2JgJ9bSM2xQZTX/j
DYZTtoEsmyBadANAEZx4kQAoITwxXVRPBPJnB74EdSMXNhrDBJ+sZSEy7kxmc3a/
UK6CzdN3wiEMd/Bb1nuzR2cpWrWhniG66lnOTJb3sF8iiVtw7Q==
Private-Lines: 14
AAABAEtmRBsyQ5RcxzgWCrW14sr8gEExIrJVBH/ZSyQXGtmkDXmjysP1gZfGpsHk
qCpIaoEdWcXPPNsrfPzloGRDaTq57W5otvdCyImUkjLhs4ejaB5IQz6qEqVya2i+
DS9+O+S0YhLBO9WAhBFrijtiIl3oivB11wQ2mXlRCwZLZ9MugQ3rPfS0O/E2asrO
+MyiR2uv84lVb2wUT0Be3eeubnT2Qp8CoX3qV93LwBM053tepmD0jtSBsaXepADw
mdjBfIkUm+z/69PKtsEoYtIRe5DQRGQhUrwZasJnrfwAvdkr56NKM/rEL0ocUPDX
pXQalD88fKHKog5pr25c8aImklUAAACBAPmqhoRBWy+VJxgZ2BeMYv2PbmQCrQyp
ZVqua9byU59cANjarPcEb1zSUVEJyO8KlTW1eiTeMrya3cdrqbk3Rhp8XgolRsrv
ZK2rMQxP5nIoHyndtG1CCVqrWnwjIsb4r4cq2aBaUWjQJ+ofpiUHVkHev+h58zEA
zpDZs0Jrv5n7AAAAgQDMU04pqQ1hnv050gs8C2Gy+DjbW8b/NbQl64x0HFik/lWB
CRLrCd341ZQWY0PcU5ZjwNa8GwwJZPJI2bX84/a7dq2ENnT1+uYqpuK8lnPTcBDy
WjlGcf/fWJyJpdLqqXkMZ7or4k9ReVIXz9A8xRkhGEH2xM8Vk2fyoLY88RFUNwAA
AIA+zxdPdF8dPr8HwTJ5Pb0NQjl2likOxR24QmqxlCu/bD1p8R1tZzX5Jh2HP2on
RO+KVLYHzNKqtKRZW/MHftYcm6AvYLhP7hxG/tFoNM9lEmsqdJenxaWP745LP9Pc
k3qh7kC4KfTXyfgsd+C6dXzqhAokz81zL41QtyJxlQzR2w==
Private-MAC: ce79771084cbabf61fc2bf3b1403e42a9957e2af

id_rsa.pub

---- BEGIN SSH2 PUBLIC KEY ----
Comment: "rsa-key-20170110"
AAAAB3NzaC1yc2EAAAABJQAAAQEAx0UhtZcgUT5XpoNlcoVFGHbArEsARQVCv5m0
TRh90Xq15gxOvL+x7I0B29xOuOP054RtQaOzHqnKUzpMdrIoZFkYEYJ11p42kC05
PVR/CwtKBuONJZzoIveJlNG1IhbC3G8DMZD5j68T5OVbCqftHMIBe4CTr7TewJ9T
/lmSZPytWXk/Xtcvn1i1TQZS2ShtSNOwtx77fLzkVmC6F4uM2JgJ9bSM2xQZTX/j
DYZTtoEsmyBadANAEZx4kQAoITwxXVRPBPJnB74EdSMXNhrDBJ+sZSEy7kxmc3a/
UK6CzdN3wiEMd/Bb1nuzR2cpWrWhniG66lnOTJb3sF8iiVtw7Q==
---- END SSH2 PUBLIC KEY ----

What may be wrong here?

Upvotes: 17

Views: 49464

Answers (3)

Nick Misner
Nick Misner

Reputation: 81

Sorry for answering an old question, but the listed solutions didn't help. Here's what worked for me:

Git may not be using the correct SSH client for outgoing connections.

I was having a similar issue and ended up finding an answer that set me on the right track to fix it. From what I understand, both Git & My command line where generating key-pairs using openssh, but Git was attempting to open them using it's own SSH client.

I'm not sure why Git would use two different ssh clients, one for generating, one for connecting, but this was evidenced by being able to verify my key by opening an SSH session to GitHub, but not connect to a repository. A further check seemed to validate the theory:

C:\where ssh
C:\Windows\System32\OpenSSH\ssh.exe

And in git bash:

$ where ssh
C:\Program Files\Git\usr\bin\ssh.exe

While trying the commands listed in the answer didn't fix my problem, they led me on a much more successful google search that got me to this GitHub comment, which contained the following single line command, to be run in git bash:

git config --global core.sshCommand "'C:\Windows\System32\OpenSSH\ssh.exe'"

This command will not output anything if it works, but it will set git to use the ssh client packaged with windows instead of the one included with git. After running this command, Git worked flawlessly for me.

Weirdly enough, running "where ssh" in git bash now returns two paths, but it doesn't seem to be causing any more issues:

$ where ssh
C:\Program Files\Git\usr\bin\ssh.exe
C:\Windows\System32\OpenSSH\ssh.exe

Upvotes: 8

Raz Buchnik
Raz Buchnik

Reputation: 8401

Verify the next things:

  1. Check if you have an empty id_rsa.pub file in the ~/.ssh directory. There could be more than one .pub files in there (depend on what you have already created). If you find a blank .pub file, go to where it should be kept in a remote server, in the ~/.ssh/authorized_keys file (on the server). Copy it's values and paste in your local public key file. Save.

  2. If everything correct in your local public keys, check that you didn't pasted the private key to your server or something.

Upvotes: 0

Jakuje
Jakuje

Reputation: 25956

Git is using OpenSSH and it does not support PuTTY key formats. You need to convert it to OpenSSH format. This was described in SO documentation, now in the answer to Not able to add SSH key in Jenkins configuration:

  • Open PuttyGen
  • Click Load
  • Load your private key
  • Go to Conversions->Export OpenSSH and export your private key
  • Copy your private key to ~/.ssh/id_rsa

Upvotes: 47

Related Questions