SSH-KEYGEN(1)

๐ŸŽ€ NAME ๐ŸŽ€

okay so ssh-keygen is the authentication key generation, management and conversion girlie from the General Commands Manual, section 1. periodt. ๐Ÿ’…


โœจ SYNOPSIS โœจ

       ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa | rsa1]
                  [-N new_passphrase] [-C comment] [-f output_keyfile]
       ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]
       ssh-keygen -i [-m key_format] [-f input_keyfile]
       ssh-keygen -e [-m key_format] [-f input_keyfile]
       ssh-keygen -y [-f input_keyfile]
       ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]
       ssh-keygen -l [-v] [-E fingerprint_hash] [-f input_keyfile]
       ssh-keygen -B [-f input_keyfile]
       ssh-keygen -D pkcs11
       ssh-keygen -F hostname [-f known_hosts_file] [-l]
       ssh-keygen -H [-f known_hosts_file]
       ssh-keygen -R hostname [-f known_hosts_file]
       ssh-keygen -r hostname [-f input_keyfile] [-g]
       ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]
       ssh-keygen -T output_file -f input_file [-v] [-a rounds] [-J num_lines]
                  [-j start_line] [-K checkpt] [-W generator]
       ssh-keygen -s ca_key -I certificate_identity [-h] [-n principals]
                  [-O option] [-V validity_interval] [-z serial_number]
                  file ...
       ssh-keygen -L [-f input_keyfile]
       ssh-keygen -A
       ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]
                  file ...
       ssh-keygen -Q -f krl_file file ...

๐Ÿ’– DESCRIPTION ๐Ÿ’–

okay bestie, so ssh-keygen generates, manages and converts authentication keys for ssh(1). she can create keys for use by SSH protocol versions 1 and 2.

Protocol 1 should not be used and is only offered to support legacy devices. It suffers from a number of cryptographic weaknesses and doesn't support many of the advanced features available for protocol 2. no cap, do not use protocol 1. ๐Ÿ’€

The type of key to be generated is specified with the -t option. If invoked without any arguments, ssh-keygen will generate an RSA key for use in SSH protocol 2 connections.

ssh-keygen is also used to generate groups for use in Diffie-Hellman group exchange (DH-GEX). See the โ€œMODULI GENERATIONโ€ section for details.

Finally, ssh-keygen can be used to generate and update Key Revocation Lists, and to test whether given keys have been revoked by one. See the โ€œKEY REVOCATION LISTSโ€ section for details.

Normally each user wishing to use SSH with public key authentication runs this once to create the authentication key in ~/.ssh/identity, ~/.ssh/id_dsa, ~/.ssh/id_ecdsa, ~/.ssh/id_ed25519 or ~/.ssh/id_rsa. Additionally, the system administrator may use this to generate host keys. ๐Ÿ‘‘

