ssh-keygen Tutorial – Generating RSA and DSA keys
In this post I will walk you through generating RSA and DSA keys using ssh-keygen. Public key authentication for SSH sessions are far superior to any password authentication and provide much higher security. ssh-keygen is the basic way for generating keys for such kind of authentication. I will also explain how to maintain those keys by changing their associated comments and more importantly by changing the passphrases using this handy utility.
Generating Keys
Generating public keys for authentication is the basic and most often used feature of ssh-keygen. ssh-keygen can generate both RSA and DSA keys. RSA keys have a minimum key length of 768 bits and the default length is 2048. When generating new RSA keys you should use at least 2048 bits of key length unless you really have a good reason for using a shorter and less secure key. The key length for DSA is always 1024 bits as specified in FIPS 186-2. Because DSA key length is limited to 1024, and RSA key length isn’t limited, so one can generate much stronger RSA keys than DSA keys, I prefer using RSA over DSA. Another reason for not using DSA is that DSA is a government standard and one may wonder if the key length was limited deliberately so it will be possible for government agencies to decrypt it.
To generate a pair of public and private keys execute the following command:
ssh-keygen -t rsa -b 2048
You can use “dsa” instead of the “rsa” after the -t to generate a DSA key. The number after the -b specifies the key length in bits.
After executing the command it may take some time to generate the keys (as the program waits for enough entropy to be gathered to generate random numbers). When the key generation is done you would be prompted to enter a filename in which the key will be saved. The public key will have the same filename but it will end with .pub. You should make sure that the key can only be read by you and not by any other user for security reasons.
Next you’ll be prompted to enter a passphrase. Each generated key can be protected by a passphrase. When a key is generated with a passphrase, the key can’t be used without the passphrase, so by using a passphrase one can prevent others from using his private keys without first guessing the passphrase. A good passphrase should be at least 10 characters long. One should stay away from English sentences as their entropy level is just too low to be used as a safe passphrase. I usually use a randomly generated passphrase, as this kind is considered the most secure. If you intend to use the key for accessing a remote machine from inside an automated script you may wish to enter an empty password, so the script won’t need user interaction. In this case just press <ENTER> twice.
If you create a passphrase-less key just make sure you only put it on trusted hosts as it may compromise the remote machine if the key falls to the wrong hands.
After entering you passphrase twice the program will print the key fingerprint, which is some kind of hashing used to distinguish different keys, followed by the default key comment (more on key comments later). After printing the key information the program will terminate. Congratulations, you’ve just created you own public key using ssh-keygen.
Comments
Adding comments to keys can allow you to organize your keys more easily. The comments are stored in end of the public key file and can be viewed in clear text. For example:
cat id_rsa2.pub ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAyyA8wePstPC69PeuHFtOwyTecByonsHFAjHbVnZ+h0dpomvLZxUtbknNj3+c7MPYKqKBOx9gUKV/diR/mIDqsb405MlrI1kmNR9zbFGYAAwIH/Gxt0Lv5ffwaqsz7cECHBbMojQGEz3IH3twEvDfF6cu5p00QfP0MSmEi/eB+W+h30NGdqLJCziLDlp409jAfXbQm/4Yx7apLvEmkaYSrb5f/pfvYv1FEV1tS8/J7DgdHUAWo6gyGUUSZJgsyHcuJT7v9Tf0xwiFWOWL9WsWXa9fCKqTeYnYJhHlqfinZRnT/+jkz0OZ7YmXo6j4Hyms3RCOqenIX1W6gnIn+eQIkw== This is the key's comment
As you can see the comment is appended in clear text to the end of the public key file. To alter the comment just edit the public key file with a plain text editor such as nano or vim.
To add a comment to the public key file when generating the key add to the key generation command -C "you comment". For example to generate 4048 bit RSA key with “home machine” as a comment you will do the following:
ssh-keygen -b 4048 -t rsa -C "home machine"
Notice that each copy of a public key can have its own comment and you cannot retrieve the comment from the private key.
Passphrases
Passphrases allow you to prevent unauthorized usage of your key by meaning of protecting the key itself by a password. Although your ssh directory holding the private keys should be unaccessible to other users, the root user of the system, or anyone who can achieve is privileges can access your key and copy it. In this case the passphrase will prevent him from using it.
To add a passphrase to a key just type it when prompted during the key generation process. Keep in mind that the password must be at least 5 characters long. A good passphrase, as I said before, should be at least 10 characters long, and consist of random upper and lower case letters, numbers and symbols.
While the passphrase boosts the security of the key, under some conditions you may want to leave it empty. Leaving the passphrase empty allows you to use the key from within scripts, for example to transfer a file via scp. While passphraseless keys are very useful for scripts just remember to only use them at trusted machines.
You can change the passphrase of key after it’s been created, and you should do it at least annually. To change the passphrase execute:
ssh-keygen -p
After this you will be prompted to enter the location of your private key and enter twice the new passphrase. If you don’t want a passphrase just enter empty one.
End Notes
Feel free to share your public key, as its name suggests, it should be public. Keep in mind that your private key should be kept private. If someone gets hold of your private key, change it imediatly, even if it’s passphrase protected.
hi….have gone thru the doc….think its not helpful for beginners….
–I think there shud be something like “Going thru this doc req. basic knowlege of xxxx”
RajaSekhar
2 Jan 08 at 11:07
perfect;
takes the fluff out;
just what a practitioner needs;
Moncy Vergis
21 May 08 at 21:39
Running this command requires knowledge of keyboard usage:
ssh-keygen -t rsa -b 2048
rox0r
2 Jul 08 at 17:16
Thanks for this
Dave
13 Oct 08 at 09:52
[...] read the following articles: SSH Advanced Techniques; Advanced SSH Usage; SSH Host Key Protection; ssh-keygen Tutorial; and SSH authorized_keys howto. What I will provide, though, are three quick [...]
Quick Introduction to Advanced SSH « Code Ghar
17 Feb 09 at 02:18
Thank for this information. I has all the basic information required to generate ssh key.
Shravan Kumar
9 Apr 09 at 17:12
Thanks Guy. I need the quick howto and the description of DSA vs. RSA made it simple and clear which to use.
Bob
14 Apr 09 at 17:26
Hello! I tried to generate the key without passphrase but when I try to connect to another pc OpenSSH asks for the passphrase… i just press enter and it works…
but why does OpenSSH ask for the passphrase if I didn’t set it?
I’m working on an Ubuntu PC but I think it should be the same thing on another Linux distribution, am I right?
Thank you.
Doddy
9 May 09 at 12:40
I never came across such problem, it looks like there is something weird in the your ssh configuration. Take a look at /etc/ssh/ssh_config and see maybe it something you can tweak.
Guy
12 May 09 at 15:40
Hi,
good stuff. I’d appreciate a simple comment outlining the usage of the generated keys e.g. can I include them in my keychain and use them to encrypt and sign emails with the maill.app on a Mac Os X?
Thanks.
Hans
3 Jul 09 at 21:30
Hi Hans,
Unfortunately I don’t have any experience with the mail app on Mac, so I can’t instruct how to use the generated keys with it.
Guy
4 Jul 09 at 12:51
Good article – very clear and concise. It’s also good to understand the advantage of using rsa over dsa which I’d never read before.
Jeff Howe
20 Jul 09 at 23:19
[...] http://www.guyrutenberg.com/2007/10/05/ssh-keygen-tutorial-generating-rsa-and-dsa-keys/ has a good tutorial that takes you to the point of creating the private and public keys. [...]
Using SSH keys without entering a password | The bloody toe (a runner's blog)
29 Jul 09 at 20:03
I like the way the information is provided. Indeed the information gives a very clear understanding.
I have two questions.
1. DSA – govt Standard ,does it mean RSA cannot be decrypted?
2. Can I use rsa and dsa? Are they going to conflict each other?
thank you,
Palanikumar
Palanikumar
3 May 10 at 17:58
1. The government doesn’t disclose any information about what they can do with it. Both are widely used so I think they should be good enough.
2. RSA and DSA are completely different algorithms. You can’t use both for the same key.
Guy
3 May 10 at 20:30