linux php sftp实现跨服务器文件传输

linux php sftp实现跨服务器文件传输,第1张

linux php sftp实现跨服务器文件传输 什么是SFTP?SFTP 与 FTP 区别终端使用sftpsftp可用命令php 中通过SSH2 扩展模块 使用SFTP安装php SSH2 扩展 编写SFTP使用类结语

什么是SFTP? SFTP(Secure File Transfer Protocol)是一种安全的文件传输协议,一种通过网络传输文件的安全方法;它确保使用私有和安全的数据流来安全地传输数据,SFTP作为SSH的一部分,包含在SSH软件中。SFTP自身没有独立的守护进程,需要使用sshd守护进程来完成服务器的连接和文件传输。由于SFTP作为SSH的一个组件存在,SFTP能够直接使用SSH加密通道来传输数据。SFTP要求客户端用户必须由服务器进行身份验证,并且数据传输必须通过安全通道(SSH)进行,即不传输明文密码或文件数据。它允许对远程文件执行各种 *** 作,有点像远程文件系统协议。在静态资源,数据库远程维护的过程中,经常需要和本机进行数据的交互,常用的交互方式为ftp,但是这种方式需要确保21端口和ftp服务都存在。在远程访问服务器的时候大部分使用ssh来进行连接,其使用的端口为22端口,与之共用的数据传输方式为sftp,此种加密数据传输方式更加稳妥方便。centos 系统自带的internal-sftp 服务,查看sftp服务是否启动 ps -ef |grep sftp
SFTP 与 FTP 区别

安全通道
FTP 不提供任何安全通道来在主机之间传输文件;而SFTP协议提供了一个安全通道,用于在网络上的主机之间传输文件。

使用的协议
FTP使用TCP / IP协议。而,SFTP是SSH协议的一部分。

链接方式
FTP使用TCP端口21上的控制连接建立连接。而,SFTP是在客户端和服务器之间通过SSH协议(TCP端口22)建立的安全连接来传输文件,SFTP 基于SSH来加密传输文件,可靠性高,可断点续传。

安全性
FTP密码和数据以纯文本格式发送,大多数情况下是不加密的,安全性不高。而,SFTP会在发送之前加密数据,二进制的形式传递,是无法“按原样”阅读的,安全性较高。

效率
由于FTP数据无需加密,明文传输,效率比SFTP较高。

终端使用sftp useradd -s /sbin/nologin -M dalei # 创建用户 dalei,并禁止ssh登录,不创建家目录passwd 123456 #设置用户密码mkdir /sftp #创建sftp根目录,所有sftp用户都将在该目录下活动。设置目录权限,目录的权限设定有两个要点:

目录开始一直往上到系统根目录为止的目录拥有者都只能是root
目录开始一直往上到系统根目录为止都不可以具有群组写入权限

chown root:root /sftp
chmod 755 /sftp 
创建用户dalei根目录,目录名为用户名
cd /sftp
mkdir dalei
设置dalei目录属组权限
chown root:dalei /sftp/dalei