Normally this program generates the key and asks for a file in which to store the private key. The public key is stored in a file with the same name but โ€œ.pubโ€ appended. The program also asks for a passphrase. The passphrase may be empty to indicate no passphrase (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 any string of characters you want. 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. The passphrase can be changed later by using the -p option. ๐Ÿ”‘

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. ๐Ÿ˜ญ

For RSA1 keys and keys stored in the newer OpenSSH format, there is also a comment field in the key file that is only for convenience to the user to help identify the key. The comment can tell what the key is for, or whatever is useful. The comment is initialized to โ€œuser@hostโ€ when the key is created, but can be changed using the -c option. ๐ŸŽ€

After a key is generated, instructions below detail where the keys should be placed to be activated.

The options are as follows:


๐Ÿ’… OPTIONS ๐Ÿ’…

  • -A โ€” For each of the key types (rsa1, rsa, dsa, ecdsa and ed25519) for which host keys do not exist, generate the host keys with the default key file path, an empty passphrase, default bits for the key type, and default comment. This is used by system administration scripts to generate new host keys. โœจ
  • -a rounds โ€” When saving a new-format private key (i.e. an ed25519 key or any SSH protocol 2 key when the -o flag is set), this option specifies the number of KDF (key derivation function) rounds used. Higher numbers result in slower passphrase verification and increased resistance to brute-force password cracking (should the keys be stolen). When screening DH-GEX candidates (using the -T command), this option specifies the number of primality tests to perform. ๐Ÿ”ฅ
  • -B โ€” Show the bubblebabble digest of specified private or public key file. ๐Ÿฆ‹
  • -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 2048 bits. Generally, 2048 bits is considered sufficient. DSA keys must be exactly 1024 bits as specified by FIPS 186-2. 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. Ed25519 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. This operation is only supported for RSA1 keys and keys stored in the newer OpenSSH format. The program will prompt for the file containing the private keys, for the passphrase if the key has one, and for the new comment. ๐ŸŽ€
  • -D pkcs11 โ€” Download the RSA 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 the 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. ๐Ÿ“ค
  • -F hostname โ€” Search for the specified hostname 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. ๐Ÿ“
  • -G output_file โ€” Generate candidate primes for DH-GEX. These primes must be screened for safety (using the -T option) before use. ๐Ÿงช
  • -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. ๐Ÿ•ต๏ธ
  • -h โ€” When signing a key, create a host certificate instead of a user certificate. Please see the โ€œCERTIFICATESโ€ section for details. ๐Ÿ‘‘
  • -I certificate_identity โ€” Specify the key identity when signing a public key. Please 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โ€. ๐Ÿ“ฅ
  • -J num_lines โ€” Exit after screening the specified number of lines while performing DH candidate screening using the -T option. ๐Ÿšช
  • -j start_line โ€” Start screening at the specified line number while performing DH candidate screening using the -T option. ๐Ÿšฆ
  • -K checkpt โ€” Write the last line processed to the file checkpt while performing DH candidate screening using the -T option. This will be used to skip lines in the input file that have already been processed if the job is restarted. ๐Ÿ’พ
  • -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. ๐Ÿšซ
  • -L โ€” Prints the contents of one or more certificates. ๐Ÿ“œ
  • -l โ€” Show fingerprint of specified public key file. Private RSA1 keys are also supported. For RSA and DSA keys ssh-keygen tries 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. ๐ŸŽจ
  • -M memory โ€” Specify the amount of memory to use (in megabytes) when generating candidate moduli for DH-GEX. ๐Ÿง 
  • -m key_format โ€” Specify a key format for the -i (import) or -e (export) conversion options. The supported key formats are: โ€œRFC4716โ€ (RFC 4716/SSH2 public or private key), โ€œPKCS8โ€ (PEM PKCS8 public key) or โ€œPEMโ€ (PEM public key). The default conversion format is โ€œRFC4716โ€. ๐Ÿ—‚๏ธ
  • -N new_passphrase โ€” Provides the new passphrase. ๐Ÿ”‘
  • -n principals โ€” Specify one or more principals (user or host names) to be included in a certificate when signing a key. Multiple principals may be specified, separated by commas. Please see the โ€œCERTIFICATESโ€ section for details. ๐Ÿ‘ฅ
  • -O option โ€” drop a certificate option while you're signing a key ๐Ÿ’…. You can say this one more than once, stack them up like charms on a bracelet. The deets live in the CERTIFICATES section down below. The ones that actually do something for user certificates are:
    • clear โ€” wipes all the permissions that were enabled. Super handy when you wanna nuke the default permission set so you can add them back one by one, your way ๐ŸŽ€.
    • force-command=command โ€” forces command to run instead of whatever shell or command the user tries to use when this cert is doing auth. no takebacks, bestie ๐Ÿซถ.
    • no-agent-forwarding โ€” turns off ssh-agent(1) forwarding (which is allowed by default, fyi).
    • no-port-forwarding โ€” turns off port forwarding (also on by default).
    • no-pty โ€” turns off PTY allocation (yep, default-on).
    • no-user-rc โ€” stops sshd(8) from running ~/.ssh/rc (default-on, again).
    • no-x11-forwarding โ€” turns off X11 forwarding (you guessed it: default-on).
    • permit-agent-forwarding โ€” allows ssh-agent(1) forwarding โœจ.
    • permit-port-forwarding โ€” allows port forwarding.
    • permit-pty โ€” allows PTY allocation.
    • permit-user-rc โ€” allows sshd(8) to run ~/.ssh/rc.
    • permit-x11-forwarding โ€” allows X11 forwarding.
    • source-address=address_list โ€” locks down which source addresses this cert counts as valid from. The address_list is a comma-separated list of one or more address/netmask pairs in CIDR format. gatekeeping, but make it cryptography ๐Ÿ”ฅ.
rn there are literally no options that are valid for host keys. nada. zero. ๐Ÿ’€
  • -o โ€” makes ssh-keygen save private keys in the shiny new OpenSSH format instead of the more compatible PEM format. The new format puts up way more of a fight against brute-force password cracking ๐Ÿ’ช, but OpenSSH versions before 6.5 just don't support it. Also: Ed25519 keys always use the new private key format, no matter what you say.
  • -P passphrase โ€” hands over the (old) passphrase.
  • -p โ€” asks to change the passphrase of a private key file instead of making a whole new private key. It's gonna prompt you for the file with the private key in it, then the old passphrase, then twice for the new one. glow-up szn ๐Ÿ’–.
  • -Q โ€” tests whether keys have been revoked in a KRL.
  • -q โ€” silences ssh-keygen. shhh ๐Ÿคซ.
  • -R hostname โ€” deletes every key that belongs to hostname from a known_hosts file. This one is your bestie for deleting hashed hosts (peep the -H option above).
  • -r hostname โ€” prints the SSHFP fingerprint resource record named hostname for the public key file you gave it.
  • -S start โ€” sets the start point (in hex) when you're generating candidate moduli for DH-GEX.
  • -s ca_key โ€” certifies (signs) a public key with the CA key you named ๐Ÿ‘‘. Head to the CERTIFICATES section for the full story.

When you're generating a KRL, -s gives a path to a CA public key file that's used to revoke certificates directly by key ID or serial number. See the KEY REVOCATION LISTS section for the tea.

  • -T output_file โ€” tests DH group exchange candidate primes (the ones made with the -G option) to see if they're safe.
  • -t dsa | ecdsa | ed25519 | rsa | rsa1 โ€” says what type of key to create. The possible values are rsa1 for protocol version 1, and dsa, ecdsa, ed25519, or rsa for protocol version 2. pick your fighter โญ.
  • -u โ€” updates a KRL. When you use it alongside -k, the keys you listed on the command line get added to the existing KRL instead of a brand new KRL being created.
  • -V validity_interval โ€” sets a validity interval when signing a certificate. A validity interval can be a single time (meaning the cert is valid starting now and expires at that time), or two times separated by a colon to spell out an explicit time interval. The start time can be a date in YYYYMMDD format, a time in YYYYMMDDHHMMSS format, or a relative time (relative to the current time) made of a minus sign followed by a relative time in the format described in the TIME FORMATS section of sshd_config(5). The end time can be a YYYYMMDD date, a YYYYMMDDHHMMSS time, or a relative time starting with a plus character.

For example: +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), -1d:20110101 (valid from yesterday to midnight, January 1st, 2011). it's giving time traveler fr fr ๐Ÿฆ‹.

  • -v โ€” verbose mode. Makes ssh-keygen print debugging messages about what it's doing. Genuinely helpful for debugging moduli generation. Multiple -v options crank the verbosity up higher โ€” max is 3.
  • -W generator โ€” sets the generator you want when testing candidate moduli for DH-GEX.
  • -y โ€” reads a private OpenSSH format file and prints an OpenSSH public key to stdout. lil magic trick โœจ.
  • -z serial_number โ€” sets a serial number to embed in the certificate, so you can tell this cert apart from the other ones from the same CA. Default serial number is zero.

