109

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?

Shafique Jamal
  • 1,550
  • 3
  • 21
  • 45
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245

2 Answers2

200

Copy the public key to clipboard.

Linux

ssh-keygen -f private.pem -y | xclip

MacOS

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

Save to file

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

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 
JazzCat
  • 4,243
  • 1
  • 26
  • 40
  • 6
    On Mac, "ssh-keygen -f Private.pem -y|pbcopy" works well. – Grant Johnson Oct 03 '19 at 19:26
  • 1
    Not sure why the different Linux and macOS commands are given. If you want the public key in a file, the "Linux" command is pretty much universal. If you want the command on a clipboard, then the macOS command is correct for macOS, and there are various Linux equivalents depending on what desktop environment you are using. – chepner Feb 22 '22 at 21:54
  • There was an edit that added that, good point changed the answer. – JazzCat Feb 23 '22 at 22:06
7

JazzCat's answer works. small addition: if your permissions are vague on .pem file, ssh-keygen will generate empty .pub file.

if you see any complains on terminal about private key too open, try to narrow it using chmod 400 private.pem and retry above command.

ps: sorry I don't have permissions to add a comment instead of answer.

Lilanga
  • 101
  • 1
  • 4