OpenSSH Key Generation

How to generate OpenSSH keys on Linux.

This guide will show you how to generate OpenSSH keys on Linux.

Prerequisites

Make sure you have the openssh-server package installed.

1
2
sudo apt-get update
sudo apt-get install openssh-server

Generate OpenSSH Key

To generate an OpenSSH key:

1
2
# generate an OpenSSH key
ssh-keygen -t rsa

This will prompt you for a passphrase (you can leave it blank if you don’t want to use one), ask you for the location of the key file, and then generate a private key (default id_rsa) and a public key file (default id_rsa.pub).

Generate OpenSSH Key of a specific length

-b flag specifies the number of bits in the key to create. For RSA keys, the minimum size is 1024 bits and the default is 3072 bits. Generally, 3072 bits is considered sufficient. For ECDSA keys, the -b flag determines the key length by selecting from one of three elliptic curve sizes: 256, 384 or 521 bits. Attempting to use bit lengths other than these three values for ECDSA keys will fail. ECDSA-SK, Ed25519 and Ed25519-SK keys have a fixed length and the -b flag will be ignored.

To generate an OpenSSH key of a specific length:

1
2
# generate an OpenSSH key with a specific length
ssh-keygen -t rsa -b 4096

This will generate a private key file id_rsa and a public key file id_rsa.pub.