When you're generating a KRL, the -z flag is what you use to set a KRL version number.


๐ŸŒˆ MODULI GENERATION ๐ŸŒˆ

ssh-keygen can be used to generate groups for the Diffie-Hellman Group Exchange (DH-GEX) protocol. Making these groups is a two-step process: first, candidate primes get generated with a fast-but-memory-hungry process. Then those candidate primes get tested for suitability (and that part is CPU-intensive ๐Ÿ˜ฎโ€๐Ÿ’จ).

Prime generation happens with the -G option. You can set how long you want the primes with the -b option. Like so:

# ssh-keygen -G moduli-2048.candidates -b 2048

By default the hunt for primes starts at a random point inside the length range you asked for. You can override that with the -S option, which sets a different start point (in hex).

Once you've got a set of candidates, they gotta get screened for suitability. That's the -T option's job. In this mode ssh-keygen reads candidates from standard input (or a file you name with the -f option). For example:

# ssh-keygen -T moduli-2048 -f moduli-2048.candidates

By default every candidate gets hit with 100 primality tests. You can override that with the -a option. The DH generator value gets picked automatically for whatever prime is under consideration. But if you want a specific generator, you can ask for it with the -W option. Valid generator values are 2, 3, and 5.

Screened DH groups can be installed in /etc/ssh/moduli. It matters that this file has moduli of a range of bit lengths, and that both ends of a connection share common moduli ๐Ÿซถ.


๐Ÿ‘‘ CERTIFICATES ๐Ÿ‘‘

ssh-keygen supports signing keys to produce certificates that can be used for user or host authentication. A certificate is made of a public key, some identity info, zero or more principal (user or host) names, and a set of options โ€” all signed by a Certification Authority (CA) key. After that, clients or servers can trust just the CA key and verify its signature on a certificate instead of trusting a whole pile of user/host keys. Heads up: OpenSSH certificates are a different, and much simpler, format than the X.509 certificates used in ssl(8).

ssh-keygen supports two types of certificates: user and host. User certificates authenticate users to servers, while host certificates authenticate server hosts to users. To generate a user certificate:

