ssh2_auth_pubkey_file身份验证始终失败

时间:2014-05-27 02:10:41

标签: php ssh ssh-keys private-key dsa

我尝试使用PHP的ssh2功能连接到另一台机器。我知道ssh密钥是在没有密码的情况下创建的并且分发正确,我可以ssh user@host在我的机器终端上到服务器。

PHP函数尝试使用ssh密钥文件连接到ip地址: -

 function minnerConnect($miner_serial) {

    $port = '7822';
    $miner_ip = $this->getMinerIp($miner_serial);

    $methods = array(
        'kex' => 'diffie-hellman-group1-sha1',
        'hostkey' => 'ssh-dss',
        'client_to_server' => array(
            'crypt' => '3des-cbc',
            'mac' => 'hmac-md5',
            'comp' => 'none'),
        'server_to_client' => array(
            'crypt' => '3des-cbc',
            'mac' => 'hmac-md5',
            'comp' => 'none'));
    $connection = ssh2_connect($miner_ip, $port, $methods);
    if (ssh2_auth_pubkey_file($connection, 'root',
        '/root/.ssh/id_dsa.pub',
        '/root/.ssh/id_dsa','')) {
      echo "Public Key Authentication Successful\n";
    } else {
      echo "Public Key Authentication Failed";
    }

但显示的错误是: -

  

(!)警告:ssh2_auth_pubkey_file():使用公钥的root身份验证失败:第95行/var/www/application/models/miner_model.php中的回调返回错误

第95行是'/root/.ssh/id_dsa','')) {

有人可以建议修复吗?

1 个答案:

答案 0 :(得分:10)

这种情况下的错误是密钥是由root用户生成的,但是它们需要由Web服务器组/所有者www-data访问。

我不喜欢将ssh密钥保存在www-data的Web文件夹中,因此我将密钥文件移动到新用户的主目录(/home/keyuser/然后让www-data访问它们。身份验证成功。

即使原始错误说它找到了该文件,它也无法读取该文件。

更好的调试方法是尝试通过php读取文件:

$prv_key = file_get_contents('/var/www/application/files/id_dsa');
print "<pre>";
var_export($prv_key);
print "</pre>";