1

I am not able to ssh from Windows 7 to VMWare Guest CentOS 6.2

I have Windows 7 64 bit OS on my Laptop. Installed Strawberry Perl 5.16.2 on it. I have also installed VMWare Player and running CentOS 6.2 on it.

Using the articles I found on the net, I have been successfully able to SSH without password from PUTTY to the CentOS 6.2.

I have created user perl514 on CentOS 6.2. Using the script below, if I login using the username and password, everything works. But it doesn't work with the Public and Private Keys that I created using the SSH KEYGEN Tool that came along with the PUTTY.

Given below is the script:

    #!C:\strawberry\perl\bin\perl.exe

    use Modern::Perl;
    use Net::SSH2;


    my $hostname = '192.168.247.128';
    my $username = 'perl514';
    my $password = 'redhat';

    my $ssh2 = Net::SSH2->new();
    say "Connecting to $hostname";

    $ssh2->connect("$hostname") || die "PROBELM - $!";

    #$ssh2->auth_password("$username","$password") || die "Username/Password not #right";#COMMENTED OUT. This Works. Stuff given below does not work.
    $ssh2->auth_publickey ("perl514", "C:\\Users\\winuser\\Documents\\perl\\work\\putty_priv.ppk",
                   "C:\\Users\\winuser\\Documents\\perl\\work\\public.pub") || die "ERROR", $ssh2->error;



    my $chan = $ssh2->channel();
    $chan->blocking(0);
    $chan->exec('ls -la');
    while (<$chan>){ print } 

I get the following error:

    Connecting to 192.168.247.128
    ERROR-19LIBSSH2_ERROR_PUBLICKEY_UNVERIFIEDInvalid public key at ssh2.pl line 17.

With the username and password, it works fine. But not with the public and private key.

I am pretty sure I am going wrong somewhere. Kindly help me.

3 Answers3

1

Net::SSH2 expects the key files to be in the OpenSSH format that is different to that used by PuTTY.

You should be able to convert to OpenSSH format using the PuTTY GUI. For instance, see How to convert SSH keypairs generated using PuttyGen(Windows) into key-pairs used by ssh-agent and KeyChain(Linux) .

update:

Detailed conversion steps:

  • Open PuTTY key generator
  • Load the private key into it
  • On the conversions menu, export the private key in OpenSSH format
  • Select and copy with the mouse the public key for OpenSSH from the box under "Public key for pasting into OpenSSH authorized_keys file" and paste it into a new file with the same name as the one you have given to the private key with ".pub" appended.
Community
  • 1
  • 1
salva
  • 9,943
  • 4
  • 29
  • 57
  • Hi Salva, Thank you for responding. I did go through the articles you mentioned, but they seem to be talking about generating keys from a linux box to login to another linux box. Of the steps given below, here is what I can understand: – pritesh_ugrankar Nov 27 '12 at 11:27
0

Sorry to answer the question rather than commenting, but the comment section doesnt let me put steps one below the other. I checked your link and here is what I understand:

    1.Open PuttyGen
    2.Click Load
    3.Load your private key
    4.Go to Conversions->Export OpenSSH and export your private key

The steps above I know are to be run on Windows Box, but what about the steps below? Are they to be run on the CentOS Guest? Not sure if I have to copy the private key created by putty inside the CentOS 6.2 Guest OS. Another problem is the Private Key created by Puttygen has private as well as public key in it.

     5.Copy your private key to ~/.ssh/id_dsa (or id_rsa).

     6.Create the RFC 4716 version of the public key using ssh-keygen

    ssh-keygen -e -f ~/.ssh/id_dsa > ~/.ssh/id_dsa_com.pub

     7.Convert the RFC 4716 version of the public key to the OpenSSH format:

    ssh-keygen -i -f ~/.ssh/id_dsa_com.pub > ~/.ssh/id_dsa.pub

Reason why I am doing this is, once this test setup works, I will be using similar methods to log into some NAS Arrays (linux based) to run some reports. Hence the questions.

  • Instead of creating an answer you can just edit your question. – salva Nov 27 '12 at 11:47
  • Hi Salva, Sorry I was not aware of that. The link that you had provided had two other links, the first one is exactly what I tried to get to the CentOS Guest OS from my Windows Laptop (Office) to login without a password, and it worked perfectly fine. It doesn't work from within the script for some reason. I have installed Ubuntu 12.10 on my personal laptop, and will try it on that. Earlier installed SL 6.3 on it, but switched to Ubuntu as some other stuff wasn't working as expected. I'll try this on that, and I am sure it will work, just this Windows stuff is not working out. – pritesh_ugrankar Nov 27 '12 at 14:10
  • You can activate debugging on the Net::SSH2 object with `$ssh2->debug(1)` – salva Nov 27 '12 at 14:27
0

I didn't have ssh server installed on CentOS vm. After installing ssh server it worked.

yum -y install openssh-server openssh-clients