PHP ssh2_connect到远程无密码服务器

时间:2017-09-05 04:12:42

标签: php ssh sftp

我在本地客户端和远程服务器之间设置了SSH无密码登录。

本地客户端具有私钥,远程服务器添加了本地客户端的公钥。

本地客户端能够SSH / SFTP到远程服务器而无需密码 - > sftp remoteusr @ server

---on local client machine---
private key location -> /home/localusr/.ssh/id_rsa (owner:group = localusr:localusr)
public key path -> /home/localusr/.ssh/id_rsa.pub (owner:group = localusr:localusr)

---on remote server---
added client public key to /home/remoteusr/.ssh/authorized_keys

我无法使用本地客户端的PHP建立SFTP连接

$conn = ssh2_connect($server, $port, array('hostkey'=>'ssh-rsa'));

if (ssh2_auth_pubkey_file($conn, 'remoteusr', '/home/localusr/.ssh/id_rsa.pub', '/home/localusr/.ssh/id_rsa')) {
    echo "Public Key Authentication Successful";
} else {
    die('Public Key Authentication Failed');
}

我遇到了这个错误

PHP Warning:  ssh2_auth_pubkey_file(): Authentication failed for 
remoteusr using public key: Unable to open public key file in 
xxx.php on line 29

这是由于权限问题,我认为apache服务器无权读取密钥。能够通过从

复制私钥和公钥来使其工作
/home/localusr/.ssh/id_rsa
/home/localusr/.ssh/id_rsa.pub

/var/www/project/id_rsa
/var/www/project/id_rsa.pub
and chmod 700

0 个答案:

没有答案