Programster's Blog

Tutorials focusing on Linux, programming, and open-source

SSH Key Cheatsheet

Related Posts

Table of Contents

General

For the cheats below, you may replace $KEY_PATH with ~/.ssh/id_rsa if you are concerned with just your own computers key rather than a key file.

Create Key

ssh-keygen

PEM Format

If you want to create a PEM key, instead of an OpenSSH key, then do the following:

ssh-keygen -t rsa -m PEM

You need to manually add the .pem extension to the generated private key file.

Add Passphrase

If you were given an unencrypted SSH key, such as from AWS, then you probably want to add a
passphrase to it with the following command:

ssh-keygen -p -f my-private-key.pem

Remove Passphrase

Just do the same again as adding passphrase, this time enter the current passphrase and then don't enter anything for the new one.

ssh-keygen -p -f my-private-key.pem

Generate public key from private key

ssh-keygen -f $KEY_PATH -y > $KEY_PATH.pub

Add Key To Remote Server

Refer here if you wish to see how to easily add this key to a remote server so that you can use it to log in.

Get Key Fingerprint

If you wish to get the fingerprint of a (public) key then do the following:

ssh-keygen -l -E MD5 -f /etc/ssh/ssh_host_rsa_key.pub

Remove the -E MD5 if you want the fingerprint in SHA256 format instead of MD5.

Public Host Key

If you are interested in your server's public fingerprint that it will identify itself to others with (e.g. when they try to connect via SSH), then you want to use your host key at /etc/ssh/ssh_host_rsa_key.pub. E.g.

ssh-keygen -l -E MD5 -f /etc/ssh/ssh_host_rsa_key.pub

Conversions

Convert PEM To OpenSSH

The following command will convert the PEM key file in place to the OpenSSH format, so be sure to make a copy if you want to keep the original format too.

ssh-keygen \
  -f $INPUT_FILE \
  -i \
  -mPEM \
  > $OUTPUT_FILE
  • The -f stands for filename, and allows us to specify the input file. This input file can be either a public or private key, and will generate the corresponding public/private output key.
  • The -mPEM tells the tool that the input file is expected to be in the PEM format. Some users may find that they need to specify PKCS8 instead.
  • The -i option tells the tool to read an unencrypted private (or public) key file in the format specified by the -m option and print an OpenSSH compatible priā€vate (or public) key to stdout.

Convert OpenSSH To PEM

The following command will create a new PEM key from an inputted OpenSSH key.

ssh-keygen \
  -f $INPUT_FILE \
  -m 'PEM' \
  -e \
  > output.pem
  • The -f stands for filename, and allows us to specify the input file. This input file can be either a public or private OpenSSH key, and will generate the corresponding public/private output key.
  • The -m PEM tells the tool to output in the PEM format.
  • The -e tells the tool to output to stdout, rather than to the file.

References

Last updated: 1st September 2022
First published: 16th August 2018