Generating a key pair for automatic SSH Public Key Authentication

30 November -0001

Public Key Authentication (also referred to as PKA) is an authentication method that bypasses the traditional password prompt for SSH access. Installing a public key on a remote SSH server allows you to log into that machine without entering a passphrase. While this can open a host of security vulnerabilities, it is sometimes necessary to execute scripts that fire ssh scripts that don't have the ability to properly authenticate against the remote machine. A good work-around to these security vulnerabilities is to limit command access to shells spawned with SSH PKA.

This tutorial assumes you have SSHSecureShell client installed locally and the remote machine is using OpenSSH ( You can download this package for Windows from has two types of clients available, the free client and a commercial version. Download the free, non-commercial version for private use.

Make sure to install the command line utilities for the package. Check to make sure your environmental variables include the path to the executable. You can check this in your Control Panel under 'System'. Check the 'Advanced' tab, then click the 'Environment Variables' button. Your PATH variables should list something like 'C:\Program Files\SSH Communications Security\SSH Secure Shell'. If this does not exist find out where SSH has installed and add it to your environmental variables by clicking the PATH listing, then the 'Edit' button. Open a command prompt (Start->Run->cmd.exe) and type in:

> ssh-keygen2

This executes the command ssh-keygen2.exe usually found in C:\Program Files\SSH Communications Security\SSH Secure Shell. You will be prompted for your passphrase. Go ahead and enter a relatively complex passphrase. Completing this command generates a public/private key pair that is stored in:

C:\Documents and Settings\[your userName]\Application Data\SSH\UserKeys

Where [your userName] is the username that you log into Windows under. The next step is to move your public key (the one with the .pub extension) onto the target machine. To do this SSH to the target machine. In your home directory check to see if a .ssh directory exists. Look for one using:

$ ls -lah 

on a linux system (on Solaris try "ls -la"). If you don't see the .ssh directory create one using:

$ mkdir .ssh

Once that directory exists you need to sftp your public key into the directory. Use SSH's file transfer interface to do this. Once your key is in the target directory you'll need to change the format to an OpenSSH format. The current format should look something like:

Subject: Your User Name
Comment: "2048-bit dsa, Your User Name@YourMachineName, Wed Mar 23 2005 15:21:27"

Assume for this example that your key is named You'll need to alter the format to an OpenSSH formaby typing in:

$ ssh-keygen -i -f >> authorized_keys

You can then take a look at the new key ("authorized_keys") and it should begin with the string "ssh-dss" and then a bunch of gibberish.

Make sure to check your modes on the .ssh directory and the authorized_keys file. The directory should bee set to 700 and the authorized keys file to 600 with:

$ chmod 700 .ssh
$ chmod 600 .ssh/authorized_keys

Next to you'll need to update the authentication mode in you SSH client. Open it up and edit your profiles using the "Profiles" button and selecting "Edit Profiles". Highlight the profile you wish to edit in the left hand pane. Check the "Authentication" tab and make sure "Public Key" is first in the list. The target system must have PKA enabled. On a Mandrake Linux system you can find this configuration in /etc/ssh/sshd_config on the lines that looks like:

#PubkeyAuthentication yes  
#AuthorizedKeysFile     .ssh/authorized_keys

Uncomment these lines and restart the ssh daemon to allow PKA authentication. Once these changes take effect you should be able to fire up your Windows SSH client and connect to the desired machine without entering a passphrase.