Ssh – PEM_read_PrivateKey failed

amazon-web-servicesmac-osxsshssh-keys

I'm trying to use ssh to log in to AWS from OSX Mavericks and having a hell of a time. I enter:

ssh -i ~/.ssh/filename.pem
ec2-user@ec2-xx-x-xxx-xx.compute-1.amazonaws.com -v

And I get this, as well as a keychain popup that asks for a password, even though there is none to give…

debug1: key_parse_private_pem: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
Saving password to keychain failed.

I launched a new instance on AWS and generated a new key pair. When I left the .pem file unprotected, the OSX keychain popup didn't appear, but I was unable to access AWS because the file was unprotected:

Permissions 0644 for '/Users/cvn/.ssh/chris-test.pem' are too open.

It is required that your private key files are NOT accessible by

others. This private key will be ignored. bad permissions: ignore

key: /Users/cvn/.ssh/chris-test.pem Permission denied (publickey).

So I ran

chmod 400 chris-test.pem

and the Keychain returned asking for a password that I do not have…

Best Answer

In my case private key was in openssh format (line -----BEGIN OPENSSH PRIVATE KEY----- in key file to check), but client ssh works only with rsa format (-----BEGIN RSA PRIVATE KEY----- line). I'd just converted one to another and it works. See how to convert Openssh Private Key to RSA Private Key here.