Network

The ssh-keygen command

Generates an SSH key pair, so you can log in to servers without typing a password.

Syntax

ssh-keygen [-t tipo] [-b bits] [-f archivo] [-C comentario] [-N frase]Try it in the terminal →

Most useful options

-t tipoKey algorithm. ed25519 is the modern choice; rsa is for old compatibility
-b bitsKey size in bits, for rsa and ecdsa. ed25519 has a fixed length and ignores it
-f archivoPath where the private key is saved. The public key is created next to it, with .pub
-C comentarioText that identifies the key, for example your email or your machine name
-N frasePassphrase that protects the private key. With -N "" there is no passphrase, which only makes sense for tests
-pChanges the passphrase of an existing key without regenerating it
-yPrints the public key from the private one, in case you lost the .pub
-lShows the fingerprint of a public key

Copy-ready examples

$ ssh-keygen -t ed25519creates the key in ~/.ssh and asks for the path and the passphrase.
$ ssh-keygen -t ed25519 -C "miguel@portatil" -f ~/.ssh/id_servidorcreates a key with its own name and a comment that identifies it.
$ ssh-keygen -y -f ~/.ssh/id_servidor > recuperada.pubrecovers the public key from the private one and saves it to a new file.
$ ssh-keygen -l -f ~/.ssh/id_servidor.pubshows the key fingerprint, useful to compare with the one on the server.
$ ssh-keygen -p -f ~/.ssh/id_servidorchanges the passphrase of the private key.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
unknown key type dsaThe key type you asked for is not valid in this version of ssh-keygen.Use -t ed25519 or -t rsa.
noexiste.pub: No such file or directoryThe file you pass with -f or at the end of the command does not exist or is misspelled.List the folder with ls ~/.ssh and use the exact name.

Typical mistakes

  • The private key (no .pub) is never shared with anyone.
  • Without a passphrase, whoever steals the private file can use it as it is.
  • If you pick a name that already exists and confirm the overwrite, you lose the old key: make a copy first.

Differences from similar commands

  • ssh: connects to the server using the key. ssh-keygen only creates it.
  • ssh-copy-id: installs the public key on the server, so you can log in without a password.
  • scp: copies files between machines, and can use the key ssh-keygen created.

Security

Anyone with the private file can log in to the servers that accept it. Set a passphrase and do not copy it off your machine; only the public (.pub) file is shared.

Related commands

FAQ

ed25519 or RSA?

ed25519: shorter and faster. Use RSA only if an old system accepts nothing else.

What is the passphrase?

A password that encrypts the private key on disk. It is asked each time you use the key, unless an SSH agent has it loaded.

Can I use the same key on several servers?

Yes, but one key per machine is better: if you lose it, only that machine is affected.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course