Skip to main content

ssh-keygen: Create and Manage SSH Keys on Linux

The ssh-keygen command creates the key pairs that let you log in to a remote computer without typing its password. It is part of OpenSSH, and in the OpenBSD manual page dated October 30, 2026, Ed25519 is the default key type. This guide shows how to create a key, put it on a server and look after it.

What it does

Many people manage a second computer from a distance. It might be a web server, a home server or a tiny computer in a cupboard. SSH (Secure Shell) is the standard way to log in to such a machine over a network. Typing a password every time is annoying and slow.

The ssh-keygen tool offers another way. It creates two matching files, called a key pair. One file is private and never leaves your computer. The other file is public, and you copy it to the server.

Think of a padlock and its key. The public key is like a padlock that you hang on the server's door. Anyone may see the padlock, and copying it does not help them open the door. The private key is the only key that fits. When you connect, the server checks that you hold that key, and it lets you in.

The comparison has one limit. A metal key takes effort to copy, but a key file can be copied in seconds by anyone who gets into your computer. That is why you can protect it with a passphrase, a secret sentence that unlocks the file.

If you only use a desktop or laptop and never connect to other machines, you can ignore this tool. If you log in to a server, create a key pair once and copy the public file to each server you use.

Back to top

How to use it

The tool is part of OpenSSH, so the commands below are the same on every distribution that ships it, from Debian and Ubuntu to Fedora, Arch and openSUSE.

Create a key

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

This creates an Ed25519 key pair with a comment, then asks where to save it and which passphrase to use.

Press Enter at the file question to accept the default, ~/.ssh/id_ed25519. The public half goes to ~/.ssh/id_ed25519.pub.

ssh-keygen -t rsa -b 4096 -C "user@example.com"

This creates a 4096-bit RSA key, an option for older systems.

Put the key on a server

ssh-copy-id user@example.com

This copies your default public key to the server and adds it to ~/.ssh/authorized_keys there, unless it is already present.

ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 user@example.com

This installs one specific key on a server that listens on port 2222.

ssh-copy-id -n user@example.com

This is a dry run: it shows what would happen without changing anything on the server.

Look after your keys

ssh-keygen -l -f ~/.ssh/id_ed25519.pub

This prints the fingerprint of the key, a short value that identifies it.

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

This changes the passphrase of your private key.

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

This reads the private key and prints the matching public key, which helps if you lost the .pub file.

ssh-keygen -R example.com

This removes every stored entry for a host from known_hosts, for example after that server got a new host key.

Warning: This rewrites your private key file in place. Make a backup copy of the key before you run it.

ssh-keygen -f ~/.ssh/id_rsa -p -m PEM

This converts an RSA private key file to the PEM format.

For most people, the best choice is a default Ed25519 key with a passphrase, installed with ssh-copy-id.

Back to top

How it works

Key types and sizes

The -t option picks the algorithm. The current OpenBSD page lists ecdsa, ecdsa-sk, ed25519, ed25519-sk, mldsa44-ed25519 and rsa. Without -t, the tool creates Ed25519.

Key length depends on the type. RSA keys default to 3072 bits, and 1024 bits is the minimum. For ECDSA, -b accepts only 256, 384 or 521. Ed25519 and the two security key types have a fixed length, so -b has no effect for them.

Each type has its own default file name, such as id_ed25519, id_rsa or id_ecdsa in ~/.ssh. The public key uses the same name with .pub added. The -f option sets a different file name.

Passphrases and the KDF

A passphrase encrypts the private key file. The tool runs it through a key derivation function (KDF), which turns the passphrase into the encryption key. The -a option sets the number of KDF rounds, with 32 as the default. More rounds slow down each guess, so a brute-force attack on a stolen file takes longer. The -Z option picks the cipher, and aes256-ctr is the default there.

You can also give the passphrase directly on the command line with -N. Later, -p changes it and -c changes the comment of an existing key. Without -C, the comment is user@host.

Permissions and authorized_keys

The private key file must be readable only by its owner. On the server side, sshd is just as strict. If authorized_keys, ~/.ssh or the home directory is writable by other users, sshd refuses to use the file. The only exception is when StrictModes is set to "no".

Each line in authorized_keys holds one key: optional options, the key type, the base64-encoded key and a comment. Lines that start with # are ignored. By default, sshd also reads authorized_keys2, and it enforces a minimum RSA size of 1024 bits.

The ssh-copy-id tool creates the directory and the file when they are missing. Some servers do not allow remote commands. For those, -s makes it edit the file over SFTP instead. The -t option sends the key to a different target path.

known_hosts and fingerprints

Your client stores the host keys of servers it knows in known_hosts. Besides -R, you can search that file with -F hostname. The -H option hashes the host names, so someone who reads the file cannot see which servers you use. Entries that are already hashed stay as they are, and the original file is kept with a .old suffix.

Fingerprints use SHA256 by default. Add -E md5 for the older MD5 format. Combine -l with -v to get a small ASCII art picture of the key.

Hardware security keys

The ecdsa-sk and ed25519-sk types work with FIDO hardware tokens. Here, the private file holds only a key handle. The real private key stays inside the device and cannot be exported. Options such as resident, verify-required and no-touch-required control how the token behaves. An application value must begin with "ssh:". The -K option downloads resident keys from an authenticator.

Still unclear

  • The mldsa44-ed25519 type appears in the current OpenBSD page, but the OpenSSH versions that distributions ship may not support it yet.
  • The manual page gives no numeric file modes, only the rule that the owner alone may access the files.
Back to top

Sources

  1. ssh-keygen(1) manual — OpenBSD man pages
  2. ssh-copy-id(1) — Debian manpages
  3. sshd(8) manual — OpenBSD man pages
  4. tldr ssh-keygen page — tldr-pages
Back to top
Peter Martin
Linux admin, PHP dev and Joomla specialist

Open-source enthusiast since 2005 and co-founder of Linux Nijmegen (Netherlands), passionate about solving technical challenges and sharing knowledge.

Frequently asked questions

The ssh-keygen command creates key pairs. These pairs allow you to log into a remote computer without needing to type a password repeatedly. It’s part of OpenSSH and uses Ed25519 as the default key type.

The public key is like a padlock that you place on the server's door. Anyone can see the padlock, but it doesn’t allow them to open the door. The server uses this padlock to verify your private key.

If you don’t use a passphrase, anyone who gains access to your private key file can log in to the server. It’s important to protect your key with a secret sentence to prevent unauthorized access.