How to generate SSH keys

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

There may be times when you need to generate SSH keys, such as when you use Git over SSH, you will need to provide your public SSH key to the server that you want to connect to.

Your SSH key consists of a private and public key located in .ssh directory.  Because this directory starts with a dot, it is a hidden directory that you need to enable settings in your operating system to see it.  In Windows, it is typically located in “C:\Users\Username\.ssh”

Look in that folder to see if you already have a set of SSH keys.  For example, my .ssh folder contains these …

.ssh-folder

.ssh-folder

The id_rsa is the private RSA key.  The id_rsa.pub is the public RSA key.  The private one you keep private.  The public one is the one that you can send to the server or provide to your system administrator.  RSA is a type of cryptosystem for public-key encryption.  Another cryptosystem is DSA.  So you might see id_dsa.pub instead or as well.  The known_hosts filecontains the public key of known hosts that you have connected to before.

Let’s assume that you don’t have any of those files and not even the .ssh folder and you want to generate them.  If you do, re-generating may overwrite the ones you have (which may or may not be fine depending on your situtation).

You use ssh-keygen, which is a Unix utility.  If you are on Windows, you can install Git for Windows and you get Git Bash, a terminal that when launched gives you the ssh-keygen command.

You type …

ssh-keygen -t rsa -C “your_email@example.com”

The “-t rsa” specify that we are going to generate a rsa key.   The -C flag indicates that a comment is to follow.  And we just use our email address as the comment.

When I run this command in Git Bash, it asks me to enter the file to save to.  And by default, it would be the location mentioned above.  It also asked if I wanted to overwrite (since I had file already).  It also will ask for a pass phrase, which is optional (just press enter for empty/none pass phrase).  This is an arbitrary password that you come up with.  But it is called a pass phrase because you can have spaces in it.

The pass phrase is an extra layer of security so that when you use SSH, it will ask you for your pass phrase before continuing.

Upon completion of the command, it tells you that it generated the file and the fingerprint.

Open the newly generated id_rsa.pub file and you will see that it starts with “ssh-rsa ” and a space followed by the public key which is bunch of alphanumeric characters.  At the end is two equal signs of which follows is the comment that was entered upon key generation.

Copy the whole content of that file.  That is what you would paste in the SSH keys for GitHub or other services that you want to connect to.  Make sure you paste the public key and not the private key.

 


Related Posts

Tags

Share This