I've read somewhere that in order to make that function work I have to
supply it with public key with specific format due to limitation of
libssh2. The public key should look like this: <key_type>{1 space
character}<key_data>
Well it still does not work even if I change my public key file. Says
"Authentication failed for <user_name> using public key" Can somebody
help me and perhaps give an idea where to look next?
Yes, unfortunately i can not use any other way of logging in to the
remote PC, so username/password is not an option for me
Are you sure you have all the pieces in place for this? For it to work,
you must:
1) Have the public key installed on the remote server in the user's
authorized_keys file. The file (and sometimes the whole directory) must
have appropriate permissions (typically 600) or the server may ignore it.
2) Have both the private and public keys on the local machine (I'm not
sure why they require the pubkey, but they do). If the private key is
encrypted, you must also pass the passphrase to ssh2_auth_pubkey_file.
I have done this successfully without monkeying with the key files, so I
doubt that is your issue. Did you use OpenSSH to generate the keys? If
not, they could be in the wrong format - but you almost certainly did
use OpenSSH so the keys should be OK. I'm betting it's a problem on the
remote server (permissions) or the private key being encrypted.
Jeremy
Well the thing is that I have used that private key to generate my
public key (did that using PuTTYgen) I do not mention that I've added
public key to "authorized_keys" file and now i successfully login to
the remote server (using PuTTY) with this particular public key that I
use in my php script. So i assume this is not a problem of incorrect
permissions. Perhaps it happens because I use ssh-dss key type instead
of ssh-rsa key?
Ok I've tried one million ways to resolve this problem and it still
persists.
In order to make a clean experiment I've done the following
1) Generated private key using following: openssl genrsa -out ~/
private.ssh 1024
2) Extracted pub key using: ssh-keygen -y
3) Added pub key to the "authorized_keys" file
3.1) My "authorized_keys" file has following permissions: -rw-r--r--
4) Checked if i can login using: ssh -i ~/private.ssh user@<domain>
(Yes I can!!!!)
5) As far as I use PHP from Windows I've copied both private and
public keys locally to C:/ssh_keys/php
Now when I execute the following code:
$connection = ssh2_connect('<domain>', 22, array('hostkey'=>'ssh-
rsa'));
echo $connection;
echo "<br>processing keys<br>";
echo ssh2_auth_pubkey_file($connection, 'user', 'C:\\ssh_keys\\php\
\public.ssh', 'C:\\ssh_keys\\php\\privat.ssh', "");
I get the following output:
********************************************************
Resource id #2
processing keys
Warning: ssh2_auth_pubkey_file() [function.ssh2-auth-pubkey-file]:
Authentication failed for user using public key in C:\eclipse
\workspace_php\PHP_SSH\myssh.php on line 132
********************************************************
WHY???????? Why is it failed? I'm 100% sure I use valid keys and all
the key files are successfully read from file system (i can see it
from filemon) I just have no more ideas perhaps someone else has...