如何获取 pem 文件的公钥?

我有一个包含我的私钥的 .pem文件,但是 BitBucket 部署密钥的格式如下:

ABAABAQDfZxX2LXOJlo5MP2tLP4fmQyjIAcATwATFKwM6K3mtT7 + LKx1jk6YlFlEcj2CFxJHHTy6LCdDqoVzL3iNcD + mDl7NbcNEHytZNJFQ5lAHPxDVa9ZbLnmP1OlfUvsQS + jAt7yMSwd8gZ6McOJfp9Zun + r5LLpjYkF + AMQFPsf + 6 lhSJaOjOTbsA39OJwlnnSO6HF3W2Om + 8 Bgdpa/E4En5RZTBwFCAvLaaXY3XgN76xCR24TwTWFicBHWeLDdADGFXB7OBOv4y805fNGbNKOl3Yb21mG89aUQlYjobeLqImyEhX36hEdMW/1I0QC///uLa + GjJouuue4WY3 Ubuntu@Box

它通常使用:

cat ~/.ssh/id_rsa.pub | pbcopy

如何以这种格式从 .pem文件提取公钥?

84717 次浏览

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