(注:设置用户dalei,如果设置拥有者为root,表示该目录dalei没有权限读写,在该目录下建立其它目录,赋权给dalei用户读写权限;若需要对该目录拥有读写权限,设置权限:chown dalei:dalei /sftp/dalei
7. chmod 755 /sftp/dalei #这里的目录dalei 权限也只能是755,否则无法限制目录。
8. 配置sshd_config

vi /etc/ssh/sshd_config
#注释掉下面一行
#Subsystem sftp /usr/libexec/openssh/sftp-server

#添加这行:
Subsystem sftp internal-sftp  #指定使用sftp服务使用系统自带的internal-sftp 
Match User dalei        #匹配用户,如果要匹配多个组,多个组之间用逗号分割
ChrootDirectory /sftp/%u   #用chroot将指定用户的根目录,这里的%u指的是账号名
ForceCommand internal-sftp #指定sftp命令
X11Forwarding no           #这两行,如果不希望该用户能使用端口转发的话就加上,否则删掉
AllowTcpForwarding no

%u指的是账号名,/sftp/%u 含义是:/sftp/dalei

重启sshd服务器
systemctl restart sshd.service   #重启sshd
systemctl status sshd.service    #查询sshd启动状态
dalei用户登录测试:
sftp -oPort=22 [email protected]

这里我使用的是lj这个用户登录

至此已经完成了sftp创建用户,并修改权限,限制根目录。

在用户登录测试过程中,可能会遇到如下错误:

     Couldn't read packet: Connection reset by peer

这是什么原因导致的呢,是目录权限导致了该问题,具体解决方案,回看以上4、5、6步骤!

sftp可用命令

下表展示的为sftp所支持的命令:

exit和quit
退出sftp服务器,退回到本机。put -r 文件名 上传文件夹下所有子文件 php 中通过SSH2 扩展模块 使用SFTP 检测php是否安装ssh2模块 php -m

如果已经安装 php -m 就会出现如上图中的ssh2,安装过可跳过下面步骤 安装php SSH2 扩展

下载包
wget http://www.libssh2.org/download/libssh2-1.8.2.tar.gz
wget http://pecl.php.net/get/ssh2-1.2.tgz

先安装 libssh2 再安装 ssh2, 因为ssh2模块依赖libssh2

tar -zxvf libssh2-1.8.2.tar.gz
cd libssh2-1.8.2
./configure --prefix=/usr/local/src/libssh2-1.8.2
make && make install
编译安装ssh2
tar -zxvf ssh2-1.2
cd ssh2-1.2
phpize
./configure --prefix=/usr/local/src/ssh2-1.2 --with-ssh2=/usr/local/src/libssh2-1.8.2 --with-php-config=/usr/local/php/bin/php-config
make && make install
修改php.ini 加入 ssh2.so 动态链接库文件
编写SFTP使用类


class Sftp
{
        // 初始配置为NULL
        private $config = NULL;

        // 连接为NULL
        private $conn = NULL;

        //sftp resource
        private $ressftp = NULL;

        private $remote_dir = '/xm';

        // 初始化
        public function __construct($config)
        {
                $this->config = $config;
                $this->connect();
        }
		// SFTP连接
        public function connect()
        {
                $this->conn = ssh2_connect($this->config['host'], $this->config['port']);
                if( ssh2_auth_password($this->conn, $this->config['username'], $this->config['password']))
                {
                        $this->ressftp = ssh2_sftp($this->conn);
                }else{
                        echo "用户名或密码错误";
                }

        }

        // 下载文件
        public function downftp($remote, $local)
        {
                return copy("ssh2.sftp://".intval($this->ressftp).$remote, $local);
        }

        // 文件上传
        public function upftp( $local,$remote, $file_mode = 0777)
        {
                return copy($local,"ssh2.sftp://".intval($this->ressftp).$remote);
        }

        //创建目录
        public function ssh2_sftp_mchkdir($path)  //使用创建目录循环
        {
                ssh2_sftp_mkdir($this->ressftp, $path,0777);
        }

        //判段目录是否存在
        public function ssh2_dir_exits($dir){
                return file_exists("ssh2.sftp://".intval($this->ressftp).$dir);
        }
		//文件夹上传
		public function copy_dir($file_path,$count=0)
		{
		    $dir = opendir($file_path);
		
		    $String =  strstr($file_path,'php_test');
		    $length = strlen('php_test');
		    $remote = substr($String, $length);
		
		
		    if($count > 0){
		        #$ml = substr($file_path, strrpos($file_path,'/')+1);
			    if(is_dir($file_path)){
			        $remote_dir = $this->remote_dir.$remote;
			        #echo $remote_dir.'G'.PHP_EOL;
			        if(!$this->ssh2_dir_exits($remote)){
			                $this->ssh2_sftp_mchkdir($remote_dir);
			                #$this->remote_dir = $this->remote_dir.$ml.'/';
			
			        }
			    }
			}
		    while (false !== ($file = readdir($dir))) {
		        if (($file != '.') && ($file != '..')) {
		           # echo $file_path.'/'.$file.PHP_EOL;
		            if(is_dir($file_path.'/'.$file)){
		                $this->copy_dir($file_path.'/'.$file,++$count);
		            }else{
		                #echo $file.PHP_EOL;
		               copy($file_path . '/' . $file, "ssh2.sftp://".intval($this->ressftp) . $this->remote_dir.$remote. '/' . $file);
		            }
		         }
		    }
		    closedir($dir);
		}
		

}



$config = array(

'host'=>'192.168.227.132',

'username'=>'lj',

'password'=>'123456',

'port'=>'22'

);

$sftp = new sftp($config);
//文件上传
$sftp->copy_dir("/home/dalei/php_test");
//检测文件是否存在
$re = $sftp->ssh2_dir_exits("/xm");
if($re){//如果目录存在直接上传
        $sftp->upftp("/home/dalei/php_test/cs.html","/xm/cs.html");
}else{
        $sftp->ssh2_sftp_mchkdir("/xm");
        $sftp->upftp("/home/dalei/php_test/cs.html","/xm/cs.html");
}


结语

自此就可以方便使用sfpt上传同步文件了,当然你还可以使用 SSH2 模块 扩展更多功能。

如有错误请指正!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/928868.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-17
下一篇 2022-05-17

发表评论

登录后才能评论

评论列表(0条)

保存