Generate public SSH key from existing private SSH key

1 min read

To be able to successfully connect using SSH keys we need to have the private key on the client side and the public key set on the server side (where we want to connect). If we only have the private key, we can generate a public SSH key from it

Assuming we have the private key as private_key.pem we just need to use ssh-keygen the following flags:

  • option -y: To instruct ssh-keygen to generate a the OpenSSH public key to stdout
  • option -f: To specify the key's filename

So the command would look like this:

$ ssh-keygen -yf private_key.pem
ssh-rsa AAAAQABAAABA...

The public will be print to stdout, so if we want to write it to a file we can just redirect it's output like this:

$ ssh-keygen -yf private_key.pem > public_key.pem

Posted on 24/12/2021

Categories