0

i'm having some difficulty trying to generate public key from private key , from the command line, where keys were made with openssh

(I know it's probably bad form to be root for this, but anyhow)

There are three private key files here. hp_blah_id_rsa, id_rsa, rodney

root@droplet-50-1:~/.ssh# ls -l
total 20
-rw------- 1 root root 1512 May 29 00:14 authorized_keys
-rw------- 1 root root 2602 May 28 23:15 hp_blah_id_rsa
-rw------- 1 root root 2602 May 28 23:28 id_rsa
-rw-r--r-- 1 root root  222 May 28 21:47 known_hosts
-rw------- 1 root root 2602 May 28 21:17 rodney

as you see there's no .pub files there.

here the file command confirms they are private keys without me having to cat them!

root@droplet-50-1:~/.ssh# file hp_blah_id_rsa 
hp_blah_id_rsa: OpenSSH private key

root@droplet-50-1:~/.ssh# file rodney
rodney: OpenSSH private key

root@droplet-50-1:~/.ssh# file id_rsa
id_rsa: OpenSSH private key

I would like the public keys that can be derived from those private keys, in .pub files.

I saw this question https://stackoverflow.com/questions/5244129/use-rsa-private-key-to-generate-public-key

and it mentions openssl rsa -in mykey.pem -pubout > mykey.pub

But that doesn't work for me e.g.

root@droplet-50-1:~/.ssh# openssl rsa -in id_rsa -pubout -out a.a
unable to load Private Key
140565827753280:error:0909006C:PEM routines:get_name:no start line:../crypto/pem/pem_lib.c:745:Expecting: ANY PRIVATE KEY
1
  • OpenSSH keys and OpenSSL keys are not the same and are not cross-compatible.
    – JW0914
    May 30 at 11:57

1 Answer 1

2

supposing the private key is id_rsa then ssh-keygen -y -f id_rsa will output the public key so you can do ssh-keygen -y -f id_rsa >id_rsa.pub

5
  • 1
    from DT's comment on SO "This is not programming or development, but (1) the privatekey files created by OpenSSH 7.8 up (which is 7 years later than the very old Q .. linked{in the posted question}) cannot be read by OpenSSL and (2) when you can use OpenSSL the kind of publickey it creates is not the type created by OpenSSH and needed if you want to use it in OpenSSH. OTOH if you just kept reading, the Q ..linked{in the posted question} DOES give the correct way to create an OpenSSH publickey from an OpenSSH privatekey: ssh-keygen -y"
    – barlop
    May 29 at 10:11
  • Maybe will be better to add your comment as part of the answer because the answer is more or less just one command w/o explaining the reason behind. May 29 at 12:18
  • General FYI: ssh-keygen -y -f <key> will output a public key without a comment
    – JW0914
    May 30 at 12:01
  • @RomeoNinov the comment just gives some historical point about OpenSSL(which is not the command in my answer). .So, the comment doesn't explain whatever you think "the reasoning behind" the command would be, whatever your phrase means anyhow.
    – barlop
    May 30 at 19:07
  • @JW0914 You mention that the command will output without a comment. What "comment" would anybody expect ssh-keygen -y -f file, to output?!?!
    – barlop
    May 30 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.