How to extract public key using OpenSSL?


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?


This question is tagged with openssl public-key-encryption pki

~ Asked on 2012-04-22 19:12:57

The Best Answer is


openssl rsa -in privkey.pem -pubout >

That writes the public key to

~ Answered on 2012-04-22 19:19:02


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

edit Thanks @makenova for the complete line:

ssh-keygen -y -f key.pem >

~ Answered on 2014-05-01 17:36:29

Most Viewed Questions: