Generate

Introduction

ssh-keygen creates SSH key pairs and manages comments, passphrases, and host keys. This sheet covers key types, fingerprints, and known_hosts maintenance.

Create a key

ssh-keygen -t ed25519 -C "me@example.com"

Writes ~/.ssh/id_ed25519 and its .pub file. Set a passphrase when prompted.

See: ssh-keygen(1)

Key types

ssh-keygen -t ed25519
ssh-keygen -t rsa -b 4096
ssh-keygen -t ecdsa -b 521
ssh-keygen -t ed25519-sk

Ed25519 is the usual default. RSA defaults to 3072 bits; add -b 4096 for a larger key. -sk keys live on a security key.

See: ssh-keygen(1)

Custom filename

ssh-keygen -t ed25519 \
  -f ~/.ssh/id_work \
  -C "work laptop"

-f writes the private key and its .pub sibling.

See: ssh-keygen(1)

Inspect

Fingerprint

ssh-keygen -lf ~/.ssh/id_ed25519.pub

Prints the bit length, SHA256 fingerprint, comment, and algorithm.

See: ssh-keygen(1)

Public key from private

ssh-keygen -y -f ~/.ssh/id_ed25519

Recreates the public key from the private one; copy it into authorized_keys.

See: ssh-keygen(1)

MD5 fingerprint

ssh-keygen -l -E md5 \
  -f ~/.ssh/id_ed25519.pub

-E md5 prints the older colon-separated digest; SHA256 is the default.

See: ssh-keygen(1)

Manage

Change passphrase

ssh-keygen -p -f ~/.ssh/id_ed25519

Prompts for the old and new passphrases, then re-encrypts the key in place.

See: ssh-keygen(1)

Remove passphrase

ssh-keygen -p -P oldpass \
  -N '' -f ~/.ssh/id_ed25519

-P and -N supply passphrases without prompts; an empty -N removes one.

See: ssh-keygen(1)

Change comment

ssh-keygen -c -C "work laptop" \
  -f ~/.ssh/id_ed25519

Changes only the label printed with the public key, not the key itself.

See: ssh-keygen(1)

Host keys

Find an entry

ssh-keygen -F github.com \
  -f ~/.ssh/known_hosts

Prints matching host keys with their line numbers.

See: ssh-keygen(1)

Remove an entry

ssh-keygen -R github.com

Removes the host key and saves the previous file as .old. Use [host]:2222 for a non-default port.

See: ssh-keygen(1)

Hash hostnames

ssh-keygen -H -f ~/.ssh/known_hosts

Replaces hostnames and addresses with hashes; delete the .old copy once it is verified.

See: ssh-keygen(1)

Check a remote key

ssh-keyscan github.com | ssh-keygen -lf -

Compare the fingerprint with the host's published value before accepting it.

See: ssh-keyscan(1)

Also see

0 Comments for this cheatsheet. Write yours!