337 Words

ssh 150x150

Introduction

A short best-practice guide on how to generate an SSH key pair on the Linux command line.

A public/private key pair is a pair of mathematically related keys used in asymmetric cryptography for authentication, digital signatures or key establishment. As the name indicates, the private key is used by the owner of the key pair, is kept secret and should be protected at all times, while the public key can be published and used by the other party to complete the protocol or to verify the operations performed with the private key.

NEVER SHARE YOUR PRIVATE KEY!

Preferred key type: Ed25519

I prefer the Ed25519 key type. It was added to OpenSSH in 2014 and is supported by all current OpenSSH versions. Some old clients, servers and network devices don’t support it yet, so check their documentation if you run into problems.

Generate a key pair by running this command in a Linux/Unix terminal. It creates two files in your ~/.ssh folder (id_ed25519 and id_ed25519.pub):

ssh-keygen -t ed25519 -a 100 -C my@email.tld
  • -a The number of KDF rounds (Key Derivation Function: English / Dutch). Higher numbers make passphrase verification slower, which increases resistance to brute-force password cracking if the private key is stolen. Use at least 100 rounds.
  • -t The type of key to create, in our case Ed25519.
  • -f The filename of the generated key file. If you want SSH to find it automatically, store it in the default .ssh directory in your home directory.
  • -C A comment. It’s purely informational and can be anything, but it’s usually <login>@<hostname> of whoever generated the key.

Here are IBM’s ssh-keygen instructions.

Next steps

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server   # put the public key on a server
ssh-add ~/.ssh/id_ed25519                          # type the passphrase once per session
ssh-keygen -p -f ~/.ssh/id_ed25519                 # add or change the passphrase later

Always give your key a passphrase: if someone copies the file, they still can’t use it. Even safer is a key that lives on a hardware key, see YubiKey: SSH with FIDO2.