$ ssh-keygen -s /path/to/ca_key -I key_id /path/to/user_key.pub

The resulting certificate lands in /path/to/user_key-cert.pub. A host certificate needs the -h option:

$ ssh-keygen -s /path/to/ca_key -I key_id -h /path/to/host_key.pub

The host certificate gets output to /path/to/host_key-cert.pub.

You can totally sign using a CA key stored in a PKCS#11 token โ€” just hand over the token library with -D and identify the CA key by passing its public half as an argument to -s:

$ ssh-keygen -s ca_key.pub -D libpkcs11.so -I key_id user_key.pub

In every case, key_id is a "key identifier" that the server logs whenever the certificate is used for authentication.

Certificates can be limited to being valid for a set of principal (user/host) names. By default, generated certificates are valid for all users or hosts. To generate a certificate for a specific set of principals:

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

Extra limits on the validity and use of user certificates can be set through certificate options. A certificate option can disable features of the SSH session, be valid only when presented from particular source addresses, or force the use of a specific command. For the list of valid certificate options, check the documentation for the -O option above โ˜๏ธ.

Finally, certificates can be given a validity lifetime. The -V option lets you set certificate start and end times. A certificate presented at a time outside that range won't be considered valid. By default, certificates are valid from the Unix Epoch all the way to the distant future. main character lifespan, honestly ๐Ÿ’….

For certificates to be used for user or host authentication, the CA public key must be trusted by sshd(8) or ssh(1). Go peek at those manual pages for the details ๐Ÿ“–.

๐Ÿ’€ KEY REVOCATION LISTS ๐Ÿ’€

okay bestie this is the villain era part of ssh-keygen โ€” we're talking about Key Revocation Lists (KRLs for short), and no, that's not a typo, that's just the acronym being extra. ssh-keygen can manage OpenSSH format KRLs, and these are binary files that list out keys or certificates you're revoking, in a format so compact it can take as little as one bit per certificate if you're revoking by serial number. literally one bit. the girlies love efficiency โœจ

You make a KRL with the -k flag. What it does: it reads one or more files off the command line and generates a brand new KRL. Those files can either be a KRL specification (more on that in a sec) or public keys, listed one per line. Plain public keys get revoked by listing 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).

Revoking keys with a KRL specification gives you explicit control over which types of record get used to revoke stuff, and it also lets you directly revoke certificates by serial number or key ID without having the complete original certificate on hand. so you don't even need the whole receipt, just the number. ๐Ÿ’…

A KRL specification is lines containing 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, they do not include 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 one gets 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. 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 here, then it gets revoked as a plain public key.
  • sha1: public_key โ€” Revokes the specified key by its SHA1 hash.

KRLs can also be updated using the -u flag alongside -k. When you do that, the keys listed via the command line get merged into the KRL, adding to the ones already chilling there.

You can also go the other way: given a KRL, test whether it revokes a particular key (or keys). The -Q flag queries an existing KRL, testing each key specified on the command line. Here's the dramatic part: if any key listed on the command line has been revoked (or an error happens), then ssh-keygen exits with a non-zero exit status. A zero exit status only comes back if no key was revoked. so like, exit zero is the good timeline. ๐Ÿซถ

๐Ÿ“ FILES ๐Ÿ“

  • ~/.ssh/identity โ€” Contains the protocol version 1 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; that passphrase will be used to encrypt the private part of this file using 3DES. 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/identity.pub โ€” Contains the protocol version 1 RSA public key for authentication. The contents of this file should be added to ~/.ssh/authorized_keys on all machines where the user wants to log in using RSA authentication. There's no need to keep the contents of this file secret.
  • ~/.ssh/id_dsa, ~/.ssh/id_ecdsa, ~/.ssh/id_ed25519, ~/.ssh/id_rsa โ€” Contains the protocol version 2 DSA, ECDSA, 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; that passphrase will be 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_dsa.pub, ~/.ssh/id_ecdsa.pub, ~/.ssh/id_ed25519.pub, ~/.ssh/id_rsa.pub โ€” Contains the protocol version 2 DSA, ECDSA, 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 wants to log in using public key authentication. There's no need to keep the contents of this file secret.
  • /etc/ssh/moduli โ€” Contains Diffie-Hellman groups used for DH-GEX. The file format is described in moduli(5).
ok the whole vibe here is: private files = keep them secret keep them safe ๐Ÿ”’, public files = share them everywhere, no shame. that's the whole point of asymmetric crypto and she ate. ๐Ÿ’…

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

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 a wrap on the whole page bestie ๐ŸŽ€ this part came from the Debian build, dated June 16, 2016, and it's the SSH-KEYGEN(1) manual โ€” go forth and key-gen responsibly, mwah ๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from