SSH-KEYGEN(1)

โœจ ssh-keygen โœจ

okay bestie, welcome to ssh-keygen โ€” that's the OpenSSH authentication key utility, and it lives in the General Commands Manual, section 1. it's giving the key-making tool. the one. periodt. ๐ŸŽ€


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

so here's the vibe โ€” ssh-keygen has a lot of modes, and each one is its own little synopsis line. copy these exactly, no cap:

ssh-keygen [-q] [-a rounds] [-b bits] [-C comment] [-f output_keyfile]
           [-m format] [-N new_passphrase] [-O option]
           [-t ecdsa | ecdsa-sk | ed25519 | ed25519-sk | mldsa44-ed25519 | rsa]
           [-w provider] [-Z cipher]
ssh-keygen -p [-a rounds] [-f keyfile] [-m format] [-N new_passphrase]
           [-P old_passphrase] [-Z cipher]
ssh-keygen -i [-f input_keyfile] [-m key_format]
ssh-keygen -e [-f input_keyfile] [-m key_format]
ssh-keygen -y [-f input_keyfile]
ssh-keygen -c [-a rounds] [-C comment] [-f keyfile] [-P passphrase]
ssh-keygen -l [-v] [-E fingerprint_hash] [-f input_keyfile]
ssh-keygen -B [-f input_keyfile]
ssh-keygen -D pkcs11
ssh-keygen -F hostname [-lv] [-f known_hosts_file]
ssh-keygen -H [-f known_hosts_file]
ssh-keygen -K [-a rounds] [-w provider]
ssh-keygen -R hostname [-f known_hosts_file]
ssh-keygen -r hostname [-g] [-f input_keyfile]
ssh-keygen -M generate [-O option] output_file
ssh-keygen -M screen [-f input_file] [-O option] output_file
ssh-keygen -I certificate_identity -s ca_key [-hU] [-D pkcs11_provider]
           [-n principals] [-O option] [-V validity_interval]
           [-z serial_number] file ...
ssh-keygen -L [-f input_keyfile]
ssh-keygen -A [-a rounds] [-f prefix_path]
ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]
           file ...
ssh-keygen -Q [-l] -f krl_file file ...
ssh-keygen -Y find-principals [-O option] -s signature_file -f
           allowed_signers_file
ssh-keygen -Y match-principals -I signer_identity -f
           allowed_signers_file
ssh-keygen -Y check-novalidate [-O option] -n namespace -s
           signature_file
ssh-keygen -Y sign [-O option] -f key_file -n namespace file ...
ssh-keygen -Y verify [-O option] -f allowed_signers_file -I
           signer_identity -n namespace -s signature_file
           [-r revocation_file]

๐ŸŒธ DESCRIPTION ๐ŸŒธ

so ssh-keygen generates, manages and converts authentication keys for ssh(1). and yes, it can create keys for use by SSH protocol version 2. iconic behavior honestly. ๐Ÿ’…

the type of key you wanna generate? that's the -t option's whole job. and if you just run it with no arguments at all, ssh-keygen will generate an Ed25519 key. default queen. โœจ

ssh-keygen is also used to generate groups for use in Diffie-Hellman group exchange (DH-GEX). go peek at the โ€œMODULI GENERATIONโ€ section for the deets. ๐Ÿฆ‹

and finally โ€” yes there's a finally โ€” ssh-keygen can generate and update Key Revocation Lists, and test whether given keys have been revoked by one. see the โ€œKEY REVOCATION LISTSโ€ section. it does it all fr fr.


okay so normally, every user who wants to use SSH with public key authentication runs this once to create the authentication key in:

  • ~/.ssh/id_ecdsa
  • ~/.ssh/id_ecdsa_sk
  • ~/.ssh/id_ed25519
  • ~/.ssh/id_ed25519_sk
  • ~/.ssh/id_mldsa44_ed25519
  • ~/.ssh/id_rsa

and on top of that, the system administrator may use this to generate host keys, as seen in /etc/rc. sysadmins stay busy. ๐Ÿ‘‘


so here's how the normal flow goes. this program generates the key and asks for a file in which to store the private key. the public key gets stored in a file with the same name but with โ€œ.pubโ€ appended. cute little duo. ๐Ÿซถ

then it also asks for a passphrase. the passphrase may be empty to indicate no passphrase โ€” and heads up, host keys must have an empty passphrase โ€” or it may be a string of arbitrary length.

a passphrase is similar to a password, except it can be a phrase with a series of words, punctuation, numbers, whitespace, or literally any string of characters you want. the way that a passphrase can be a whole sentence is so much better than a password ngl.

good passphrases are 10-30 characters long, are not simple sentences or otherwise easily guessable โ€” English prose has only 1-2 bits of entropy per character, and provides very bad passphrases โ€” and contain a mix of upper and lowercase letters, numbers, and non-alphanumeric characters. ๐Ÿ’…

and you can change the passphrase later by using the -p option. slay.

๐Ÿšจ there is no way to recover a lost passphrase. if the passphrase is lost or forgotten, a new key must be generated and the corresponding public key copied to other machines. so likeโ€ฆ write it down bestie. ๐Ÿ˜ญ

