How do I get the public key of a pem file?

sdgfsdh picture sdgfsdh · Jul 15, 2016 · Viewed 39.9k times · Source

I have a .pem file containing my private key. However, a BitBucket deployment key has this format:

ssh-rsa AAAAB3NzaC3yc2EAAAADAQABAAABAQDfZxX2LXOJlo5MP2tLP4fmQyjIAcATwATFKwM6K3mtT7+LKx1jk6YlFlEcj2CFxJHHTy6LCdDqoVzL3iNcD+mDl7NbcNEHytZNJnFQ5lAHPxDVa9ZbLnmP1OlfUvsQS+jAt7yMSwd8gZ6McOJfp9ZUn+r5LLpjYkF+AMQFPsf+6lhSJaOjOTbsA39OJwlnnSO6HF3W2Om+8Bgdpa/E4En5RZTBwFCAvLaaXY3XgN76xCR24TwTWFicBHWeLdADGFXB7OBOv4y805fNGbNKOl3Yb21mG89aUQlYjobeLqImyIrrEhX36hEdMW/t6zZK/1I0QC//uLa+GjJoeuPW4WY3 ubuntu@Box

It is usually found using:

cat ~/.ssh/id_rsa.pub | pbcopy

How do I extract my public key, in this format, from a .pem file?

Answer

JazzCat picture JazzCat · Jul 15, 2016

On Linux:

ssh-keygen -f private.pem -y > public.pub

On macOS:

ssh-keygen -f Private.pem -y | pbcopy

Note that if your permissions are vague on the .pem file, then ssh-keygen will generate an empty .pub file.

You can usually fix this with:

chmod 400 private.pem