SFTP远程文件上传

远程服务器remote_host=192.168.29.142
用户为remote_www,
用户当前目录为/home/remote_www


本地服务器local_host=192.168.29.135
用户为local_www
用户当前目录为/home/local_www

1.首先查看在本地服务器用户当前目录下是否有.ssh目录,即是否存在/home/local_www/.ssh/  如果没有.ssh目录,则创建:
$mkdir .ssh

local_host机下生成公钥/私钥对。
$ssh-keygen -t rsa -P ''
-t表示密钥类型
-P表示密码,-P '' 就表示空密码,也可以不用-P参数,这样就要三车回车,用-P就一次回车。
该命令将在/local_www/.ssh目录下面产生一对密钥id_rsaid_rsa.pub
一般采用的sshrsa密钥:
id_rsa     私钥
id_rsa.pub 公钥
下述命令产生不同类型的密钥
ssh-keygen -t dsa
ssh-keygen -t rsa
ssh-keygen -t rsa1

Generating public/private rsa key pair.
生成公钥/私钥rsa密钥对。

Enter file in which to save the key (/home/local_www/.ssh/id_rsa): 
输入要保存密钥的文件

Your identification has been saved in /home/local_www/.ssh/id_rsa.
您的身份已保存在/home/local_www/.ssh/id_rsa中。

Your public key has been saved in /home/local_www/.ssh/id_rsa.pub.
您的公开金钥已储存在/home/local_www/.ssh/id_rsa.pub中。

The key fingerprint is:
关键指纹是:

ac:a7:4b:bd:3e:ae:a6:de:0f:b8:43:fe:c3:24:2d:b5 www@gold-dev002
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|      ..         |
|     o .S        |
|    +.Eo         |
|   o.=+ o        |
|    o+++..       |
|   .+=*B=.       |
+-----------------+


2.查看在远程sftp服务器用户当前目录是否有.ssh目录,即是否存在/home/remote_wwwr/.ssh/  如果没有.ssh目录,则创建:
$mkdir .ssh


3.将本地服务器生成的公钥上传到远程sftp服务器的/home/remote_www/.ssh/

$scp id_dsa.pub remote_www@remote_host:/home/remote_www/.ssh/


4.查看在远程sftp服务器用户当前目录的.ssh目录是否有authorized_keys文件  如果有则把新生成的公钥追加到authorized_keys

$cat id_dsa.pub >> authorized_keys
(id_rsa.pub的内容追加到authorized_keys)

如果没有则把公钥id_dsa.pub重命名为authorized_keys

$mv id_dsa.pub authorized_keys
$chmod 644 authorized_keys
修改文件的读写权限

5.测试在本地服务器上,以local_www登录
$sftp remote_www@remote_host如果成功则进入sftp服务器
sftp>
如果不成功,则提示要输入密码

简单测试代码:

function fileUpload($fileName)
{

    $sftp = 'ssh2.sftp://';

    //远程host
    $remoteServer = 'remote.com';

    //远程服务器端口
    $remoteServerPort = 22;

    //远程服务器用户名
    $remoteServerUsername = 'www';

    //本地公钥文件
    $pubkey = '/local/id_rsa.pub';

    //本地私钥文件
    $prikey = '/local/id_rsa';

    //连接SSH2服务器
    $resConnection = ssh2_connect($remoteServer, $remoteServerPort);
    if (!is_resource($resConnection)) {
        die('连接失败');
    }

    //上传到远程服务器的绝对目录
    $remotePath = '/data/other/' . $fileName;

    //本地服务器的绝对目录
    $localPath = '/data/other' . $fileName;

    //密钥身份校验
    if (ssh2_auth_pubkey_file($resConnection, $remoteServerUsername, $pubkey, $prikey)) {
        //初始化SFTP子系统
        $resSFTP = ssh2_sftp($resConnection);
        if (!is_resource($resSFTP)) {
            die('初始化SFTP子系统');
        }
    } else {
        die('密钥身份校验');
    }

    if (!file_exists($sftp . $resSFTP . $remotePath)) {
        $mkdir = ssh2_sftp_mkdir($resSFTP, $remotePath, 0755, true);
        if (!$mkdir) {
            die('文件夹创建失败');
        }
    }

    //通过SCP发送文件
    $sendbol = ssh2_scp_send($resConnection, $localPath, $remotePath, 0777);
    if (!$sendbol) {
        die('上传文件失败');
    }

    //没有关闭SSH会话,内部缓冲区不会被刷新,文件将不写入到磁盘
    ssh2_exec($resConnection, 'exit');

    return true;
}

  

原文地址:https://www.cnblogs.com/wangyulu/p/6537918.html