51

I created a key pair using ssh-keygen and get the two clasic id_rsa and id_rsa.pub.

I imported the public key into my AWS EC2 account.

Now I created a windows instance and to decrypt that instance password, AWS console is asking me for a .pem file. How I can get that .pem file from my two id_rsa and id_rsa.pub files?

  • 3
    The private key is already in PEM format and can be used as-is. – Michael Hampton Jul 16 '15 at 18:59
  • @MichaelHampton, this will depend on the version of ssh-keygen and the command used to generate the key pair. Some versions use RFC4716 by default, instead of PEM. – austinian Jul 16 '15 at 20:16
61

According to this, this command can be used:

ssh-keygen -f id_rsa -e -m pem

This will convert your public key to an OpenSSL compatible format. Your private key is already in PEM format and can be used as is (as Michael Hampton stated).

Double check if AWS isn't asking for a (X.509) certificate in PEM format, which would be a different thing than your SSH keys.

| improve this answer | |
  • But they don't need the public key do they? They need the private key for decrypting their password from the AWS Console – Michael Bailey Jul 16 '15 at 18:40
  • I have no idea what OP needs this for. – fuero Jul 16 '15 at 18:41
  • Not sure how familiarized you are with AWS, but when you spin up a windows instance (basically server) AWS keeps the password from you until you give them your private key. Then they give you the server password. That way Windows and Linux servers on AWS both depend on private keys. – Michael Bailey Jul 16 '15 at 18:43
  • 1
    This doesn't work on OS X (ssh -v ==> OpenSSH_6.2p2) – chrish Mar 3 '16 at 20:50
  • 4
    contrary to the documentation, it will output PUBLIC key in PEM format, not the private one. currently ssh-keygen does not support converting private keys. – morgwai Aug 10 '18 at 8:44
33

Using ssh-keygen to export the key in the .pem format worked for me.

ssh-keygen -f id_rsa.pub -m 'PEM' -e > id_rsa.pem

Then simply copy the .pem key as necessary.

For reference:

  • the -f id_rsa.pub portion indicates the input file to read from
  • -m 'PEM indicates a PEM filetype
  • the -e option indicates that the output will be exported
| improve this answer | |
  • For me, 'pkcs8' key_format works to verify signature as follows: "ssh-keygen ... -m pkcs8 ..." – Fumisky Wells Mar 30 at 23:44
7

id_rsa is the file that you have to use to decrypt the Windows EC2 instance password, but just make sure that the file you copy paste is not phrase protected.

I solved the problem getting a temporarily unprotected the id_rsa file with something like:

$ openssl rsa -in ~/.ssh/id_rsa -out tmp_file.pem
| improve this answer | |
5

Initially, when using ssh-keygen, I could generate a public key that was compatible with AWS EC2, but had issues with creating private keys that were compatible. The following creates both public and private keys pairs that are compatible with AWS EC2.

ssh-keygen -P "" -t rsa -b 4096 -m pem -f my-key-pair

Here's info on each parameter:

  • -P: is for passphrase. Intentionally set to empty.
  • -t: Specifies the type of key to create.  AWS EC2 Key Pair requires RSA. It's my experience that this pertains to the public key that is created.
  • -b: Specifies the number of bits in the key. The supported lengths are 1024, 2048, and 4096. If you connect using SSH while using the EC2 Instance Connect API, the supported lengths are 2048 and 4096.
  • -m: Specifies a key format for key generation. Setting a format of “PEM” when generating a supported private key type will cause the key to be stored in the legacy PEM private key format.  AWS EC2 Key Pair need the legacy format
  • -f: Specifies the output filename of the key file

Resources:

For more information on ssh-keygen, see: https://man.openbsd.org/ssh-keygen.1

AWS - EC2 Key Pairs - https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html

| improve this answer | |
  • Mike has chosen not to include documentation for the '-b' option. Perhaps obvious to some, that means "4096-bit encryption please". As stated in the linked AWS doc, "The supported lengths are 1024, 2048, and 4096. If you connect using SSH while using the EC2 Instance Connect API, the supported lengths are 2048 and 4096." – nclark Oct 23 '19 at 19:51
  • @nclard, Not sure why I left that out -b parameter. Just added it. – Mike Barlow - BarDev Oct 24 '19 at 4:03
0

When you launch an EC2 instance, you assign to it a key pair (or none). It cannot be changed afterwards.

Only using that .pem file from that key pair will you be able to decrypt the Windows password.

The .pem file would have been downloaded when the key pair was created. You cannot get it again. If you have lost it, you're out of luck.

You cannot use a .pem file that you generated yourself unless you imported that key into AWS before the instance was launched and assigned it to the instance.

Simply put, if you don't have the original .pem file, you cannot get the password.

Edit: after re-reading the question, I realize the OP had imported his key into AWS.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy