SSH Keying through PuTTY on Windows or Linux

Here we will go over how to setup SSH keys using PuTTY, a popular SSH client for Windows. There is also a Linux version as well.


1. Download PuTTY and PuTTYgen

First you will need to download PuTTY and PuTTYgen. You can get the latest version for both at the following links:

  • PuTTY: the Telnet and SSH client itself (Windows x86)
  • PuTTYgen: an RSA and DSA key generation utility (Windows x86)

The Unix/Linux version is available on the PuTTY download page.


2. Configure PuTTY

First things first, we need to create a profile in PuTTY to access our server. Run PuTTY and you’ll be shown the basic PuTTY options. In the Host Name field, enter your domain, or the IP address of your server. The port should already be default (22). However, if you’re connecting to your shared/reseller account, we use a non-standard port. In that event, change 22 to 2222.

Under Connection > Data, specify the username you wish to connect with under Auto-login username. If this is not root, it will most likely be your cPanel username.

Click back over to Session (top of the left menu), type a name to identify your settings, i usually use my domain name, then click save. This will save the current configuration profile into PuTTY for later use.


3. Connect to the Server

Now that PuTTY is configured, we should connect to the server at this time. To do so, click Open. This will open the PuTTY session. You will prompted about a potential security breach. This is normal, click Yes. This should bring you to the bash prompt of your server. If you see something like the below, then everything worked.

gator100 ~ $

4. Generate the SSH Key

Now we need to generate the SSH key itself. To do this, fire up PuTTYgen. Make sure to select SSH-2 RSA under type of key to generate, and specify the bits to 1024 (this is default).

Go ahead and click on generate, then move the cursor around the blank area. PuTTYgen uses this movement to generate the key’s randomness.

Once this is generated, you can set a key comment, or a passphrase. The comment isn’t important, as it’s just a label, but the passphrase will require that you enter this passphrase when using the key. If you’re trying to have a ‘passwordless login’ then this should be left blank.

Now click Save Public Key, and save this file to a name of your choosing somewhere safe on your harddrive. Please remember the location of this file, as it is needed later. Then do the same for Save Private Key.


5. Get the Keys on the Server

Now that we have the keys generated and saved, we need to get the keys on the server. Copy the data inside the putty window under Public Key for pasting into SSH authorized keys file. It should look something like this. (Note that the below example is all on one line. Do not use 3 lines, as displayed in the example.)

ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAi5zTs+THmSa5rxfc3rYzVORk9neoefVZg1PZwSI
9vj/eg3UL5fg8ENCw9OGRm3R0t2FgfvpudWGkiNaOp1HWz3FamX7zZ4svqJHloYgpL
/0vzZynFEc2Hm2O024PLzy7G6H6GDTSuGxU1Ar7gluBiNDfR9SXcUD+CddliW2+zXc= user@domain.com

Please Note:When you paste the key, it needs to be all on one line, with no extra spaces or line returns.

Go back to the PuTTY session and run the following commands in order.

mkdir ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys2

This will bring up a text editor, simply right click in the putty window to paste the key you copied earlier. Now generally this will paste into three lines, you want it to be on one line. so go to the beginning of the second and third lines, and hit backspace once. This should move the entire string all into one line.

Once it’s all in one line, and looks good, hit CTRL+X, then Y, then Enter. This will save the file and return you back to the bash prompt. There, run the following command.

chmod 600 ~/.ssh/authorized_keys2

6. Configure SSH Keys to Work

Now we’re almost done. Close PuTTY, and re-open it, so we get back to the configuration menu. Click on the profile you saved earlier, and click load. This will load the profiles configuration into the current PuTTY window.

On the left hand side, click SSH > Auth. Here is the configuration for the key. Click on browse, and browse your harddrive for the private key. we saved earlier. Now go back to sessions (again, top left) and click save once more.


7. Confirm Setup is Successful

And you’re all set. To confirm that all this worked, simply click open again. PuTTY will attempt a connection with your server, send the key, and your server should accept it without error. You should be returned instantly to the bash prompt.