Creating an Identity

Most SSH implementations include a program for creating key pairs. We will cover ssh-keygen from SSH1, SSH2, and OpenSSH.

Generating RSA Keys for SSH1

SSH1 and its derivatives use the program ssh-keygen1 to create key pairs. ["Generating Key Pairs with ssh-keygen"] The program might also be called ssh-keygen, depending on how SSH1 was installed. Let's go into more detail about this program than we have before. Appendix B, "SSH Quick Reference" summarizes the ssh-keygen options.ssh-keygen1 can create new keys or modify existing keys. When creating a new key, you may indicate the following with command-line options: If you specify both -f (specify output file) and -N (specify password), ssh-keygen1 issues no prompts. Therefore, you can automate key generation using these options (and perhaps redirecting output to /dev/null ) :

$ ssh-keygen1 -f mykey -N secretword


You might use this technique to automate generation of a large number of keys for some purpose. Use it carefully, though, on a secure machine. The password on the command line is probably visible to other users on the same Unix machine via ps or similar programs, and if you're scripting with this technique, obviously the passphrases shouldn't be kept in files for long.In addition to creating keys, ssh-keygen1 can modify existing keys in the following ways:
WARNING: Before using any option that places your passphrase on the shell command line, such as -N or -P, carefully consider the security implications. Because the passphrase appears on your screen, it may be visible to onlookers, and while running, it may be visible in the machine's process list. Because it is on the command line, it is visible to other users on the same host using the ps command. In addition, if your shell creates history files of the commands you type, the passphrase is inserted into a history file where it can be read by a third party.Also, if you think have a good reason to just type Return and give your key no passphrase, think again. Doing that is essentially equivalent to putting your password in a file in your home directory named MY-PASSWORD.PLEASE-STEAL-ME. If you don't want to have to type a passphrase, the right thing to do is to use ssh-agent, trusted-host authentication, or Kerberos. There are very limited circumstances having to do with unattended usage (e.g., cron jobs) where a plaintext, passphrase-less client key might be acceptable. ["Unattended SSH: Batch or cron Jobs"]
When you make changes to a key, such as its passphrase or comment, the changes are applied to the key file only. If you have keys loaded into an SSH agent, the copies in the agents aren't changed. For instance, if you list the keys in the agent with ssh-add1 -l (lowercase L) after changing the comment, you still see the old comment in the agent. To make the changes take effect in the agent, unload and reload the affected keys.

Generating RSA/DSA Keys for SSH2

SSH2 and its derivatives use the cleverly named program ssh-keygen2 to create key pairs. The program might also be called ssh-keygen, depending on how SSH2 was installed. As with ssh-keygen1, you can create new keys or modify existing ones; however, the command-line options are significantly different. ssh-keygen2 also has a few other options for printing diagnostics.When creating a new key, you may choose the name of the private key file to be generated, by specifying the name at the end of the command line:

$ ssh-keygen2 mykey creates mykey and mykey.pub


The name is relative to your current directory, and as usual, the public key file is named after the private one with pub appended. If you omit this option, the key is saved in the directory ~/.ssh2, in a file whose name indicates the encryption algorithm and number of bits. An example is id_dsa_1024_a, which was generated by the DSA algorithm with 1024 bits.You also may indicate the following with command-line options: In addition to creating keys, ssh-keygen2 can operate on existing keys in the following ways: ssh-keygen2 also gives you some control over input, output and diagnostics: Finally, ssh-keygen2 has one guru-level advanced option, -r, for affecting the random numbers used for key generation. It causes ssh-keygen2 to modify ~/.ssh2/random_seed using data you enter on standard input. ["Randomness"] The SSH2 manpages call this "stirring data into the random pool." Note that the program doesn't prompt you to enter data; it just sits there looking like it's hung. When this occurs, type as much data as you like and press the EOF character (Control-D in most shells).

$ ssh-keygen2 -r I am stirring the random pool. blah blah blah ^D Stirred in 46 bytes.


Generating RSA/DSA Keys for OpenSSH

OpenSSH's ssh-keygen program supports all the same features and options of its SSH1 counterpart. It also adds the capability to generate DSA keys for SSH-2 connections and a few more options:

Selecting a Passphrase

Choose your passphrases carefully. Make them at least 10 characters long, containing a mix of uppercase and lowercase letters, digits, and nonalphanumeric symbols. At the same time, you want the passphrase to be easy to remember, but hard for others to guess. Don't use your name, username, phone number, or other easily guessed information in the passphrase. Coming up with an effective passphrase can be a chore, but the added security is worth it.If you forget a passphrase, you are out of luck: the corresponding SSH private key becomes unusable because you can't decrypt it. The same encryption that makes SSH so secure also makes passphrases impossible to recover. You have to abandon your SSH key, generate a new one, and choose a new passphrase for it. You must also install the new public key on every machine that had your original.