ssh-keygen will by default write keys in an OpenSSH-specific format. this format is preferred because it offers better protection for keys at rest and allows storage of key comments within the private key file itself. the key comment may be useful to help identify the key. the comment is initialized to user@host when the key is created, but can be changed using the -c option. so helpful honestly.

it is still possible for ssh-keygen to write the previously-used PEM format private keys using the -m flag. this may be used when generating new keys, and existing new-format keys may be converted using this option in conjunction with the -p (change passphrase) flag. iykyk.

after a key is generated, ssh-keygen will ask where the keys should be placed to be activated. โœจ

the options are as follows:


๐Ÿ’– OPTIONS ๐Ÿ’–

  • -A โ€” generate host keys of all default key types (rsa, ecdsa, mldsa44-ed25519 and ed25519) if they don't already exist. the host keys are generated with the default key file path, an empty passphrase, default bits for the key type, and default comment. if -f has also been specified, its argument is used as a prefix to the default path for the resulting host key files. this is used by /etc/rc to generate new host keys. ๐Ÿ 
  • -a rounds โ€” when saving a private key, this option specifies the number of KDF (key derivation function, currently bcrypt_pbkdf(3)) rounds used. higher numbers result in slower passphrase verification and increased resistance to brute-force password cracking (should the keys be stolen). the default is 32 rounds. ๐Ÿ”ฅ
  • -B โ€” show the bubblebabble digest of the specified private or public key file. bubblebabble. that's the actual name. iconic. ๐Ÿซง
  • -b bits โ€” specifies the number of bits in the key to create. for RSA keys, the minimum size is 1024 bits and the default is 3072 bits. generally, 3072 bits is considered sufficient. for ECDSA keys, the -b flag determines the key length by selecting from one of three elliptic curve sizes: 256, 384 or 521 bits. attempting to use bit lengths other than these three values for ECDSA keys will fail. ECDSA-SK, Ed25519 and Ed25519-SK keys have a fixed length and the -b flag will be ignored. ๐Ÿ’€
  • -C comment โ€” provides a new comment. ๐Ÿ’ฌ
  • -c โ€” requests changing the comment in the private and public key files. the program will prompt for the file containing the private keys, for the passphrase if the key has one, and for the new comment. glow up your key's little nametag. โœจ
  • -D pkcs11 โ€” download the public keys provided by the PKCS#11 shared library pkcs11. when used in combination with -s, this option indicates that a CA key resides in a PKCS#11 token (see the โ€œCERTIFICATESโ€ section for details). ๐Ÿ”
  • -E fingerprint_hash โ€” specifies the hash algorithm used when displaying key fingerprints. valid options are: โ€œmd5โ€ and โ€œsha256โ€. the default is โ€œsha256โ€. ๐Ÿงฌ
  • -e โ€” this option will read a private or public OpenSSH key file and print to stdout a public key in one of the formats specified by the -m option. the default export format is โ€œRFC4716โ€. this option allows exporting OpenSSH keys for use by other programs, including several commercial SSH implementations. take your key on tour bestie ๐Ÿงณ
  • -F hostname | [hostname]:port โ€” search for the specified hostname (with optional port number) in a known_hosts file, listing any occurrences found. this option is useful to find hashed host names or addresses and may also be used in conjunction with the -H option to print found keys in a hashed format. ๐Ÿ”
  • -f filename โ€” specifies the filename of the key file. the moment of truth. ๐Ÿ“
  • -g โ€” use generic DNS format when printing fingerprint resource records using the -r command. ๐ŸŒ
  • -H โ€” hash a known_hosts file. this replaces all hostnames and addresses with hashed representations within the specified file; the original content is moved to a file with a .old suffix. these hashes may be used normally by ssh and sshd, but they do not reveal identifying information should the file's contents be disclosed. this option will not modify existing hashed hostnames and is therefore safe to use on files that mix hashed and non-hashed names. so considerate honestly. ๐Ÿซถ
  • -h โ€” when signing a key, create a host certificate instead of a user certificate. see the โ€œCERTIFICATESโ€ section for details. ๐Ÿ‘‘
  • -I certificate_identity โ€” specify the key identity when signing a public key. see the โ€œCERTIFICATESโ€ section for details. ๐Ÿชช
  • -i โ€” this option will read an unencrypted private (or public) key file in the format specified by the -m option and print an OpenSSH compatible private (or public) key to stdout. this option allows importing keys from other software, including several commercial SSH implementations. the default import format is โ€œRFC4716โ€. reverse uno card ๐Ÿ”„
  • -K โ€” download resident keys from a FIDO authenticator. public and private key files will be written to the current directory for each downloaded key. if multiple FIDO authenticators are attached, keys will be downloaded from the first touched authenticator. see the โ€œFIDO AUTHENTICATORโ€ section for more information. ๐Ÿ”‘
  • -k โ€” generate a KRL file. in this mode, ssh-keygen will generate a KRL file at the location specified via the -f flag that revokes every key or certificate presented on the command line. keys/certificates to be revoked may be specified by public key file or using the format described in the โ€œKEY REVOCATION LISTSโ€ section. it's giving consequences ๐Ÿ’…
  • -L โ€” prints the contents of one or more certificates. ๐Ÿ“œ
  • -l โ€” show fingerprint of specified public key file. ssh-keygen will try to find the matching public key file and prints its fingerprint. if combined with -v, a visual ASCII art representation of the key is supplied with the fingerprint. not me getting a whole portrait of my key ๐Ÿ˜ญ๐ŸŽจ
  • -M generate โ€” generate candidate Diffie-Hellman Group Exchange (DH-GEX) parameters for eventual use by the โ€˜diffie-hellman-group-exchange-*โ€™ key exchange methods. the numbers generated by this operation must be further screened before use. see the โ€œMODULI GENERATIONโ€ section for more information. ๐Ÿงฎ

(more coming bestie, don't go anywhere ๐Ÿ’–)

  • -M screen โ€” screening time for the Diffie-Hellman Group Exchange candidates, bestie</p> ๐Ÿ’… This one takes a whole list of candidate numbers and vibe-checks them: are they safe (Sophie Germain) primes, and do they have acceptable group generators? Whatever comes out of this lil operation can get yeeted into the /etc/ssh/moduli file. More deets in the โ€œMODULI GENERATIONโ€ section, go look ๐Ÿ‘€
  • -m key_format โ€” pick your key format โœจ This applies to key generation, to the -i (import) and -e (export) conversion options, and to the -p change-passphrase operation. That last one can be used to convert between OpenSSH private key and PEM private key formats, no cap. The formats that are supported are:
    • RFC4716 โ€” RFC 4716/SSH2 public or private key
    • PKCS8 โ€” PKCS8 public or private key
    • PEM โ€” PEM public key

By default, OpenSSH writes newly-generated private keys in its own format, but when you're converting public keys for export, the default format is RFC4716. And heads up: if you set the format to PEM while generating or updating a supported private key type, the key gets stored in the legacy PEM private key format. Legacy, bestie. We're in vintage territory ๐Ÿซ 

  • -N new_passphrase โ€” hands over the new passphrase ๐Ÿ” That's it, that's the whole thing.
  • -n principals โ€” list the principal(s) โ€” those are user or host names โ€” that should be included in a certificate when you're signing a key. You can list multiple principals, just separate them with commas. The โ€œCERTIFICATESโ€ section has the full lore ๐Ÿ“œ
  • -O option โ€” a key/value option, ooooh ๐ŸŽ€ These are specific to whatever operation you asked ssh-keygen to do, so which ones apply depends entirely on the mission.

When you're signing certificates, you can specify one of the options listed in the โ€œCERTIFICATESโ€ section right here.

When you're doing moduli generation or screening, you can specify one of the options listed in the โ€œMODULI GENERATIONโ€ section.

When you're generating FIDO authenticator-backed keys, the options listed in the โ€œFIDO AUTHENTICATORโ€ section may be specified.

When you're changing the passphrase for an existing key, the options listed in the documentation for the -p flag may be used.

When you're doing signature-related options with the -Y flag, these are the ones that get accepted:

  • hashalg=algorithm โ€” picks the hash algorithm used for hashing the message that's about to be signed. Valid algorithms are sha256 and sha512. The default is sha512. ๐Ÿ‘‘
  • print-pubkey โ€” prints the full public key to standard output after signature verification. Show me the key, show me the key ๐Ÿ’…
  • verify-time=timestamp โ€” specifies a time to use when validating signatures instead of the current time. You can give the time as a date or time in the YYYYMMDD[Z] format or in the YYYYMMDDHHMM[SS][Z] format. Dates and times get interpreted in the current system time zone unless you slap a Z character on the end, which makes them get interpreted in the UTC time zone. ๐ŸŒ

When you're generating SSHFP DNS records from public keys using the -r flag, this one is accepted:

  • hashalg=algorithm โ€” picks a hash algorithm to use when printing SSHFP records using the -D flag. Valid algorithms are sha1 and sha256. The default is to print both. both, because why choose โœจ

And the -O option can be specified multiple times, so stack 'em up bestie ๐Ÿงš

  • -P passphrase โ€” provides the (old) passphrase ๐Ÿ”“ Yes, old. The one from before. The pre-glow-up passphrase.
  • -p โ€” requests changing the passphrase of a private key file instead of creating a brand new private key. The program will prompt you for the file containing the private key, for the old passphrase, and twice for the new passphrase. Double-check energy, we love to see it ๐Ÿ’–

Updating the passphrase causes encrypted keys to be reencrypted, which is lowkey a glow-up opportunity: it lets you change the cipher and/or the number of KDF rounds (that's the -Z and -a options respectively, iykyk).

This option can also be used to set or clear FIDO related options via the -O flag. These FIDO options can be modified:

  • touch-required / no-touch-required โ€” adds or removes the requirement that signatures made using this key include a demonstration of user presence (like, the user actually touching the authenticator). ๐Ÿ‘†
  • verify-required / no-verify-required โ€” adds or removes the requirement for signatures made using this key to first verify the user identity, e.g. by PIN or on-token biometrics. ๐Ÿ”
  • -Q โ€” tests whether keys have been revoked in a KRL ๐Ÿšซ And if you also throw in the -l option, then the contents of the KRL get printed. Surprise bonus content ๐ŸŽ
  • -q โ€” silence ssh-keygen ๐Ÿคซ Not a peep. Main character silence.
  • -R hostname | [hostname]:port โ€” removes all keys belonging to the specified hostname (with an optional port number) from a known_hosts file. This one is genuinely useful for deleting hashed hosts โ€” see the -H option above. Bye bye, ghost of hosts past ๐Ÿ‘‹
  • -r hostname โ€” prints the SSHFP fingerprint resource record named hostname for the specified public key file. ๐Ÿ“‡
  • -s ca_key โ€” certifies (signs) a public key using the specified CA key. The โ€œCERTIFICATESโ€ section has the details.

When you're generating a KRL, -s instead specifies a path to a CA public key file used to revoke certificates directly by key ID or serial number. See the โ€œKEY REVOCATION LISTSโ€ section for that whole situation. ๐Ÿ“‹

  • -t ecdsa | ecdsa-sk | ed25519 | ed25519-sk | mldsa44-ed25519 | rsa โ€” specifies the type of key to create ๐Ÿ—๏ธ The possible values are ecdsa, ecdsa-sk, ed25519 (the default), ed25519-sk, mldsa44-ed25519 or rsa. So yeah, ed25519 is the default, and it's iconic for a reason โญ

This flag can also be used to specify the desired signature type when signing certificates using an RSA CA key. The available RSA signature variants are:

  • ssh-rsa โ€” SHA1 signatures, not recommended ๐Ÿ’€
  • rsa-sha2-256
  • rsa-sha2-512 โ€” the default for RSA keys ๐Ÿ‘‘
  • -U โ€” when used together with -s or -Y sign, this option indicates that a CA key resides in an ssh-agent(1). See the โ€œCERTIFICATESโ€ section for more info. Your key is in the agent, bestie, it's living its best life ๐Ÿซถ
  • -u โ€” update a KRL ๐Ÿ“ When specified with -k, the keys you list via the command line get added to the existing KRL rather than a whole new KRL being created. Growth mindset ๐ŸŒฑ
  • -V validity_interval โ€” specify a validity interval when signing a certificate โณ A validity interval can be a single time โ€” meaning the certificate is valid starting now and expires at that time โ€” or it can be two times separated by a colon, which indicates an explicit time interval. It's giving "from X to Y" energy.

The start time may be specified as:

  • The string always, to indicate the certificate has no specified start time. ๐ŸŒˆ
  • A date or time in the system time zone formatted as YYYYMMDD or YYYYMMDDHHMM[SS].
  • A date or time in the UTC time zone as YYYYMMDDZ or YYYYMMDDHHMM[SS]Z.
  • A relative time before the current system time, consisting of a minus sign followed by an interval in the format described in the TIME FORMATS section of sshd_config(5).
  • A raw seconds since epoch (Jan 1 1970 00:00:00 UTC) as a hexadecimal number beginning with 0x.

The end time may be specified similarly to the start time:

  • The string forever, to indicate the certificate has no specified end time. โ™พ๏ธ
  • A date or time in the system time zone formatted as YYYYMMDD or YYYYMMDDHHMM[SS].
  • A date or time in the UTC time zone as YYYYMMDDZ or YYYYMMDDHHMM[SS]Z.
  • A relative time after the current system time, consisting of a plus sign followed by an interval in the format described in the TIME FORMATS section of sshd_config(5).
  • A raw seconds since epoch (Jan 1 1970 00:00:00 UTC) as a hexadecimal number beginning with 0x.

For example, feast your eyes ๐Ÿ‘€:

+52w1d  Valid from now to 52 weeks and one day from now.

-4w:+4w
        Valid from four weeks ago to four weeks from now.

20100101123000:20110101123000
        Valid from 12:30 PM, January 1st, 2010 to 12:30 PM,
        January 1st, 2011.

20100101123000Z:20110101123000Z
        Similar, but interpreted in the UTC time zone rather
        than the system time zone.

-1d:20110101
        Valid from yesterday to midnight, January 1st, 2011.

0x1:0x2000000000
        Valid from roughly early 1970 to May 2033.

-1m:forever
        Valid from one minute ago and never expiring.
  • -v โ€” verbose mode ๐Ÿ” Makes ssh-keygen print debugging messages about its progress. This is helpful for debugging moduli generation, fr fr. Multiple -v options increase the verbosity. The maximum is 3. Don't try to go past 3, bestie, there's nothing there ๐Ÿ›‘
  • -w provider โ€” specifies a path to a library that will be used when creating FIDO authenticator-hosted keys, overriding the default of using the internal USB HID support. ๐Ÿ”Œ Custom provider, fancy.
  • -Y find-principals โ€” find the principal(s) associated with the public key of a signature ๐Ÿ•ต๏ธ You provide the signature using the -s flag, and an authorized signers file using the -f flag. The format of the allowed signers file is documented in the โ€œALLOWED SIGNERSโ€ section below. If one or more matching principals are found, they get returned on standard output. Found them, they were right there ๐Ÿ’…
  • -Y match-principals โ€” find the principal matching the principal name provided using the -I flag, inside the authorized signers file specified using the -f flag. If one or more matching principals are found, they get returned on standard output. Same energy, different search โœจ
  • -Y check-novalidate โ€” checks that a signature generated using ssh-keygen -Y sign has a valid structure. โš ๏ธ Heads up though: this does not validate whether a signature comes from an authorized signer. It's checking the shape, not the source, bestie. When testing a signature, ssh-keygen accepts a message on standard input and a signature namespace using -n. A file containing the corresponding signature must also be supplied using the -s flag. Successful testing of the signature is signalled by ssh-keygen returning a zero exit status. โœ…
  • -Y sign โ€” the signing era โœ๏ธโœจ. This is where ssh-keygen cryptographically signs a file or some data using an SSH key.

When it's in signing mode, ssh-keygen takes zero or more files to sign right there on the command-line โ€” and if you pass it no files at all, then ssh-keygen will sign data presented on standard input. Fr fr, it just handles it.

Signatures get written to the path of the input file with .sig appended onto the end, or to standard output if the message to be signed was read from standard input.

The key used for signing is specified using the -f option, and it may refer to either a private key, or a public key with the private half available via ssh-agent(1). You also must provide an additional signature namespace via the -n flag โ€” that's the whole thing that prevents signature confusion across different domains of use (e.g. file signing vs email signing, two completely different lanes ๐Ÿ›ฃ๏ธ). Namespaces are arbitrary strings, and may include: "file" for file signing, "email" for email signing. For custom uses, it is recommended to use names following a NAMESPACE@YOUR.DOMAIN pattern to generate unambiguous namespaces. ๐Ÿ‘‘

  • -Y verify โ€” the receipts-checking mode ๐Ÿ”. Request to verify a signature generated using ssh-keygen -Y sign as described above.

When verifying a signature, ssh-keygen accepts a message on standard input and a signature namespace using -n. A file containing the corresponding signature must also be supplied using the -s flag, along with the identity of the signer using -I and a list of allowed signers via the -f flag. The format of the allowed signers file is documented in the โ€œALLOWED SIGNERSโ€ section below. A file containing revoked keys can be passed using the -r flag. The revocation file may be a KRL or a one-per-line list of public keys. Successful verification by an authorized signer is signalled by ssh-keygen returning a zero exit status. โœ… (zero exit status = the vibe check passed, bestie.)

  • -y โ€” this option will read a private OpenSSH format file and print an OpenSSH public key to stdout. Sneaky lil "show me the public half" button ๐Ÿ”“
  • -Z cipher โ€” specifies the cipher to use for encryption when writing an OpenSSH-format private key file. You can get the list of available ciphers using "ssh -Q cipher". The default is "aes256-ctr". ๐ŸŽ€
  • -z serial_number โ€” specifies a serial number to be embedded in the certificate so it can be distinguished from others from the same CA. If the serial_number is prefixed with a + character, then the serial number will be incremented for each certificate signed on a single command-line. The default serial number is zero. ๐Ÿ”ข

When generating a KRL, the -z flag is used to specify a KRL version number.


๐ŸŒˆ MODULI GENERATION ๐ŸŒˆ

ssh-keygen may be used to generate groups for the Diffie-Hellman Group Exchange (DH-GEX) protocol. Generating these groups is a two-step process, and honestly it's giving two-part season finale: first, candidate primes are generated using a fast, but memory intensive process. Those candidate primes then get tested for suitability (a CPU-intensive process). Fast-then-thorough, we love a pipeline ๐Ÿ’–

Generation of primes is performed using the -M generate option. The desired length of the primes may be specified by the -O bits option. For example:

# ssh-keygen -M generate -O bits=2048 moduli-2048.candidates

By default, the search for primes begins at a random point in the desired length range. This may be overridden using the -O start option, which specifies a different start point (in hex). ๐ŸŽฒ

Once a set of candidates have been generated, they must be screened for suitability. This may be performed using the -M screen option. In this mode ssh-keygen will read candidates from standard input (or a file specified using the -f option). For example:

# ssh-keygen -M screen -f moduli-2048.candidates moduli-2048

By default, each candidate will be subjected to 100 primality tests. This may be overridden using the -O prime-tests option. The DH generator value will be chosen automatically for the prime under consideration. If a specific generator is desired, it may be requested using the -O generator option. Valid generator values are 2, 3, and 5. โญ

Screened DH groups may be installed in /etc/ssh/moduli. It is important that this file contains moduli of a range of bit lengths. ๐Ÿซถ

A number of options are available for moduli generation and screening via the -O flag:

  • lines=number โ€” exit after screening the specified number of lines while performing DH candidate screening.
  • start-line=line-number โ€” start screening at the specified line number while performing DH candidate screening.
  • checkpoint=filename โ€” write the last line processed to the specified file while performing DH candidate screening. This will be used to skip lines in the input file that have already been processed if the job is restarted. (Basically a save point ๐Ÿ’พ iconic behaviour.)
  • start=hex-value โ€” specify start point (in hex) when generating candidate moduli for DH-GEX.
  • generator=value โ€” specify desired generator (in decimal) when testing candidate moduli for DH-GEX.

๐Ÿ’– CERTIFICATES ๐Ÿ’–

okay so this is the part where ssh-keygen becomes a โœจliteralโœจ kingmaker, because it can sign keys to produce certificates that get used for user or host authentication. think of it like: instead of trusting a million rando keys, you trust one CA and that's it. main character energy fr.

so a certificate is made of:

  • a public key ๐Ÿ—๏ธ
  • some identity information
  • zero or more principal (user or host) names
  • a set of options

โ€ฆand all of that is signed by a Certification Authority (CA) key. that's the whole point: clients or servers can then trust only the CA key and just verify its signature on a certificate, rather than having to trust a whole crowd of user/host keys. so much less chaotic bestie.

heads up!! OpenSSH certificates are a different, and much simpler, format to the X.509 certificates used in ssl(8). not the same thing, don't mix them up ๐Ÿซถ

the two flavours ๐Ÿ“

ssh-keygen supports two types of certificates: user and host.

  • user certificates authenticate users to servers
  • host certificates authenticate server hosts to users

to generate a user certificate:

$ ssh-keygen -s /path/to/ca_key -I id -n user \
      /path/to/user_key.pub

the resultant certificate will be placed in /path/to/user_key-cert.pub. iconic placement, no notes ๐Ÿ’…

  • the argument to -I is a key identifier that will be used in logs and may be used to revoke keys
  • the argument to -n is one or more (comma-separated) principals, typically usernames, that the certificate represents

a host certificate requires the -h option:

$ ssh-keygen -s /path/to/ca_key -I id -h -n foo.example.org \
      /path/to/host_key.pub

for host certificates, the principals specified using the -n argument are hostnames and may contain wildcard characters โœจ

the host certificate will be output to /path/to/host_key-cert.pub.


CA key living somewhere fancy ๐Ÿ 

it is possible to sign using a CA key stored in a PKCS#11 token by providing the token library using -D and identifying the CA key by providing its public half as an argument to -s:

$ ssh-keygen -s ca_key.pub -D libpkcs11.so -I id -n user \
      user_key.pub

similarly, it is possible for the CA key to be hosted in an ssh-agent(1). this is indicated by the -U flag and, again, the CA key must be identified by its public half.

$ ssh-keygen -Us ca_key.pub -I id -n user user_key.pub

in all cases, key_id is a "key identifier" that is logged by the server when the certificate is used for authentication. so it's literally your name in the group chat of the server logs ๐Ÿ“


limiting who's allowed ๐Ÿ‘‘

certificates are limited to be valid for a set of principal (user/host) names. to generate a certificate for a specified set of principals:

$ ssh-keygen -s ca_key -I id -n user1,user2 user_key.pub
$ ssh-keygen -s ca_key -I id -h -n host.domain host_key.pub

additional limitations on the validity and use of user certificates may be specified through certificate options. a certificate option may:

  • disable features of the SSH session
  • be valid only when presented from particular source addresses
  • force the use of a specific command

the options that are valid for user certificates ๐ŸŽ€

  • clear โ€” clear all enabled permissions. this is useful for clearing the default set of permissions so permissions may be added individually. starting from a blank slate, bestie ๐Ÿงผ
  • critical:name[=contents], extension:name[=contents] โ€” includes an arbitrary certificate critical option or extension. the specified name should include a domain suffix, e.g. "name@example.com". if contents is specified then it is included as the contents of the extension/option encoded as a string, otherwise the extension/option is created with no contents (usually indicating a flag). extensions may be ignored by a client or server that does not recognise them, whereas unknown critical options will cause the certificate to be refused. so likeโ€ฆ critical means critical fr fr ๐Ÿ’€
  • force-command=command โ€” forces the execution of command instead of any shell or command specified by the user when the certificate is used for authentication. no free will, only vibes ๐Ÿ’…
  • no-agent-forwarding โ€” disable ssh-agent(1) forwarding (permitted by default)
  • no-port-forwarding โ€” disable port forwarding (permitted by default)
  • no-pty โ€” disable PTY allocation (permitted by default)
  • no-user-rc โ€” disable execution of ~/.ssh/rc by sshd(8) (permitted by default)
  • no-x11-forwarding โ€” disable X11 forwarding (permitted by default)
  • permit-agent-forwarding โ€” allows ssh-agent(1) forwarding
  • permit-port-forwarding โ€” allows port forwarding
  • permit-pty โ€” allows PTY allocation
  • permit-user-rc โ€” allows execution of ~/.ssh/rc by sshd(8)
  • permit-X11-forwarding โ€” allows X11 forwarding
  • no-touch-required โ€” do not require signatures made using this key include demonstration of user presence (e.g. by having the user touch the authenticator). this option only makes sense for the FIDO authenticator algorithms ecdsa-sk and ed25519-sk
  • source-address=address_list โ€” restrict the source addresses from which the certificate is considered valid. the address_list is a comma-separated list of one or more address/netmask pairs in CIDR format
  • verify-required โ€” require signatures made using this key indicate that the user was first verified, e.g. by PIN or on-token biometrics. this option only makes sense for the FIDO authenticator algorithms ecdsa-sk and ed25519-sk

at present, no standard options are valid for host keys. rip host keys, nothing for you ๐Ÿฅฒ


โฐ validity lifetime

finally, certificates may be defined with a validity lifetime. the -V option allows specification of certificate start and end times. a certificate that is presented at a time outside this range will not be considered valid. by default, certificates are valid from the Unix Epoch to the distant future โ€” so like, always and forever, lowkey romantic ๐Ÿซถ

for certificates to be used for user or host authentication, the CA public key must be trusted by sshd(8) or ssh(1). refer to those manual pages for details โœจ


๐Ÿฆ‹ FIDO AUTHENTICATOR ๐Ÿฆ‹

ssh-keygen is able to generate FIDO authenticator-backed keys, after which they may be used much like any other key type supported by OpenSSH, so long as the hardware authenticator is attached when the keys are used ๐Ÿ”Œ

FIDO authenticators generally require the user to explicitly authorise operations by touching or tapping them. yes bestie, physical contact, we love a tactile moment.

FIDO keys consist of two parts:

  1. a key handle part stored in the private key file on disk
  2. a per-device private key that is unique to each FIDO authenticator and that cannot be exported from the authenticator hardware

these are combined by the hardware at authentication time to derive the real key that is used to sign authentication challenges. it's giving teamwork honestly ๐Ÿค

supported key types are ecdsa-sk and ed25519-sk.


the options that are valid for FIDO keys ๐Ÿ”‘

  • application โ€” override the default FIDO application/origin string of "ssh:". this may be useful when generating host or domain-specific resident keys. the specified application string must begin with "ssh:"
  • challenge=path โ€” specifies a path to a challenge string that will be passed to the FIDO authenticator during key generation. the challenge string may be used as part of an out-of-band protocol for key enrollment (a random challenge is used by default)
  • device โ€” explicitly specify a fido(4) device to use, rather than letting the authenticator middleware select one
  • no-touch-required โ€” indicate that the generated private key should not require touch events (user presence) when making signatures. note that sshd(8) will refuse such signatures by default, unless overridden via an authorized_keys option
  • resident โ€” indicate that the key handle should be stored on the FIDO authenticator itself. this makes it easier to use the authenticator on multiple computers. resident keys may be supported on FIDO2 authenticators and typically require that a PIN be set on the authenticator prior to generation. resident keys may be loaded off the authenticator using ssh-add(1). storing both parts of a key on a FIDO authenticator increases the likelihood of an attacker being able to use a stolen authenticator device โš ๏ธ
  • user โ€” a username to be associated with a resident key, overriding the empty default username. specifying a username may be useful when generating multiple resident keys for the same application name
  • verify-required โ€” indicate that this private key should require user verification for each signature. not all FIDO authenticators support this option. currently PIN authentication is the only supported verification method, but other methods may be supported in the future
  • write-attestation=path โ€” may be used at key generation time to record the attestation data returned from FIDO authenticators during key generation. this information is potentially sensitive. by default, this information is discarded ๐Ÿ™ˆ

๐Ÿ’€ KEY REVOCATION LISTS ๐Ÿ’€

okay so bestie, ssh-keygen can also run the OpenSSH format Key Revocation Lists, aka KRLs. these are binary files that list exactly which keys or certs are โœจcancelledโœจ, and they're stupidly compact โ€” we're talking as little as one bit per certificate if you're revoking them by serial number. one bit. iconic efficiency tbh.

you make KRLs with the -k flag. it reads one or more files off the command line and builds you a brand new KRL. those files can hold either a KRL specification (more on that below) or plain public keys, one per line. plain public keys get revoked by sticking their hash or contents in the KRL, and certificates get revoked by serial number or key ID (if the serial is zero or not available).

using a KRL specification gives you explicit control over which record types get used to revoke stuff, and it means you can directly revoke certificates by serial number or key ID without having the whole original certificate on hand. so convenient. so slay.

a KRL specification is just lines, each one holding one of these directives, followed by a colon and some directive-specific info:

  • serial: serial_number[-serial_number] โ€” revokes a certificate with the specified serial number. serial numbers are 64-bit values, not including zero, and they can be written in decimal, hex or octal. if you give two serial numbers separated by a hyphen, then the whole range of serial numbers including and between each is revoked. the CA key must have been specified on the ssh-keygen command line using the -s option.
  • id: key_id โ€” revokes a certificate with the specified key ID string. same deal, the CA key must have been specified on the ssh-keygen command line using the -s option.
  • key: public_key โ€” revokes the specified key. if a certificate is listed, then it gets revoked as a plain public key.
  • sha1: public_key โ€” revokes the specified key by including its SHA1 hash in the KRL.
  • sha256: public_key โ€” revokes the specified key by including its SHA256 hash in the KRL. heads up: KRLs that revoke keys by SHA256 hash are not supported by OpenSSH versions prior to 7.9.
  • hash: fingerprint โ€” revokes a key using a fingerprint hash, like the one you'd grab from an sshd(8) authentication log message or from the ssh-keygen -l flag. only SHA256 fingerprints are supported here, and the resulting KRLs are not supported by OpenSSH versions prior to 7.9.

you can also update KRLs by using the -u flag alongside -k. when you do that, the keys listed on the command line get merged into the KRL, added on top of whatever's already in there. ๐Ÿซถ

and yes, you can absolutely query a KRL to test whether it revokes a particular key (or keys). the -Q flag queries an existing KRL and tests every key you list on the command line. if any key you listed has been revoked (or an error got hit), then ssh-keygen exits with a non-zero exit status. a zero exit status is only returned if no key was revoked. no cap.


๐ŸŽ€ ALLOWED SIGNERS ๐ŸŽ€

when it's verifying signatures, ssh-keygen uses a lil simple list of identities and keys to figure out whether a signature comes from an authorized source. this "allowed signers" file uses a format patterned after the AUTHORIZED_KEYS FILE FORMAT described in sshd(8). every line has these space-separated fields: principals, options, keytype, base64-encoded key. empty lines and lines starting with a โ€˜#โ€™ get ignored as comments. ๐Ÿ’…

the principals field is a pattern-list (see PATTERNS in ssh_config(5)) made of one or more comma-separated USER@DOMAIN identity patterns that are accepted for signing. when verifying, the identity you present via the -I option must match a principals pattern for that key to even be considered acceptable for verification.

the options (if they're there) are comma-separated option specifications. no spaces allowed, except inside double quotes. here's what's supported (and btw, option keywords are case-insensitive):

  • cert-authority โ€” says this key is accepted as a certificate authority (CA) and that certificates signed by this CA may be accepted for verification.
  • namespaces=namespace-list โ€” specifies a pattern-list of namespaces accepted for this key. if this option is present, the signature namespace embedded in the signature object and presented on the verification command-line must match the specified list before the key will be considered acceptable.
  • valid-after=timestamp โ€” says the key is valid for use at or after the specified timestamp, which can be a date or time in the YYYYMMDD[Z] or YYYYMMDDHHMM[SS][Z] formats. dates and times get interpreted in the current system time zone unless you slap a Z character on the end, which makes them interpreted in the UTC time zone.
  • valid-before=timestamp โ€” says the key is valid for use at or before the specified timestamp.

when verifying signatures made by certificates, the expected principal name has to match both the principals pattern in the allowed signers file and the principals embedded in the certificate itself. double check, bestie.

here's an example allowed signers file, look at her:

# Comments allowed at start of line
user1@example.com,user2@example.com ssh-rsa AAAAX1...
# A certificate authority, trusted for all principals in a domain.
*@example.com cert-authority ssh-ed25519 AAAB4...
# A key that is accepted only for file signing.
user2@example.com namespaces="file" ssh-ed25519 AAA41...

๐ŸŒˆ ENVIRONMENT ๐ŸŒˆ

  • SSH_SK_PROVIDER โ€” specifies a path to a library that'll be used when loading any FIDO authenticator-hosted keys, overriding the default of using the built-in USB HID support.

๐Ÿ“ FILES ๐Ÿ“

  • ~/.ssh/id_ecdsa
  • ~/.ssh/id_ecdsa_sk
  • ~/.ssh/id_ed25519
  • ~/.ssh/id_ed25519_sk
  • ~/.ssh/id_mldsa44_ed25519
  • ~/.ssh/id_rsa

contains the ECDSA, authenticator-hosted ECDSA, Ed25519, authenticator-hosted Ed25519 or RSA authentication identity of the user. this file should not be readable by anyone but the user. you can specify a passphrase when generating the key, and that passphrase gets used to encrypt the private part of this file using 128-bit AES. this file is not automatically accessed by ssh-keygen, but it's offered as the default file for the private key. ssh(1) will read this file when a login attempt is made.

  • ~/.ssh/id_ecdsa.pub
  • ~/.ssh/id_ecdsa_sk.pub
  • ~/.ssh/id_ed25519.pub
  • ~/.ssh/id_ed25519_sk.pub
  • ~/.ssh/id_mldsa44_ed25519.pub
  • ~/.ssh/id_rsa.pub

contains the ECDSA, authenticator-hosted ECDSA, Ed25519, authenticator-hosted Ed25519 or RSA public key for authentication. the contents of this file should be added to ~/.ssh/authorized_keys on all machines where the user wishes to log in using public key authentication. there is no need to keep the contents of this file secret. it's public, she's out and proud ๐Ÿ’–

  • /etc/ssh/moduli

contains Diffie-Hellman groups used for DH-GEX. the file format is described in moduli(5).


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

ssh(1), ssh-add(1), ssh-agent(1), moduli(5), sshd(8)

The Secure Shell (SSH) Public Key File Format, RFC 4716, 2006.


๐Ÿ‘‘ AUTHORS ๐Ÿ‘‘

OpenSSH is a derivative of the original and free ssh 1.2.12 release by Tatu Ylonen. Aaron Campbell, Bob Beck, Markus Friedl, Niels Provos, Theo de Raadt and Dug Song removed many bugs, re-added newer features and created OpenSSH. Markus Friedl contributed the support for SSH protocol versions 1.5 and 2.0.


and that's the whole page bestie, we did it ๐ŸŽ‰ this one's from the Debian folks, dated October 1, 2026, and it lives under the name SSH-KEYGEN(1). go forth and revoke responsibly ๐Ÿ’…โœจ

๐Ÿ“ฆ where this page comes from