Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Networking

Linux ssh-keygen command

Reference for ssh-keygen on Linux: generate Ed25519 and RSA key pairs, set passphrases, read fingerprints, manage known_hosts, and install keys safely.

ssh-keygen creates, inspects and converts the key pairs used by ssh. A pair has a private key, which stays secret on your machine, and a public key, a single line you copy to servers. A server that holds your public key can verify that you own the private key without any password crossing the network.

It also manages host keys and the known_hosts file, and it can sign certificates.

Terminal
ssh-keygen -t ed25519 -C "laptop 2026"
ssh-keygen -l -f ~/.ssh/id_ed25519.pub
ssh-keygen -R old.example.com

Try it

Key generation needs a user database entry that our sandbox does not provide, so the real commands are read-only. What you can run is a look inside a public key line: the middle field is Base64 of a structure that starts with the key type. We build a fake all-zero Ed25519 key to show it; it is not a usable key.

Bash Inside an ssh public key line

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Read-only: the commands you actually run.

CONSOLE
$ ssh-keygen -t ed25519 -C "laptop 2026"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/me/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Your identification has been saved in /home/me/.ssh/id_ed25519
Your public key has been saved in /home/me/.ssh/id_ed25519.pub
$ ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host

Syntax

Terminal
ssh-keygen [-t TYPE] [-b BITS] [-C COMMENT] [-f FILE] [-N PASSPHRASE]
ssh-keygen -l | -p | -y | -R HOST | -F HOST [-f FILE]
Option Description
-t TYPE Key type: ed25519 (recommended), rsa, ecdsa.
-b BITS Key size for RSA (use 3072 or 4096) and ECDSA.
-C TEXT Comment stored in the public key.
-f FILE Output file (the public key gets .pub added).
-N PASS Set the passphrase on the command line. -N "" means none.
-l Show the fingerprint of a key.
-E ALGORITHM Hash for fingerprints (sha256 or md5).
-y Print the public key for a private key.
-p Change the passphrase of a private key.
-R HOST Remove a host from known_hosts.
-F HOST Find a host in known_hosts.
-H Hash host names in known_hosts.
-q Quiet.
-a ROUNDS KDF rounds protecting a passphrase (slower guessing).

Returns: 0 on success; non-zero on failure (file exists and you declined, unreadable key, wrong passphrase).

Usage notes

  • Pick Ed25519. Keys are short, fast and secure. Use RSA 3072+ only for old systems that cannot read Ed25519. DSA is obsolete and disabled in current OpenSSH.
  • Use a passphrase so a stolen laptop or backup does not hand over server access. Pair it with ssh-agent so you type it once per session.
  • Never copy the private key to a server or into a repository. Only the .pub file is shared. Treat id_ed25519 like a password: mode 600.
  • -N on the command line lands in shell history and the process list; use it for throwaway automation keys only.
  • Installing the public key: ssh-copy-id appends it to ~/.ssh/authorized_keys on the server. Done by hand, make sure the directory is 700 and the file 600.
  • One key per purpose (laptop, CI, deploy) makes revoking a single key easy; the comment (-C) tells you which line to remove.
  • Fingerprints let you verify a host or key by eye: compare ssh-keygen -l output on both ends.
  • Host key changed warnings: after confirming the change is legitimate, ssh-keygen -R host deletes the stale line and the next connection records the new one.
  • Overwriting: it asks before replacing an existing key file; answering yes destroys the old private key permanently.