Php ssh2\u auth\u pubkey\u文件身份验证总是失败

Php ssh2\u auth\u pubkey\u文件身份验证总是失败,php,ssh,ssh-keys,private-key,dsa,Php,Ssh,Ssh Keys,Private Key,Dsa,我正在尝试使用PHP的ssh2函数连接到另一台机器。我知道ssh密钥是在没有密码的情况下创建的,并且分布正确,我可以sshuser@host在我的机器上的终端连接到服务器 PHP函数尝试使用ssh密钥文件连接到ip地址:- function minnerConnect($miner_serial) { $port = '7822'; $miner_ip = $this->getMinerIp($miner_serial); $methods = array(

我正在尝试使用PHP的ssh2函数连接到另一台机器。我知道ssh密钥是在没有密码的情况下创建的,并且分布正确,我可以
sshuser@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():使用公钥对根用户进行身份验证失败:回调在第95行的/var/www/application/models/miner_model.php中返回错误

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


有人能提出解决方案吗?

本例中的错误是,密钥由root用户生成,但需要web服务器组/所有者
www data
访问密钥

我不喜欢将ssh密钥保存在web文件夹中的想法,该文件夹对
www-data
开放,因此我将密钥文件移动到新用户的主目录(
/home/keyuser/
),然后使其可供
www-data
访问。身份验证成功

尽管最初的错误是说它找到了文件,但它无法读取该文件

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

$prv_key=file_get_contents('/var/www/application/files/id_dsa');
打印“”;
var_出口(prv_密钥);
打印“”;

请注意,试图使用密码实现此功能的用户。由于存在错误(),私钥上设置的密码无法实现此功能。注意2,如果您仍然无法实现此功能,则可能是密钥格式,我使用ssh-keygen-m PEM-t rsa解决了此问题。;)