Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot parse privateKey: Unsupported key format

In my Mac book pro with OS Majave, I used SSH-KEYGEN generate a new SSH key for node.js module SSH2. However, I got error message: Cannot parse privateKey: Unsupported key format

I validated the key with ssh command, it just works fine. But not with the node.js module SSH2.

In the private key file, the header is -----BEGIN OPENSSH PRIVATE KEY----- while the end is -----END OPENSSH PRIVATE KEY-----.

I checked module ssh2-streams keyParser.js. The regexp pattern RE_HEADER_OPENSSH_PRIV does not include my header. Can anyone help?

like image 299
George Zhang Avatar asked Nov 20 '18 19:11

George Zhang


People also ask

Can't parse PrivateKey?

This way there is no Cannot parse privateKey error. Please use ssh-keygen -m PEM -t rsa -b 2048 to generate your keys (the PEM format is required). Additionally, when I didn't specify bits with the -b option Azure told me the key file was too long.

How to change OpenSSH private key to PEM?

New keys with OpenSSH private key format can be converted using ssh-keygen utility to the old PEM format. There is no need to downgrade to older OpenSSH just to achieve this result. Some elaboration on the above answers to provide a clear path for both the public and private key.

How to export ssh-keygen to PEM format?

Downgrade your ssh-keygen binary (you can easily get old version from any linux/docker image) Add option -m PEM into your ssh-keygen command. For example, you can run ssh-keygen -m PEM -t rsa -b 4096 -C "[email protected]" to force ssh-keygen to export as PEM format.


2 Answers

Had the same problem, found the solution here.

basically use the PEM option when you create your key:

ssh-keygen -m PEM -t rsa 
like image 190
Dozon Higgs Avatar answered Sep 18 '22 06:09

Dozon Higgs


You could also convert your existing key, rather than create a new one (make sure you back it up before you run the following command as it will overwrite your original one):

ssh-keygen -p -m PEM -f ~/.ssh/id_rsa

(Original answer from https://serverfault.com/a/950686)

like image 45
Arnaud Valle Avatar answered Sep 18 '22 06:09

Arnaud Valle