SSH (Secure Shell) is a protocol for connecting two computers securely. It provides secure authentication as well as encryption. If you use Linux, most distros have ssh installed by default.
You can log to a Linux machine using SSH protocol from a putty client by entering the username and password. But if, both the machines are yours only, and no one else uses those machines, it makes a hindrance to enter the username and password, every time you do SSH to that server.
Like most users enable the auto login feature on their machines, we want to auto login using putty also. Though, you shouldn’t do this on your office computers or Laptops. Instead of authenticating using a username and password, we will authenticate using the RSA keys.
Generating the RSA and DSA Keys for SSH
To enable Passwordless SSH Using Key Based Authentication, go to your Linux server, and open a terminal and type:
ssh-keygen -t rsa
or
ssh-keygen -t dsa
After entering the above command you will be prompted for the location to save the file. By default this will be either ~/.ssh/id_rsa or ~/.ssh/id_dsa depending on the type of key generated. Just hit the enter key to save it to the default location, or specify a different name. You will then be prompted for a passphrase. Type this in and hit the enter key; you will then be prompted to re-enter to confirm. After doing so, two files will be created: the private keyfile is the name specified (by default id_rsa or id_dsa) and the public one the same but with a .pub extension.
Now go to the ~/.ssh/ directory and do the following:
cd ~/.ssh/
cat id_dsa.pub >> ~/.ssh/authorized_keys
rm id_dsa.pub
chmod 0600 authorized_keys
cd ../
chmod 0700 .ssh
Finally, we will need to tell ssh to allow the use of keys. So, we need to edit the sshd_config file. So, use your favorite editor and edit the /etc/ssh/sshd_config file.
sudo vi /etc/ssh/sshd_config
Find and edit these lines as follows:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys
Save your changes and reload the ssh daemon:
/etc/init.d/ssh reload
exit
Generating the Private Keys for Putty
Now, ftp the ~/.ssh/id_dsa file to your Windows box and delete that file from the Linux server.
Run the puttygen.exe application by double-clicking the file you downloaded (it does not need to be installed) and select “Import Key” from the “Conversions” menu as shown in the example screenshot below. This will open a standard Windows open dialog; locate the RSA or DSA private key file and click the “Open” button. These files are usually named something like id_rsa and id_dsa.
If the private key file is protected by a passphrase (highly recommended) then you will be prompted for this before the key is loaded.
The private key file is now loaded into PuttyGen as shown in the screenshot below.
Note the “Key Comment” by default will be something like “imported-openssh-key”. You should change this to something a little more meaningful, such as your name. To save the private key click the “Save Private Key” button and then choose a place to save it using the Windows save dialog. The saved private key will be named with a .ppk extension.
Connecting to an SSH server with the private key file
Now that the key has been generated we can run PuTTY to connect to the SSH server.
Select the “SSH -> Auth” section in the left navigation of the PuTTY configuration page as shown in the screenshot below. Then browse for the file you created above in the circled region.
Now click the “Data” option under the “Connection”. This is the highlighted blue option in the screenshot below. Then enter your auto-login name in the box which I’ve highlighted in red. In this example the login name is “pbolia”.
Now, just click the open button, and instead of asking for password, it will automatically log in using the RSA keys.
Using username "pbolia".
Authenticating with public key "pbolia"
Key is of wrong type (PuTTY SSH2 private key) Error
If you get an error message like so when you try to log in:
Trying public key authentication.
Key is of wrong type (PuTTY SSH2 private key)
Then you are attempting to connect to an SSH1 server with an SSH2 generated key. Go to the Connection/SSH options page and make sure the “Preferred SSH protocol version” option is set to either “2″ or “2 only”.
References: