1SSH-KEYGEN(1) BSD General Commands Manual SSH-KEYGEN(1)
2
4 ssh-keygen — authentication key generation, management and conversion
5
7 ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa]
8 [-N new_passphrase] [-C comment] [-f output_keyfile]
9 [-m format]
10 ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]
11 [-m format]
12 ssh-keygen -i [-m key_format] [-f input_keyfile]
13 ssh-keygen -e [-m key_format] [-f input_keyfile]
14 ssh-keygen -y [-f input_keyfile]
15 ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]
16 ssh-keygen -l [-v] [-E fingerprint_hash] [-f input_keyfile]
17 ssh-keygen -B [-f input_keyfile]
18 ssh-keygen -D pkcs11
19 ssh-keygen -F hostname [-f known_hosts_file] [-l]
20 ssh-keygen -H [-f known_hosts_file]
21 ssh-keygen -R hostname [-f known_hosts_file]
22 ssh-keygen -r hostname [-f input_keyfile] [-g]
23 ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]
24 ssh-keygen -T output_file -f input_file [-v] [-a rounds] [-J num_lines]
25 [-j start_line] [-K checkpt] [-W generator]
26 ssh-keygen -s ca_key -I certificate_identity [-h] [-U]
27 [-D pkcs11_provider] [-n principals] [-O option]
28 [-V validity_interval] [-z serial_number] file ...
29 ssh-keygen -L [-f input_keyfile]
30 ssh-keygen -A [-f prefix_path]
31 ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]
32 file ...
33 ssh-keygen -Q -f krl_file file ...
34
36 ssh-keygen generates, manages and converts authentication keys for
37 ssh(1). ssh-keygen can create keys for use by SSH protocol version 2.
38
39 The type of key to be generated is specified with the -t option. If
40 invoked without any arguments, ssh-keygen will generate an RSA key.
41
42 ssh-keygen is also used to generate groups for use in Diffie-Hellman
43 group exchange (DH-GEX). See the MODULI GENERATION section for details.
44
45 Finally, ssh-keygen can be used to generate and update Key Revocation
46 Lists, and to test whether given keys have been revoked by one. See the
47 KEY REVOCATION LISTS section for details.
48
49 Normally each user wishing to use SSH with public key authentication runs
50 this once to create the authentication key in ~/.ssh/id_dsa,
51 ~/.ssh/id_ecdsa, ~/.ssh/id_ed25519 or ~/.ssh/id_rsa. Additionally, the
52 system administrator may use this to generate host keys, as seen in
53 /etc/rc.
54
55 Normally this program generates the key and asks for a file in which to
56 store the private key. The public key is stored in a file with the same
57 name but “.pub” appended. The program also asks for a passphrase. The
58 passphrase may be empty to indicate no passphrase (host keys must have an
59 empty passphrase), or it may be a string of arbitrary length. A
60 passphrase is similar to a password, except it can be a phrase with a
61 series of words, punctuation, numbers, whitespace, or any string of char‐
62 acters you want. Good passphrases are 10-30 characters long, are not
63 simple sentences or otherwise easily guessable (English prose has only
64 1-2 bits of entropy per character, and provides very bad passphrases),
65 and contain a mix of upper and lowercase letters, numbers, and non-
66 alphanumeric characters. The passphrase can be changed later by using
67 the -p option.
68
69 There is no way to recover a lost passphrase. If the passphrase is lost
70 or forgotten, a new key must be generated and the corresponding public
71 key copied to other machines.
72
73 ssh-keygen will by default write keys in an OpenSSH-specific format.
74 This format is preferred as it offers better protection for keys at rest
75 as well as allowing storage of key comments within the private key file
76 itself. The key comment may be useful to help identify the key. The
77 comment is initialized to “user@host” when the key is created, but can be
78 changed using the -c option.
79
80 It is still possible for ssh-keygen to write the previously-used PEM for‐
81 mat private keys using the -m flag. This may be used when generating new
82 keys, and existing new-format keys may be converted using this option in
83 conjunction with the -p (change passphrase) flag.
84
85 After a key is generated, instructions below detail where the keys should
86 be placed to be activated.
87
88 The options are as follows:
89
90 -A For each of the key types (rsa, dsa, ecdsa and ed25519) for which
91 host keys do not exist, generate the host keys with the default
92 key file path, an empty passphrase, default bits for the key
93 type, and default comment. If -f has also been specified, its
94 argument is used as a prefix to the default path for the result‐
95 ing host key files. This is used by /etc/rc to generate new host
96 keys.
97
98 -a rounds
99 When saving a private key this option specifies the number of KDF
100 (key derivation function) rounds used. Higher numbers result in
101 slower passphrase verification and increased resistance to brute-
102 force password cracking (should the keys be stolen).
103
104 When screening DH-GEX candidates (using the -T command). This
105 option specifies the number of primality tests to perform.
106
107 -B Show the bubblebabble digest of specified private or public key
108 file.
109
110 -b bits
111 Specifies the number of bits in the key to create. For RSA keys,
112 the minimum size is 1024 bits and the default is 2048 bits. Gen‐
113 erally, 2048 bits is considered sufficient. DSA keys must be
114 exactly 1024 bits as specified by FIPS 186-2. For ECDSA keys,
115 the -b flag determines the key length by selecting from one of
116 three elliptic curve sizes: 256, 384 or 521 bits. Attempting to
117 use bit lengths other than these three values for ECDSA keys will
118 fail. Ed25519 keys have a fixed length and the -b flag will be
119 ignored.
120
121 -C comment
122 Provides a new comment.
123
124 -c Requests changing the comment in the private and public key
125 files. The program will prompt for the file containing the pri‐
126 vate keys, for the passphrase if the key has one, and for the new
127 comment.
128
129 -D pkcs11
130 Download the public keys provided by the PKCS#11 shared library
131 pkcs11. When used in combination with -s, this option indicates
132 that a CA key resides in a PKCS#11 token (see the CERTIFICATES
133 section for details).
134
135 -E fingerprint_hash
136 Specifies the hash algorithm used when displaying key finger‐
137 prints. Valid options are: “md5” and “sha256”. The default is
138 “sha256”.
139
140 -e This option will read a private or public OpenSSH key file and
141 print to stdout a public key in one of the formats specified by
142 the -m option. The default export format is “RFC4716”. This
143 option allows exporting OpenSSH keys for use by other programs,
144 including several commercial SSH implementations.
145
146 -F hostname | [hostname]:port
147 Search for the specified hostname (with optional port number) in
148 a known_hosts file, listing any occurrences found. This option
149 is useful to find hashed host names or addresses and may also be
150 used in conjunction with the -H option to print found keys in a
151 hashed format.
152
153 -f filename
154 Specifies the filename of the key file.
155
156 -G output_file
157 Generate candidate primes for DH-GEX. These primes must be
158 screened for safety (using the -T option) before use.
159
160 -g Use generic DNS format when printing fingerprint resource records
161 using the -r command.
162
163 -H Hash a known_hosts file. This replaces all hostnames and
164 addresses with hashed representations within the specified file;
165 the original content is moved to a file with a .old suffix.
166 These hashes may be used normally by ssh and sshd, but they do
167 not reveal identifying information should the file's contents be
168 disclosed. This option will not modify existing hashed hostnames
169 and is therefore safe to use on files that mix hashed and non-
170 hashed names.
171
172 -h When signing a key, create a host certificate instead of a user
173 certificate. Please see the CERTIFICATES section for details.
174
175 -I certificate_identity
176 Specify the key identity when signing a public key. Please see
177 the CERTIFICATES section for details.
178
179 -i This option will read an unencrypted private (or public) key file
180 in the format specified by the -m option and print an OpenSSH
181 compatible private (or public) key to stdout. This option allows
182 importing keys from other software, including several commercial
183 SSH implementations. The default import format is “RFC4716”.
184
185 -J num_lines
186 Exit after screening the specified number of lines while perform‐
187 ing DH candidate screening using the -T option.
188
189 -j start_line
190 Start screening at the specified line number while performing DH
191 candidate screening using the -T option.
192
193 -K checkpt
194 Write the last line processed to the file checkpt while perform‐
195 ing DH candidate screening using the -T option. This will be
196 used to skip lines in the input file that have already been pro‐
197 cessed if the job is restarted.
198
199 -k Generate a KRL file. In this mode, ssh-keygen will generate a
200 KRL file at the location specified via the -f flag that revokes
201 every key or certificate presented on the command line.
202 Keys/certificates to be revoked may be specified by public key
203 file or using the format described in the KEY REVOCATION LISTS
204 section.
205
206 -L Prints the contents of one or more certificates.
207
208 -l Show fingerprint of specified public key file. For RSA and DSA
209 keys ssh-keygen tries to find the matching public key file and
210 prints its fingerprint. If combined with -v, a visual ASCII art
211 representation of the key is supplied with the fingerprint.
212
213 -M memory
214 Specify the amount of memory to use (in megabytes) when generat‐
215 ing candidate moduli for DH-GEX.
216
217 -m key_format
218 Specify a key format for key generation, the -i (import), -e
219 (export) conversion options, and the -p change passphrase opera‐
220 tion. The latter may be used to convert between OpenSSH private
221 key and PEM private key formats. The supported key formats are:
222 “RFC4716” (RFC 4716/SSH2 public or private key), “PKCS8” (PEM
223 PKCS8 public key) or “PEM” (PEM public key). The default conver‐
224 sion format is “RFC4716”. Setting a format of “PEM” when gener‐
225 ating or updating a supported private key type will cause the key
226 to be stored in the legacy PEM private key format.
227
228 -N new_passphrase
229 Provides the new passphrase.
230
231 -n principals
232 Specify one or more principals (user or host names) to be
233 included in a certificate when signing a key. Multiple princi‐
234 pals may be specified, separated by commas. Please see the
235 CERTIFICATES section for details.
236
237 -O option
238 Specify a certificate option when signing a key. This option may
239 be specified multiple times. See also the CERTIFICATES section
240 for further details.
241
242 At present, no standard options are valid for host keys. The
243 options that are valid for user certificates are:
244
245 clear Clear all enabled permissions. This is useful for clear‐
246 ing the default set of permissions so permissions may be
247 added individually.
248
249 critical:name[=contents]
250 extension:name[=contents]
251 Includes an arbitrary certificate critical option or
252 extension. The specified name should include a domain
253 suffix, e.g. “name@example.com”. If contents is speci‐
254 fied then it is included as the contents of the exten‐
255 sion/option encoded as a string, otherwise the exten‐
256 sion/option is created with no contents (usually indicat‐
257 ing a flag). Extensions may be ignored by a client or
258 server that does not recognise them, whereas unknown
259 critical options will cause the certificate to be
260 refused.
261
262 force-command=command
263 Forces the execution of command instead of any shell or
264 command specified by the user when the certificate is
265 used for authentication.
266
267 no-agent-forwarding
268 Disable ssh-agent(1) forwarding (permitted by default).
269
270 no-port-forwarding
271 Disable port forwarding (permitted by default).
272
273 no-pty Disable PTY allocation (permitted by default).
274
275 no-user-rc
276 Disable execution of ~/.ssh/rc by sshd(8) (permitted by
277 default).
278
279 no-x11-forwarding
280 Disable X11 forwarding (permitted by default).
281
282 permit-agent-forwarding
283 Allows ssh-agent(1) forwarding.
284
285 permit-port-forwarding
286 Allows port forwarding.
287
288 permit-pty
289 Allows PTY allocation.
290
291 permit-user-rc
292 Allows execution of ~/.ssh/rc by sshd(8).
293
294 permit-X11-forwarding
295 Allows X11 forwarding.
296
297 source-address=address_list
298 Restrict the source addresses from which the certificate
299 is considered valid. The address_list is a comma-sepa‐
300 rated list of one or more address/netmask pairs in CIDR
301 format.
302
303 -P passphrase
304 Provides the (old) passphrase.
305
306 -p Requests changing the passphrase of a private key file instead of
307 creating a new private key. The program will prompt for the file
308 containing the private key, for the old passphrase, and twice for
309 the new passphrase.
310
311 -Q Test whether keys have been revoked in a KRL.
312
313 -q Silence ssh-keygen.
314
315 -R hostname | [hostname]:port
316 Removes all keys belonging to the specified hostname (with
317 optional port number) from a known_hosts file. This option is
318 useful to delete hashed hosts (see the -H option above).
319
320 -r hostname
321 Print the SSHFP fingerprint resource record named hostname for
322 the specified public key file.
323
324 -S start
325 Specify start point (in hex) when generating candidate moduli for
326 DH-GEX.
327
328 -s ca_key
329 Certify (sign) a public key using the specified CA key. Please
330 see the CERTIFICATES section for details.
331
332 When generating a KRL, -s specifies a path to a CA public key
333 file used to revoke certificates directly by key ID or serial
334 number. See the KEY REVOCATION LISTS section for details.
335
336 -T output_file
337 Test DH group exchange candidate primes (generated using the -G
338 option) for safety.
339
340 -t dsa | ecdsa | ed25519 | rsa
341 Specifies the type of key to create. The possible values are
342 “dsa”, “ecdsa”, “ed25519”, or “rsa”.
343
344 -U When used in combination with -s, this option indicates that a CA
345 key resides in a ssh-agent(1). See the CERTIFICATES section for
346 more information.
347
348 -u Update a KRL. When specified with -k, keys listed via the com‐
349 mand line are added to the existing KRL rather than a new KRL
350 being created.
351
352 -V validity_interval
353 Specify a validity interval when signing a certificate. A valid‐
354 ity interval may consist of a single time, indicating that the
355 certificate is valid beginning now and expiring at that time, or
356 may consist of two times separated by a colon to indicate an
357 explicit time interval.
358
359 The start time may be specified as the string “always” to indi‐
360 cate the certificate has no specified start time, a date in
361 YYYYMMDD format, a time in YYYYMMDDHHMM[SS] format, a relative
362 time (to the current time) consisting of a minus sign followed by
363 an interval in the format described in the TIME FORMATS section
364 of sshd_config(5).
365
366 The end time may be specified as a YYYYMMDD date, a YYYYMMD‐
367 DHHMM[SS] time, a relative time starting with a plus character or
368 the string “forever” to indicate that the certificate has no
369 expirty date.
370
371 For example: “+52w1d” (valid from now to 52 weeks and one day
372 from now), “-4w:+4w” (valid from four weeks ago to four weeks
373 from now), “20100101123000:20110101123000” (valid from 12:30 PM,
374 January 1st, 2010 to 12:30 PM, January 1st, 2011), “-1d:20110101”
375 (valid from yesterday to midnight, January 1st, 2011).
376 “-1m:forever” (valid from one minute ago and never expiring).
377
378 -v Verbose mode. Causes ssh-keygen to print debugging messages
379 about its progress. This is helpful for debugging moduli genera‐
380 tion. Multiple -v options increase the verbosity. The maximum
381 is 3.
382
383 -W generator
384 Specify desired generator when testing candidate moduli for DH-
385 GEX.
386
387 -y This option will read a private OpenSSH format file and print an
388 OpenSSH public key to stdout.
389
390 -z serial_number
391 Specifies a serial number to be embedded in the certificate to
392 distinguish this certificate from others from the same CA. If
393 the serial_number is prefixed with a ‘+’ character, then the
394 serial number will be incremented for each certificate signed on
395 a single command-line. The default serial number is zero.
396
397 When generating a KRL, the -z flag is used to specify a KRL ver‐
398 sion number.
399
401 ssh-keygen may be used to generate groups for the Diffie-Hellman Group
402 Exchange (DH-GEX) protocol. Generating these groups is a two-step
403 process: first, candidate primes are generated using a fast, but memory
404 intensive process. These candidate primes are then tested for suitabil‐
405 ity (a CPU-intensive process).
406
407 Generation of primes is performed using the -G option. The desired
408 length of the primes may be specified by the -b option. For example:
409
410 # ssh-keygen -G moduli-2048.candidates -b 2048
411
412 By default, the search for primes begins at a random point in the desired
413 length range. This may be overridden using the -S option, which speci‐
414 fies a different start point (in hex).
415
416 Once a set of candidates have been generated, they must be screened for
417 suitability. This may be performed using the -T option. In this mode
418 ssh-keygen will read candidates from standard input (or a file specified
419 using the -f option). For example:
420
421 # ssh-keygen -T moduli-2048 -f moduli-2048.candidates
422
423 By default, each candidate will be subjected to 100 primality tests.
424 This may be overridden using the -a option. The DH generator value will
425 be chosen automatically for the prime under consideration. If a specific
426 generator is desired, it may be requested using the -W option. Valid
427 generator values are 2, 3, and 5.
428
429 Screened DH groups may be installed in /etc/gsissh/moduli. It is impor‐
430 tant that this file contains moduli of a range of bit lengths and that
431 both ends of a connection share common moduli.
432
434 ssh-keygen supports signing of keys to produce certificates that may be
435 used for user or host authentication. Certificates consist of a public
436 key, some identity information, zero or more principal (user or host)
437 names and a set of options that are signed by a Certification Authority
438 (CA) key. Clients or servers may then trust only the CA key and verify
439 its signature on a certificate rather than trusting many user/host keys.
440 Note that OpenSSH certificates are a different, and much simpler, format
441 to the X.509 certificates used in ssl(8).
442
443 ssh-keygen supports two types of certificates: user and host. User cer‐
444 tificates authenticate users to servers, whereas host certificates
445 authenticate server hosts to users. To generate a user certificate:
446
447 $ ssh-keygen -s /path/to/ca_key -I key_id /path/to/user_key.pub
448
449 The resultant certificate will be placed in /path/to/user_key-cert.pub.
450 A host certificate requires the -h option:
451
452 $ ssh-keygen -s /path/to/ca_key -I key_id -h /path/to/host_key.pub
453
454 The host certificate will be output to /path/to/host_key-cert.pub.
455
456 It is possible to sign using a CA key stored in a PKCS#11 token by pro‐
457 viding the token library using -D and identifying the CA key by providing
458 its public half as an argument to -s:
459
460 $ ssh-keygen -s ca_key.pub -D libpkcs11.so -I key_id user_key.pub
461
462 Similarly, it is possible for the CA key to be hosted in a ssh-agent(1).
463 This is indicated by the -U flag and, again, the CA key must be identi‐
464 fied by its public half.
465
466 $ ssh-keygen -Us ca_key.pub -I key_id user_key.pub
467
468 In all cases, key_id is a "key identifier" that is logged by the server
469 when the certificate is used for authentication.
470
471 Certificates may be limited to be valid for a set of principal
472 (user/host) names. By default, generated certificates are valid for all
473 users or hosts. To generate a certificate for a specified set of princi‐
474 pals:
475
476 $ ssh-keygen -s ca_key -I key_id -n user1,user2 user_key.pub
477 $ ssh-keygen -s ca_key -I key_id -h -n host.domain host_key.pub
478
479 Additional limitations on the validity and use of user certificates may
480 be specified through certificate options. A certificate option may dis‐
481 able features of the SSH session, may be valid only when presented from
482 particular source addresses or may force the use of a specific command.
483 For a list of valid certificate options, see the documentation for the -O
484 option above.
485
486 Finally, certificates may be defined with a validity lifetime. The -V
487 option allows specification of certificate start and end times. A cer‐
488 tificate that is presented at a time outside this range will not be con‐
489 sidered valid. By default, certificates are valid from UNIX Epoch to the
490 distant future.
491
492 For certificates to be used for user or host authentication, the CA pub‐
493 lic key must be trusted by sshd(8) or ssh(1). Please refer to those man‐
494 ual pages for details.
495
497 ssh-keygen is able to manage OpenSSH format Key Revocation Lists (KRLs).
498 These binary files specify keys or certificates to be revoked using a
499 compact format, taking as little as one bit per certificate if they are
500 being revoked by serial number.
501
502 KRLs may be generated using the -k flag. This option reads one or more
503 files from the command line and generates a new KRL. The files may
504 either contain a KRL specification (see below) or public keys, listed one
505 per line. Plain public keys are revoked by listing their hash or con‐
506 tents in the KRL and certificates revoked by serial number or key ID (if
507 the serial is zero or not available).
508
509 Revoking keys using a KRL specification offers explicit control over the
510 types of record used to revoke keys and may be used to directly revoke
511 certificates by serial number or key ID without having the complete orig‐
512 inal certificate on hand. A KRL specification consists of lines contain‐
513 ing one of the following directives followed by a colon and some direc‐
514 tive-specific information.
515
516 serial: serial_number[-serial_number]
517 Revokes a certificate with the specified serial number. Serial
518 numbers are 64-bit values, not including zero and may be
519 expressed in decimal, hex or octal. If two serial numbers are
520 specified separated by a hyphen, then the range of serial numbers
521 including and between each is revoked. The CA key must have been
522 specified on the ssh-keygen command line using the -s option.
523
524 id: key_id
525 Revokes a certificate with the specified key ID string. The CA
526 key must have been specified on the ssh-keygen command line using
527 the -s option.
528
529 key: public_key
530 Revokes the specified key. If a certificate is listed, then it
531 is revoked as a plain public key.
532
533 sha1: public_key
534 Revokes the specified key by including its SHA1 hash in the KRL.
535
536 sha256: public_key
537 Revokes the specified key by including its SHA256 hash in the
538 KRL. KRLs that revoke keys by SHA256 hash are not supported by
539 OpenSSH versions prior to 7.9.
540
541 hash: fingerprint
542 Revokes a key using a fingerprint hash, as obtained from a
543 sshd(8) authentication log message or the ssh-keygen -l flag.
544 Only SHA256 fingerprints are supported here and resultant KRLs
545 are not supported by OpenSSH versions prior to 7.9.
546
547 KRLs may be updated using the -u flag in addition to -k. When this
548 option is specified, keys listed via the command line are merged into the
549 KRL, adding to those already there.
550
551 It is also possible, given a KRL, to test whether it revokes a particular
552 key (or keys). The -Q flag will query an existing KRL, testing each key
553 specified on the command line. If any key listed on the command line has
554 been revoked (or an error encountered) then ssh-keygen will exit with a
555 non-zero exit status. A zero exit status will only be returned if no key
556 was revoked.
557
559 ~/.ssh/id_dsa
560 ~/.ssh/id_ecdsa
561 ~/.ssh/id_ed25519
562 ~/.ssh/id_rsa
563 Contains the DSA, ECDSA, Ed25519 or RSA authentication identity
564 of the user. This file should not be readable by anyone but the
565 user. It is possible to specify a passphrase when generating the
566 key; that passphrase will be used to encrypt the private part of
567 this file using 128-bit AES. This file is not automatically
568 accessed by ssh-keygen but it is offered as the default file for
569 the private key. ssh(1) will read this file when a login attempt
570 is made.
571
572 ~/.ssh/id_dsa.pub
573 ~/.ssh/id_ecdsa.pub
574 ~/.ssh/id_ed25519.pub
575 ~/.ssh/id_rsa.pub
576 Contains the DSA, ECDSA, Ed25519 or RSA public key for authenti‐
577 cation. The contents of this file should be added to
578 ~/.ssh/authorized_keys on all machines where the user wishes to
579 log in using public key authentication. There is no need to keep
580 the contents of this file secret.
581
582 /etc/gsissh/moduli
583 Contains Diffie-Hellman groups used for DH-GEX. The file format
584 is described in moduli(5).
585
587 ssh(1), ssh-add(1), ssh-agent(1), moduli(5), sshd(8)
588
589 The Secure Shell (SSH) Public Key File Format, RFC 4716, 2006.
590
592 OpenSSH is a derivative of the original and free ssh 1.2.12 release by
593 Tatu Ylonen. Aaron Campbell, Bob Beck, Markus Friedl, Niels Provos, Theo
594 de Raadt and Dug Song removed many bugs, re-added newer features and cre‐
595 ated OpenSSH. Markus Friedl contributed the support for SSH protocol
596 versions 1.5 and 2.0.
597
598BSD June 21, 2019 BSD