84

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?

2
  • 6
    The private key is already in PEM format and can be used as-is. Jul 16, 2015 at 18:59
  • 2
    @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, 2015 at 20:16

6 Answers 6

90

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.

11
  • 1
    But they don't need the public key do they? They need the private key for decrypting their password from the AWS Console Jul 16, 2015 at 18:40
  • 1
    There's also good information on this here: unix.stackexchange.com/questions/26924/…
    – austinian
    Jul 16, 2015 at 20:17
  • 1
    This doesn't work on OS X (ssh -v ==> OpenSSH_6.2p2)
    – chrish
    Mar 3, 2016 at 20:50
  • 5
    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, 2018 at 8:44
  • 3
    id_rsa contains the keypair, not just the private key.
    – fuero
    Jul 30, 2020 at 7:41
50

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.pub.pem

Then simply copy the .pem key as necessary.

Options as follows: (See man ssh-keygen)

  • -f id_rsa.pub: input file
  • -m 'PEM': output format PEM
  • -e: output to STDOUT
1
  • For me, 'pkcs8' key_format works to verify signature as follows: "ssh-keygen ... -m pkcs8 ..." Mar 30, 2020 at 23:44
17

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

2
  • 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, 2019 at 19:51
  • @nclard, Not sure why I left that out -b parameter. Just added it. Oct 24, 2019 at 4:03
8

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
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.

0

Add your public key on your server to authorized_keys

cat .ssh/id_rsa.pub >> .ssh/authorized_keys

or you can add it manually using your text editor in your server.

Copy your private key in your server

cp .ssh/id_rsa /home/your_user/your_key.pem

Now in your client PC, download a key from server

scp your_user@yourhostserver.com:/home/your_user/your_key.pem /home/your_local_user/Downloads/
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 20 at 9:35

Your Answer

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