note 94355 added to function.ssh2-auth-pubkey-file
| From: | gramodotgnuatgmaildotcom at osu1 dot php dot net | Date: | Thu, 29 Oct 2009 23:26:46 +0000 |
| Subject: | note 94355 added to function.ssh2-auth-pubkey-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162269@lists.php.net to get a copy of this message | ||
I had an anoyiing problem with this function, everytime I tried to make it run it responds with an
Authentication failed for <user> using public key
but when I try directly to connect to the server using
ssh <user>@<domain>
Things works fine...
After lots of intents I realize that local files were read protected from user apache (they were
stored at /home/<user>/.ssh directory)
So, if you also have this problem, just make a new directory into a place where apache can read and
place there the keys.
The whole thing I do is as follows (Linux & Apache both server and client):
---------8<------------------8<------------------8<---------
First I create a directory where apache can read
mkdir /home/<user>/.newssh_keys
chmod 777 /home/<user>/.newssh_keys
(This is a security issue, so maybe you need to realize how to make it safer.)
---------8<------------------8<------------------8<---------
Then I create keys into local server choosing /home/<user>/.newssh_keys/id_dsa as the file to
save the key:
ssh-keygen -t dsa
...
Enter file in which to save the key (/home/<user>/.ssh/id_dsa):
/home/<user>/.newssh_keys/id_dsa
...
Then I have to change permissions to private key
(This is a security issue, so maybe you need to realize how to make it safer.)
chmod 644 /home/<user>/.newssh_keys/id_dsa
---------8<------------------8<------------------8<---------
I copy the public key into the server's .ssh directory
client$ scp id_dsa.pub <remoteuser>@<server_domain>:~/.ssh/
and then I connect myself to the server using traditional ssh in order to append the public key at
the end of authorized_keys2 file
server$ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys2
and remove the public key in order to be clean
server$ rm ~/.ssh/id_dsa.pub
---------8<------------------8<------------------8<---------
Finally I use this code into my php script
//----------------------------------------
// This in order to be sure apache can read public key
// (remove this after debug)
$pub_key = file_get_contents('/home/<user>/.newssh_keys/id_dsa.pub');
print "<pre>";
var_export($pub_key);
print "</pre>";
//----------------------------------------
// This in order to check private one
// (remove this after debug)
$prv_key = file_get_contents('/home/<user>/.newssh_keys/id_dsa');
print "<pre>";
var_export($prv_key);
print "</pre>";
//----------------------------------------
// Finally the connection code
$connection = ssh2_connect('<server_domain>', 22, array('hostkey',
'ssh-dss'));
if (ssh2_auth_pubkey_file($connection, '<server_user>',
'/home/<user>/.newssh_keys/id_dsa.pub',
'/home/<user>/.newssh_keys/id_dsa')) {
echo "Public Key Authentication Successful\n";
} else {
echo "Public Key Authentication Failed";
}
---------8<------------------8<------------------8<---------
----
Server IP: 69.147.83.197
Probable Submitter: 186.80.6.34 (proxied: 186.80.6.34)
----
Manual Page -- http://www.php.net/manual/en/function.ssh2-auth-pubkey-file.php
Edit -- https://master.php.net/note/edit/94355
Del: integrated -- https://master.php.net/note/delete/94355/integrated
Del: useless -- https://master.php.net/note/delete/94355/useless
Del: bad code -- https://master.php.net/note/delete/94355/bad+code
Del: spam -- https://master.php.net/note/delete/94355/spam
Del: non-english -- https://master.php.net/note/delete/94355/non-english
Del: in docs -- https://master.php.net/note/delete/94355/in+docs
Del: other reasons-- https://master.php.net/note/delete/94355
Reject -- https://master.php.net/note/reject/94355
Search -- https://master.php.net/manage/user-notes.php