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较高。
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 sftpuser@127.0.0.1
这里我使用的是lj这个用户登录
至此已经完成了sftp创建用户,并修改权限,限制根目录。
在用户登录测试过程中,可能会遇到如下错误:
Couldn't read packet: Connection reset by peer
这是什么原因导致的呢,是目录权限导致了该问题,具体解决方案,回看以上4、5、6步骤!
sftp可用命令下表展示的为sftp所支持的命令:
退出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 模块 扩展更多功能。
如有错误请指正!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)