145

The following command generates a file which contains both public and private key:

openssl genrsa -des3 -out privkey.pem 2048

Source: here

With OpenSSL, the private key contains the public key information as well, so a public key doesn't need to be generated separately

How can we extract the public key from the privkey.pem file?

Thanks.

jww
  • 90,984
  • 81
  • 374
  • 818
Jake
  • 15,539
  • 46
  • 120
  • 195
  • 1
    @anish People should NOT be encouraged to paste private keys into random web forms. That's hugely disconcerting from a security perspective, and given you built that "tool" it's also self-promotion. Please remove your comment. – aendra Oct 20 '20 at 14:31

6 Answers6

215
openssl rsa -in privkey.pem -pubout > key.pub

That writes the public key to key.pub

stewe
  • 40,424
  • 13
  • 77
  • 74
  • 35
    Always is better use the internal option to do this: `-out`, for example: `openssl rsa -in privkey.pem -pubout -out key.pub` instead of redirect stdout to a file. – Juan Antonio Nov 09 '16 at 09:03
  • 2
    @JuanAntonio would it be possible for you to explain why it is better to use -out rather than redirect? Many Thanks – Banoona Mar 01 '22 at 10:30
149

Though, the above technique works for the general case, it didn't work on Amazon Web Services (AWS) PEM files.

I did find in the AWS docs the following command works: ssh-keygen -y

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html

edit Thanks @makenova for the complete line:

ssh-keygen -y -f key.pem > key.pub
lababidi
  • 2,434
  • 1
  • 19
  • 14
  • 38
    Thanks. This is want I needed. To skip the prompts, you can use `ssh-keygen -y -f key.pem > key.pub` – makenova May 19 '15 at 22:56
  • 6
    This is the correct answer `ssh-keygen -y -f key.pem` – Justin Jun 10 '16 at 16:47
  • 1
    this is asking me for a passphrase, but I didn't put any passphrase – kavain Mar 23 '17 at 03:35
  • 2
    @makenova This will **regenerate** the key in `key.pem`, which could prevent you from logging into instances that require that key! – SubmittedDenied May 01 '17 at 17:42
  • If you got the same problem as @kavain where it asks you for the passphrase you didn't put, and you're using your key with `ssh -i`, make sure you're [linking to your private key there, **not** the public one](https://serverfault.com/a/267994/91532) – mehov Aug 12 '17 at 11:36
  • Can anyone elaborate why AWS is picky about the "correct answer" above? – GreenLake4964 Dec 05 '19 at 09:33
12

For those interested in the details - you can see what's inside the public key file (generated as explained above), by doing this:-

openssl rsa -noout -text -inform PEM -in key.pub -pubin

or for the private key file, this:-

openssl rsa -noout -text -in key.private

which outputs as text on the console the actual components of the key (modulus, exponents, primes, ...)

cnd
  • 1,637
  • 16
  • 14
4

For AWS importing an existing public key,

  1. Export from the .pem doing this... (on linux)

    openssl rsa -in ./AWSGeneratedKey.pem -pubout -out PublicKey.pub
    

This will produce a file which if you open in a text editor looking something like this...

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn/8y3uYCQxSXZ58OYceG
A4uPdGHZXDYOQR11xcHTrH13jJEzdkYZG8irtyG+m3Jb6f9F8WkmTZxl+4YtkJdN
9WyrKhxq4Vbt42BthadX3Ty/pKkJ81Qn8KjxWoL+SMaCGFzRlfWsFju9Q5C7+aTj
eEKyFujH5bUTGX87nULRfg67tmtxBlT8WWWtFe2O/wedBTGGQxXMpwh4ObjLl3Qh
bfwxlBbh2N4471TyrErv04lbNecGaQqYxGrY8Ot3l2V2fXCzghAQg26Hc4dR2wyA
PPgWq78db+gU3QsePeo2Ki5sonkcyQQQlCkL35Asbv8khvk90gist4kijPnVBCuv
cwIDAQAB
-----END PUBLIC KEY-----
  1. However AWS will NOT accept this file.

    You have to strip off the -----BEGIN PUBLIC KEY----- and -----END PUBLIC KEY----- from the file. Save it and import and it should work in AWS.

Stephen Rauch
  • 44,696
  • 30
  • 102
  • 125
Bendo
  • 41
  • 3
2

If your looking how to copy an Amazon AWS .pem keypair into a different region do the following:

openssl rsa -in .ssh/amazon-aws.pem -pubout > .ssh/amazon-aws.pub

Then

aws ec2 import-key-pair --key-name amazon-aws --public-key-material '$(cat .ssh/amazon-aws.pub)' --region us-west-2
Justin
  • 38,686
  • 72
  • 185
  • 276
  • 2
    The public key output by `openssl` is sandwiched in PEM headers, which you will have to remove before AWS CLI accepts the key. – jpsecher Apr 22 '16 at 09:49
0

use openssl to extract the pub file from the pem file as

openssl x509 -inform pem -in private_key.pem -pubkey -noout > public_key.pub
Arvind
  • 43
  • 4