ssh2_auth_pubkey_file身份验证始终失败

Jam*_*kby 11 php ssh ssh-keys dsa private-key

我正在尝试使用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";
    }
Run Code Online (Sandbox Code Playgroud)

但显示的错误是: -

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

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

任何人都可以建议修复吗?

Jam*_*kby 12

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

我不喜欢将ssh密钥保存在web文件夹中的想法www-data,所以我将密钥文件移动到新用户的主目录(/home/keyuser/)然后使它们可访问www-data.身份验证成功.

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

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

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