《搭建FTP服务器》

1.CentOS 7.2 搭建FTP 服务器

  • 内网离线安装FTP Server
  • 外网在线安装FTP Server

注意:所有的安装方式都必须先进行设置快照(出现异常可以及时恢复到之前设置快照的环境)

(1)内网离线安装

前概要:两台服务器,因为没有实现网络互通,所以需要一个文件交换的服务。就出现了这个方案来实现两个服务器系统内部文件的交互。

  • A 服务器 Windows Server 2012 R2 x64
  • B 服务器 Windows Server 2008 R2 x64
  • C 堡垒机 CentOS 7.2 1511 x64

因为之前有过物理隔绝的问题。但是出现了公司的文件交换设备(软件:前提是两边服务器网络畅通需要使用真实IP地址)

目前使用两种方案实现我们的文件传输

  • 使用vsftpd
  • 使用pure-ftpd
  1. vsftpd安装方式(创建系统用户)测试服务器

    下载地址服务器OS

#这里使用的方式是使用yum下载软件及其依赖项
#环境搭建:搭建两台与服务器相同环境的服务器(内核版本、发行版本、。。。)
#首先在搭建的服务器上下载软件包

# 使用第一台服务器(使用root用户登陆)
# 1.更新yum源
yum update -y
# 2.下载vsftpd安装包(这里选择的是将下载下来的软件包存放在/home/vsftp目录中)
yum install --downloadonly --downloaddir=/home/vsftp vsftpd
# 3.进入到/home/vsftp中
cd /home/vsftp

# 这里使用FTP软件(FileZilla)取出vsftp文件夹

# 使用第二台服务器(使用root用户登陆)
# 1.使用(fileZilla)传输vsftp到/home目录下
# 2.创建ftp文件保存根目录
mkdir /home/ftp_root
# 3.创建用户组
groupadd test_group
# 4.为新建的用户组内添加用户 (以user1用户为例)
useradd -g test_group user1
# 5.为user1设置密码
passwd user1
# 6.修改文件夹所属权限
chown user1:test_group /home/ftp_root
# 7.修改文件夹读写权限
chmod o-rx /home/ftp_root
# 8.进入到vsftp目录下进行vsftpd软件包安装(vsftpd-3.0.2-27.el7.x86_64.rpm为软件包名)
rpm -ivh /home/ftp/vsftpd-3.0.2-27.el7.x86_64.rpm
# 9.修改vsftpd配置文件vsftpd.conf(只修改:anonymous_enable=NO)
vim /etc/vsftpd/vsftpd.conf
# 10.启动vsftpd
service vsftpd start
# 11.打开浏览器访问(以127.0.0.1为例)或者使用 FileZilla 访问测试文件的上传和下载
ftp://127.0.0.1


# 总结:本操作教程使用Linux系统用户登陆FTP。可以使用登陆FTP的账号登陆Linux操作系统。
#      登陆FTP后可以操作系统内绝大多数文件(这里默认是关闭防火墙)

  1. pure-ftpd安装方式(虚拟用户)测试服务器
#这里使用的方式是使用yum下载软件及其依赖项
#环境搭建:搭建两台与服务器相同环境的服务器(内核版本、发行版本、。。。)
#首先在搭建的服务器上下载软件包

# 使用第一台服务器(使用root用户登陆)
# 1.更新yum源
yum update -y
# 2.下载并安装扩展源
yum install --downloadonly --downloaddir=/home/epel-ftpd  epel-release
cd /home/epel-ftpd
yum localinstall *.rpm -y
# 3.下载pure-ftpd安装包(因为前面有扩展源的存在才可以正常下载pure-ftpd)
yum install --downloadonly --downloaddir=/home/pure-ftpd  pure-ftpd
# 3.进入到/home/vsftp中
cd /home/vsftp

# 这里使用FTP软件(FileZilla)取出vsftp文件夹

# 使用第二台服务器(使用root用户登陆)
# 1.使用(fileZilla)传输vsftp到/home目录下
# 2.开启防火墙并开放端口(开放FTP登陆端口 21 和文件传输端口范围 30000 ~ 50000)
systemctl start firewalld
firewall-cmd --permanent --zone=public --add-port=21/tcp 
firewall-cmd --permanent --zone=public --add-port=30000-50000/tcp
firewall-cmd --reload
# 3.查看端口情况
firewall-cmd --zone=public --list-ports
# 4.安装 pure-ftpd 软件包(进入到pure-ftpd目录下)
yum localinstall *.rpm -y
# 5.配置pure-ftpd(只修改三个地方余下部分默认)
vi /etc/pure-ftpd/pure-ftpd.conf
######################################
#	PureDB /etc/pure-ftpd/pureftpd.pdb #
#	PassivePortRange 30000 50000			 #
#	NoAnonymous yes										 #
#	Bind ip														 #
######################################
# 6.添加用户组(以test_group为例)
groupadd test_group
# 7.禁止用户组内成员登陆系统
useradd -g test_group -s /sbin/nologin -d /dev/null test_group
# 8.创建FTP文件存放目录(以pure_ftpd为例)
mkdir -p /home/pure_ftpd
# 9.修改文件夹拥有者
chown -R test_group:test_group /home/pure_ftpd
# 10.添加虚拟用户并设置密码(以user1用户为例)
pure-pw useradd user1 -u test_group -d /home/pure_ftpd -m
# 11.启动 pure-ftpd
systemctl start pure-ftpd
# 12.打开浏览器访问(以127.0.0.1为例)或者使用 FileZilla 访问测试文件的上传和下载
ftp://127.0.0.1

# 总结:本操作教程使用虚拟用户登陆FTP。不可以使用登陆FTP的账号登陆Linux操作系统。
#      登陆FTP后只可以操作FTP指定文件夹内的文件

(2)内网在线安装

  • 因为在线可以直接使用yum在线安装
  1. vsftpd
  2. pure-ftpd
# vsftpd
yum install vsftpd
# 其余配置与上面一直

# pure-ftpd
yum install epel-release
yum install pure-ftpd
# 其余配置与上面一直

# 总结:不需要搭建两台相同环境的服务器的过程

(3)网络环境测试(需要打开Windows OS的出站入站规则)

因为FTPClient是Windows OS所以可以使用如下命令对端口和IP测试(以192.168.3.120为例)

telnet 192.168.3.120 21

telnet 192.168.3.120 30000-50000

(4)防火墙的开启和关闭

  • 开启防火墙
systemctl start firewalld
  • 关闭防火墙
systemctl start firewalld
  • 开启指定端口(以21端口为例)
firewall-cmd --permanent --zone=public --add-port=21/tcp 
  • 关闭开放的端口(以21端口为例)
firewall-cmd --zone=public --remove-port=21/tcp --permanent
  • 重启防火墙(每次对防火墙做修改都需要重启防火墙)
firewall-cmd --reload

2.Java代码部分

前提概要:因为本次测试的代码部分需要作为一个组件嵌入到原有的系统中。

所以使用Eclipse 创建一个Java SE工程并将工程打包成一个可运行的jar就是一个SDK。

以下是Java代码部分

package ftp;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.TimeZone;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

/**
 * FTP工具类
 * @author wangxuwen
 *
 */
public class FTPUtils {
	private FTPClient ftpClient;
    private String strIp;
    private int intPort;
    private String user;
    private String password;
   
    public FTPUtils(String strIp, int intPort, String user, String Password) {
        this.strIp = strIp;
        this.intPort = intPort;
        this.user = user;
        this.password = Password;
        this.ftpClient = new FTPClient();
        this.ftpClient.setConnectTimeout(60*1000);
    }
    /**
     * @return 判断是否登入成功
     * */ 
    public boolean ftpLogin() {
        boolean isLogin = false;
        FTPClientConfig ftpClientConfig = new FTPClientConfig();
        ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
        this.ftpClient.setControlEncoding("UTF-8");
        this.ftpClient.configure(ftpClientConfig);
        try {
            if (this.intPort > 0) {
                this.ftpClient.connect(this.strIp, this.intPort);
            }else {
                this.ftpClient.connect(this.strIp);
            }
            // FTP服务器连接回答 
            int reply = this.ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                this.ftpClient.disconnect();
                System.out.println("登录FTP服务失败!");
                return isLogin;
            }
            this.ftpClient.login(this.user, this.password);
            // 设置传输协议 
            this.ftpClient.enterLocalPassiveMode();
            this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            System.out.println("恭喜" + this.user + "成功登陆FTP服务器");
            isLogin = true;
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println(this.user + "登录FTP服务失败!" + e.getMessage());
        }
        this.ftpClient.setBufferSize(1024 * 2);
        this.ftpClient.setDataTimeout(30 * 1000);
        return isLogin;
    }
   
    /**
     * @退出关闭服务器链接
     * */ 
    public void ftpLogOut() {
        if (null != this.ftpClient && this.ftpClient.isConnected()) {
            try {
                boolean reuslt = this.ftpClient.logout();// 退出FTP服务器 
                if (reuslt) {
                	System.out.println("成功退出服务器");
                }
            }catch (IOException e) {
                e.printStackTrace();
                System.out.println("退出FTP服务器异常!" + e.getMessage());
            }finally {
                try {
                    this.ftpClient.disconnect();// 关闭FTP服务器的连接 
                }catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("关闭FTP服务器的连接异常!");
                }
            }
        }
    }
   
    /**
     * 上传Ftp文件
     * @param localFile 当地文件
     * @param romotUpLoadePath上传服务器路径 - 应该以/结束
     * */ 
    public boolean uploadFile(File localFile, String romotUpLoadePath) {
        BufferedInputStream inStream = null;
        boolean success = false;
        try {
//        	this.ftpClient.setControlEncoding("UTF-8");
//        	this.ftpClient.enterLocalPassiveMode();
            this.ftpClient.changeWorkingDirectory(romotUpLoadePath);// 改变工作路径 
            inStream = new BufferedInputStream(new FileInputStream(localFile));
            System.out.println(localFile.getName() + "开始上传.....");
            success = this.ftpClient.storeFile(localFile.getName(), inStream);
            if (success == true) {
            	System.out.println(localFile.getName() + "上传成功");
                return success;
            }
        }catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println(localFile + "未找到");
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (inStream != null) {
                try {
                    inStream.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return success;
    }
   
    /***
     * 下载文件
     * @param remoteFileName   待下载文件名称
     * @param localDires 下载到当地那个路径下
     * @param remoteDownLoadPath remoteFileName所在的路径
     * */ 
   
    public boolean downloadFile(String remoteFileName, String localDires, 
            String remoteDownLoadPath) {
        String strFilePath = localDires + remoteFileName;
        BufferedOutputStream outStream = null;
        boolean success = false;
        try {
            this.ftpClient.changeWorkingDirectory(remoteDownLoadPath);
            outStream = new BufferedOutputStream(new FileOutputStream( 
                    strFilePath));
            System.out.println(remoteFileName + "开始下载....");
            success = this.ftpClient.retrieveFile(remoteFileName, outStream);
            if (success == true) {
            	System.out.println(remoteFileName + "成功下载到" + strFilePath);
                return success;
            }
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println(remoteFileName + "下载失败");
        }finally {
            if (null != outStream) {
                try {
                    outStream.flush();
                    outStream.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (success == false) {
        	System.out.println(remoteFileName + "下载失败!!!");
        }
        return success;
    }
   
    /***
     * @下载文件夹
     * @param localDirectoryPath本地地址
     * @param remoteDirectory 远程文件夹
     * */ 
    public boolean downLoadDirectory(String localDirectoryPath,String remoteDirectory) {
        try {
            String fileName = new File(remoteDirectory).getName();
            localDirectoryPath = localDirectoryPath + "/" + fileName + "/";
            new File(localDirectoryPath).mkdirs();
            this.ftpClient.enterLocalPassiveMode();
            FTPFile[] allFile = this.ftpClient.listFiles(remoteDirectory);
            for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
                if (!allFile[currentFile].isDirectory()) {
                    downloadFile(allFile[currentFile].getName(),localDirectoryPath, remoteDirectory);
                }
            }
        }catch (IOException e) {
            e.printStackTrace();
            System.out.println("下载文件夹失败");
            return false;
        }
        return true;
    }
}

package ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * FTP传输上报文件
 * 
 * @author wangxuwen
 * @date 2020/07/27
 */
public class FTPMain {

	public static void main(String[] args) throws IOException {

		Properties properties = new Properties();
		properties.load(new FileInputStream("config/config.properties"));
		String ip = properties.getProperty("ip");
		int port = Integer.parseInt(properties.getProperty("port"));
		String userName = properties.getProperty("userName");
		String passWd = properties.getProperty("passWd");
		String uploadLoaclPath = properties.getProperty("uploadLoaclPath");
		String uploadRemotePath = properties.getProperty("uploadRemotePath");
		String downloadLoaclPath = properties.getProperty("downloadLoaclPath");
		String downloadRemotePath = properties.getProperty("downloadRemotePath");
		String flag = properties.getProperty("flag");
		FTPUtils ftp = new FTPUtils(ip, port, userName, passWd);
		
		ftp.ftpLogin();
		
		if("NO".equals(flag)) {
			ftp.uploadFile(new File(uploadLoaclPath), uploadRemotePath);
		}
		
		if("YES".equals(flag)) {
			ftp.downLoadDirectory(downloadLoaclPath, downloadRemotePath);
		}

		ftp.ftpLogOut();
	}

}

配置文件Properties-例子

#FTP服务器目标IP
ip=111.229.168.58
#FTP服务器端口
port=21
#FTP用户名
userName=test
#FTP密码
passWd=123
#FTP本地上传文件路径(绝对路径)
uploadLoaclPath=/Users/wangxuwen/Desktop/upload/upload100.txt
#FTP远程文件夹(相对路径)
uploadRemotePath=/upload
#FTP本地下载文件夹(绝对路径)
downloadLoaclPath=/Users/wangxuwen/Desktop
#FTP远程文件夹(相对路径)
downloadRemotePath=/download
#开启上传或下载(YES=下载、NO上传)
flag=NO

打包出一个可运行的Jar后,拿到A 服务器上测试 C 堡垒机。

文件结构如下:

ftp-test //应用根目录
|–ftp-test.jar //可运行的jar
|–|config
| |–config.properties //配置文件

# 将ftp-test文件放在A 服务器上 使用 java -jar命令运行
cd ftp-test
java -jar ftp-test.jar

3.测试过程中出现的异常

  • config.properties文件
  • Windows与Mac 目录斜杠不一样
  • 现象:两台服务器(内网环境)Windows无法访问CentOS的FTP服务,其他连接内网可以。

Properties文件需要使用ISO8859-1的编码方式设置整个文件不然会报错

Windows 斜杠 “\”,Mac 斜杠 “/”。

# 现象:两台服务器(内网环境)Windows无法访问CentOS的FTP服务,其他连接内网可以。
使用windows 

4.pure-ftpd配置文件详细文档

# vi pure-ftpd.conf
############################################################
#                                                          #
#         Configuration file for pure-ftpd wrappers        #
#                                                          #
############################################################

# If you want to run Pure-FTPd with this configuration   
# instead of command-line options, please run the
# following command :
#
# /usr/local/pureftpd/sbin/pure-config.pl /usr/local/pureftpd/etc/pure-ftpd.conf
#
# Please don't forget to have a look at documentation at
# http://www.pureftpd.org/documentation.shtml for a complete list of
# options.

# Cage in every user in his home directory
# 锁定所有用户到家目录中
ChrootEveryone              yes



# If the previous option is set to "no", members of the following group
# won't be caged. Others will be. If you don't want chroot()ing anyone,
# just comment out ChrootEveryone and TrustedGID.
# 信任组ID100,可以不锁定
# TrustedGID                    100



# Turn on compatibility hacks for broken clients
# 兼容不同客户端
BrokenClientsCompatibility  no



# Maximum number of simultaneous users
# 最大的客户端数量
MaxClientsNumber            50



# Fork in background
# 后台运行
Daemonize                   yes



# Maximum number of sim clients with the same IP address
# 每个ip最大连接数
MaxClientsPerIP             8



# If you want to log all client commands, set this to "yes".
# This directive can be duplicated to also log server responses.
# 记录日志
VerboseLog                  no



# List dot-files even when the client doesn't send "-a".
# 显示隐藏文件
DisplayDotFiles             no



# Don't allow authenticated users - have a public anonymous FTP only.
# 只允许匿名用户访问
AnonymousOnly               no



# Disallow anonymous connections. Only allow authenticated users.
# 不允许匿名用户
NoAnonymous                 yes



# Syslog facility (auth, authpriv, daemon, ftp, security, user, local*)
# The default facility is "ftp". "none" disables logging.
# 设置日志的告警级别,默认为ftp,none是禁止记录日志
SyslogFacility              ftp



# Display fortune cookies
# 定制用户登陆后的显示信息
# FortunesFile              /usr/share/fortune/zippy



# Don't resolve host names in log files. Logs are less verbose, but 
# it uses less bandwidth. Set this to "yes" on very busy servers or
# if you don't have a working DNS.
# 是否在日志文件中进行主机名解析,不进行客户端DNS解析
DontResolve                 yes



# Maximum idle time in minutes (default = 15 minutes)
# 最大空闲时间
MaxIdleTime                 30



# LDAP configuration file (see README.LDAP)
# LDAP 配置文件路径
# LDAPConfigFile                /etc/pureftpd-ldap.conf



# MySQL configuration file (see README.MySQL)
# MySQL 配置文件路径
 MySQLConfigFile              /usr/local/pureftpd/etc/pureftpd-mysql.conf


# Postgres configuration file (see README.PGSQL)
# Postgres 配置文件路径
# PGSQLConfigFile               /etc/pureftpd-pgsql.conf


# PureDB user database (see README.Virtual-Users)
# PureDB 用户数据库路径
 PureDB                        /usr/local/pureftpd/etc/pureftpd.pdb


# Path to pure-authd socket (see README.Authentication-Modules)
#  pure-authd 的socket 路径
# ExtAuth                       /var/run/ftpd.sock



# If you want to enable PAM authentication, uncomment the following line
# 如果你要启用 PAM 认证方式, 去掉下面行的注释
# PAMAuthentication             yes



# If you want simple Unix (/etc/passwd) authentication, uncomment this
# 如果你要启用 简单的 Unix系统 认证方式(/etc/passwd), 去掉下面行的注释
# UnixAuthentication            yes



# Please note that LDAPConfigFile, MySQLConfigFile, PAMAuthentication and
# UnixAuthentication can be used only once, but they can be combined
# together. For instance, if you use MySQLConfigFile, then UnixAuthentication,
# the SQL server will be asked. If the SQL authentication fails because the
# user wasn't found, another try # will be done with /etc/passwd and
# /etc/shadow. If the SQL authentication fails because the password was wrong,
# the authentication chain stops here. Authentication methods are chained in
# the order they are given. 



# 'ls' recursion limits. The first argument is the maximum number of
# files to be displayed. The second one is the max subdirectories depth
# 'ls' 命令的递归限制。第一个参数给出文件显示的最大数目。第二个参数给出最大的子目录深度。
LimitRecursion              10000 8



# Are anonymous users allowed to create new directories ?
# 是否允许匿名用户创建新目录
AnonymousCanCreateDirs      no



# If the system is more loaded than the following value,
# anonymous users aren't allowed to download.
# 超出负载后禁止下载
MaxLoad                     4



# Port range for passive connections replies. - for firewalling.
# 被动模式的端口范围
# PassivePortRange          30000 50000



# Force an IP address in PASV/EPSV/SPSV replies. - for NAT.
# Symbolic host names are also accepted for gateways with dynamic IP
# addresses.
# 强制一个IP地址使用被动响应
# ForcePassiveIP                192.168.0.1



# Upload/download ratio for anonymous users.
# 匿名用户的上传/下载的比率
# AnonymousRatio                1 10



# Upload/download ratio for all users.
# This directive superscedes the previous one.
# 所有用户的上传/下载的比率
# UserRatio                 1 10



# Disallow downloading of files owned by "ftp", ie.
# files that were uploaded but not validated by a local admin.
# 禁止下载匿名用户上传但未经验证的文件
AntiWarez                   yes



# IP address/port to listen to (default=all IP and port 21).
# 服务监听的IP 地址和端口。(默认是所有IP地址和21端口)
# Bind                      127.0.0.1,21



# Maximum bandwidth for anonymous users in KB/s
# 匿名用户带宽限制(KB)
# AnonymousBandwidth            8



# Maximum bandwidth for *all* users (including anonymous) in KB/s
# Use AnonymousBandwidth *or* UserBandwidth, both makes no sense.
# 所有用户的最大带宽(KB/s),包括匿名用户。
 UserBandwidth             1024



# File creation mask. <umask for files>:<umask for dirs> .
# 177:077 if you feel paranoid.
# 新建目录及文件的属性掩码值
Umask                       133:022



# Minimum UID for an authenticated user to log in.
# 认证用户允许登陆的最小组ID(UID) 
MinUID                      100



# Allow FXP transfers for authenticated users.
# 仅允许认证用户进行 FXP 传输。
AllowUserFXP                no



# Allow anonymous FXP for anonymous and non-anonymous users.
# 对匿名用户和非匿名用户允许进行匿名 FXP 传输
AllowAnonymousFXP           no



# Users can't delete/write files beginning with a dot ('.')
# even if they own them. If TrustedGID is enabled, this group
# will have access to dot-files, though.
# 不能删除/写入隐藏文件
ProhibitDotFilesWrite       no



# Prohibit *reading* of files beginning with a dot (.history, .ssh...)
# 禁止读取隐藏文件
ProhibitDotFilesRead        no



# Never overwrite files. When a file whose name already exist is uploaded,
# it get automatically renamed to file.1, file.2, file.3, ...
# 有同名文件时自动重新命名
AutoRename                  no



# Disallow anonymous users to upload new files (no = upload is allowed)
# 不允许匿名用户上传文件
AnonymousCantUpload         no



# Only connections to this specific IP address are allowed to be
# non-anonymous. You can use this directive to open several public IPs for
# anonymous FTP, and keep a private firewalled IP for remote administration.
# You can also only allow a non-routable local IP (like 10.x.x.x) to
# authenticate, and keep a public anon-only FTP server on another IP.
# 仅允许来自以下IP地址的非匿名用户连接。你可以使用这个指令来打开几个公网IP来提供匿名FTP,
# 而保留一个私有的防火墙保护的IP来进行远程管理。你还可以只允许一内网地址进行认证,而在另外
# 一个IP上提供纯匿名的FTP服务。
#
#TrustedIP                  10.1.1.1



# If you want to add the PID to every logged line, uncomment the following
# line.
# 如果你要为日志每一行添加 PID  去掉下面行的注释
#LogPID                     yes



# Create an additional log file with transfers logged in a Apache-like format :
# fw.c9x.org - jedi [13/Dec/1975:19:36:39] "GET /ftp/linux.tar.bz2" 200 21809338
# This log file can then be processed by www traffic analyzers.
# 使用类似于Apache的格式创建一个额外的日志文件
# AltLog                     clf:/var/log/pureftpd.log



# Create an additional log file with transfers logged in a format optimized
# for statistic reports.
# 使用优化过的格式为统计报告创建一个额外的日志文件
# AltLog                     stats:/var/log/pureftpd.log



# Create an additional log file with transfers logged in the standard W3C
# format (compatible with most commercial log analyzers)
# 使用标准的W3C格式创建一个额外的日志文件
# AltLog                     w3c:/var/log/pureftpd.log



# Disallow the CHMOD command. Users can't change perms of their files.
# 不接受 CHMOD 命令。用户不能更改他们文件的属性
#NoChmod                     yes



# Allow users to resume and upload files, but *NOT* to delete them.
# 允许用户恢复和上传文件,却不允许删除他们
#KeepAllFiles                yes



# Automatically create home directories if they are missing
# 用户主目录不存在的话,自动创建
CreateHomeDir               yes



# Enable virtual quotas. The first number is the max number of files.
# The second number is the max size of megabytes.
# So 1000:10 limits every user to 1000 files and 10 Mb.
# 限制用户可以创建的最大文件数和用户空间大小
Quota                       10000:10240



# If your pure-ftpd has been compiled with standalone support, you can change
# the location of the pid file. The default is /var/run/pure-ftpd.pid
# PID文件位置
#PIDFile                     /var/run/pure-ftpd.pid



# If your pure-ftpd has been compiled with pure-uploadscript support,
# this will make pure-ftpd write info about new uploads to
# /var/run/pure-ftpd.upload.pipe so pure-uploadscript can read it and
# spawn a script to handle the upload.
# Don't enable this option if you don't actually use pure-uploadscript.

# 如果你的 pure-ftpd 编译时加入了 pure-uploadscript 支持,这个指令将会使 pure-ftpd
# 发送关于新上传的情况信息到 /var/run/pure-ftpd.upload.pipe,这样 pure-uploadscript
# 就能读然后调用一个脚本去处理新的上传
#
#CallUploadScript yes



# This option is useful with servers where anonymous upload is 
# allowed. As /var/ftp is in /var, it save some space and protect 
# the log files. When the partition is more that X percent full,
# new uploads are disallowed.
# 文件所在磁盘的最大使用率
MaxDiskUsage               99



# Set to 'yes' if you don't want your users to rename files.
# 是否允许重命名文件(默认不允许)
#NoRename                  yes



# Be 'customer proof' : workaround against common customer mistakes like
# 'chmod 0 public_html', that are valid, but that could cause ignorant
# customers to lock their files, and then keep your technical support busy
# with silly issues. If you're sure all your users have some basic Unix
# knowledge, this feature is useless. If you're a hosting service, enable it.
# 打开以防止用户犯常识性错误
CustomerProof              yes



# Per-user concurrency limits. It will only work if the FTP server has
# been compiled with --with-peruserlimits (and this is the case on
# most binary distributions) .
# The format is : <max sessions per user>:<max anonymous sessions>
# For instance, 3:20 means that the same authenticated user can have 3 active
# sessions max. And there are 20 anonymous sessions max.
# 单个用户限制:每一个用户最大允许的进程;最大的匿名用户进程
# PerUserLimits            3:20



# When a file is uploaded and there is already a previous version of the file
# with the same name, the old file will neither get removed nor truncated.
# Upload will take place in a temporary file and once the upload is complete,
# the switch to the new version will be atomic. For instance, when a large PHP
# script is being uploaded, the web server will still serve the old version and
# immediatly switch to the new one as soon as the full file will have been
# transfered. This option is incompatible with virtual quotas.

# NoTruncate               yes



# This option can accept three values :
# 0 : disable SSL/TLS encryption layer (default).
# 1 : accept both traditional and encrypted sessions.
# 2 : refuse connections that don't use SSL/TLS security mechanisms,
#     including anonymous sessions.
# Do _not_ uncomment this blindly. Be sure that :
# 1) Your server has been compiled with SSL/TLS support (--with-tls),
# 2) A valid certificate is in place,
# 3) Only compatible clients will log in.

# TLS                      1


# List of ciphers that will be accepted for SSL/TLS connections
# Prefix with -S: in order to totally disable SSL but not TLS.

# TLSCipherSuite           HIGH:MEDIUM:+TLSv1:!SSLv2:+SSLv3



# Listen only to IPv4 addresses in standalone mode (ie. disable IPv6)
# By default, both IPv4 and IPv6 are enabled.

# IPV4Only                 yes



# Listen only to IPv6 addresses in standalone mode (ie. disable IPv4)
# By default, both IPv4 and IPv6 are enabled.

# IPV6Only                 yes

# UTF-8 support for file names (RFC 2640)
# Define charset of the server filesystem and optionnally the default charset
# for remote clients if they don't use UTF-8.
# Works only if pure-ftpd has been compiled with --with-rfc2640

# FileSystemCharset big5
# ClientCharset     big5

5.vsftpd配置文件详细文档

1.默认配置:
1>允许匿名用户和本地用户登陆。
     anonymous_enable=YES
     local_enable=YES
2>匿名用户使用的登陆名为ftp或anonymous,口令为空;匿名用户不能离开匿名  用户家目录/var/ftp,且只能下载不能上传。
3>本地用户的登录名为本地用户名,口令为此本地用户的口令;本地用户可以在自己家目录中进行读写操作;本地用户可以离开自家目录切换至有权限访问的其他目录,并在权限允许的情况下进行上传/下载。
    write_enable=YES
4>写在文件/etc/vsftpd.ftpusers中的本地用户禁止登陆。
2.配置文件格式:
vsftpd.conf 的内容非常单纯,每一行即为一项设定。若是空白行或是开头为#的一行,将会被忽略。内容的格式只有一种,如下所示
option=value
要注意的是,等号两边不能加空白。
3.匿名用户(anonymous)设置
anonymous_enable=YES/NO(YES)
控制是否允许匿名用户登入,YES 为允许匿名登入,NO 为不允许。默认值为YES。
write_enable=YES/NO(YES)
是否允许登陆用户有写权限。属于全局设置,默认值为YES。
no_anon_password=YES/NO(NO)
若是启动这项功能,则使用匿名登入时,不会询问密码。默认值为NO。
ftpftp_username=ftp
定义匿名登入的使用者名称。默认值为ftp。
anon_root=/var/ftp
使用匿名登入时,所登入的目录。默认值为/var/ftp。注意ftp目录不能是777的权限属性,即匿名用户的家目录不能有777的权限。
anon_upload_enable=YES/NO(NO)
如果设为YES,则允许匿名登入者有上传文件(非目录)的权限,只有在write_enable=YES时,此项才有效。当然,匿名用户必须要有对上层目录的写入权。默认值为NO。
anon_world_readable_only=YES/NO(YES)
如果设为YES,则允许匿名登入者下载可阅读的档案(可以下载到本机阅读,不能直接在FTP服务器中打开阅读)。默认值为YES。
anon_mkdir_write_enable=YES/NO(NO)
如果设为YES,则允许匿名登入者有新增目录的权限,只有在write_enable=YES时,此项才有效。当然,匿名用户必须要有对上层目录的写入权。默认值为NO。
anon_other_write_enable=YES/NO(NO)
如 果设为YES,则允许匿名登入者更多于上传或者建立目录之外的权限,譬如删除或者重命名。(如果anon_upload_enable=NO,则匿名用户 不能上传文件,但可以删除或者重命名已经存在的文件;如果anon_mkdir_write_enable=NO,则匿名用户不能上传或者新建文件夹,但 可以删除或者重命名已经存在的文件夹。)默认值为NO。
chown_uploads=YES/NO(NO)
设置是否改变匿名用户上传文件(非目录)的属主。默认值为NO。
chown_username=username
设置匿名用户上传文件(非目录)的属主名。建议不要设置为root。
anon_umask=077
设置匿名登入者新增或上传档案时的umask 值。默认值为077,则新建档案的对应权限为700。
deny_email_enable=YES/NO(NO)
若是启动这项功能,则必须提供一个档案/etc/vsftpd/banner_emails,内容为email address。若是使用匿名登入,则会要求输入email address,若输入的email address 在此档案内,则不允许进入。默认值为NO。
banned_email_file=/etc/vsftpd/banner_emails
此文件用来输入email address,只有在deny_email_enable=YES时,才会使用到此档案。若是使用匿名登入,则会要求输入email address,若输入的email address 在此档案内,则不允许进入。
4.本地用户设置
local_enable=YES/NO(YES)
控制是否允许本地用户登入,YES 为允许本地用户登入,NO为不允许。默认值为YES。
local_root=/home/username
当本地用户登入时,将被更换到定义的目录下。默认值为各用户的家目录。
write_enable=YES/NO(YES)
是否允许登陆用户有写权限。属于全局设置,默认值为YES。
local_umask=022
本地用户新增档案时的umask 值。默认值为077。
file_open_mode=0755
本地用户上传档案后的档案权限,与chmod 所使用的数值相同。默认值为0666。
5.欢迎语设置
dirmessage_enable=YES/NO(YES)
如果启动这个选项,那么使用者第一次进入一个目录时,会检查该目录下是否有.message这个档案,如果有,则会出现此档案的内容,通常这个档案会放置欢迎话语,或是对该目录的说明。默认值为开启。
message_file=.message
设置目录消息文件,可将要显示的信息写入该文件。默认值为.message。
banner_file=/etc/vsftpd/banner
当使用者登入时,会显示此设定所在的档案内容,通常为欢迎话语或是说明。默认值为无。如果欢迎信息较多,则使用该配置项。
ftpd_banner=Welcome to BOB's FTP server
这里用来定义欢迎话语的字符串,banner_file是档案的形式,而ftpd_banner 则是字符串的形式。预设为无。
6.控制用户是否允许切换到上级目录
在默认配置下,本地用户登入FTP后可以使用cd命令切换到其他目录,这样会对系统带来安全隐患。可以通过以下三条配置文件来控制用户切换目录。
chroot_list_enable=YES/NO(NO)
设置是否启用chroot_list_file配置项指定的用户列表文件。默认值为NO。
chroot_list_file=/etc/vsftpd.chroot_list
用于指定用户列表文件,该文件用于控制哪些用户可以切换到用户家目录的上级目录。
chroot_local_user=YES/NO(NO)
用于指定用户列表文件中的用户是否允许切换到上级目录。默认值为NO。
通过搭配能实现以下几种效果:
①当chroot_list_enable=YES,chroot_local_user=YES时,在/etc/vsftpd.chroot_list文件中列出的用户,可以切换到其他目录;未在文件中列出的用户,不能切换到其他目录。
②当chroot_list_enable=YES,chroot_local_user=NO时,在/etc/vsftpd.chroot_list文件中列出的用户,不能切换到其他目录;未在文件中列出的用户,可以切换到其他目录。
③当chroot_list_enable=NO,chroot_local_user=YES时,所有的用户均不能切换到其他目录。
④当chroot_list_enable=NO,chroot_local_user=NO时,所有的用户均可以切换到其他目录。
7.数据传输模式设置
FTP在传输数据时,可以使用二进制方式,也可以使用ASCII模式来上传或下载数据。
ascii_upload_enable=YES/NO(NO)
设置是否启用ASCII 模式上传数据。默认值为NO。
ascii_download_enable=YES/NO(NO)
设置是否启用ASCII 模式下载数据。默认值为NO。
8.访问控制设置
两种控制方式:一种控制主机访问,另一种控制用户访问。
①控制主机访问:
tcp_wrappers=YES/NO(YES)
设 置vsftpd是否与tcp wrapper相结合来进行主机的访问控制。默认值为YES。如果启用,则vsftpd服务器会检查/etc/hosts.allow 和/etc/hosts.deny 中的设置,来决定请求连接的主机,是否允许访问该FTP服务器。这两个文件可以起到简易的防火墙功能。
比如:若要仅允许192.168.0.1—192.168.0.254的用户可以连接FTP服务器,则在/etc/hosts.allow文件中添加以下内容:
vsftpd:192.168.0. :allow
all:all :deny
②控制用户访问:
对于用户的访问控制可以通过/etc目录下的vsftpd.user_list和ftpusers文件来实现。
userlist_file=/etc/vsftpd.user_list
控制用户访问FTP的文件,里面写着用户名称。一个用户名称一行。
userlist_enable=YES/NO(NO)
是否启用vsftpd.user_list文件。
userlist_deny=YES/NO(YES)
决定vsftpd.user_list文件中的用户是否能够访问FTP服务器。若设置为YES,则vsftpd.user_list文件中的用户不允许访问FTP,若设置为NO,则只有vsftpd.user_list文件中的用户才能访问FTP。
/etc /vsftpd/ftpusers文件专门用于定义不允许访问FTP服务器的用户列表(注意:如果 userlist_enable=YES,userlist_deny=NO,此时如果在vsftpd.user_list和ftpusers中都有某个 用户时,那么这个用户是不能够访问FTP的,即ftpusers的优先级要高)。默认情况下vsftpd.user_list和ftpusers,这两个 文件已经预设置了一些不允许访问FTP服务器的系统内部账户。如果系统没有这两个文件,那么新建这两个文件,将用户添加进去即可。
9.访问速率设置
anon_max_rate=0
设置匿名登入者使用的最大传输速度,单位为B/s,0 表示不限制速度。默认值为0。
local_max_rate=0
本地用户使用的最大传输速度,单位为B/s,0 表示不限制速度。预设值为0。
10.超时时间设置
accept_timeout=60
设置建立FTP连接的超时时间,单位为秒。默认值为60。
connect_timeout=60
PORT 方式下建立数据连接的超时时间,单位为秒。默认值为60。
data_connection_timeout=120
设置建立FTP数据连接的超时时间,单位为秒。默认值为120。
idle_session_timeout=300
设置多长时间不对FTP服务器进行任何操作,则断开该FTP连接,单位为秒。默认值为300 。
11.日志文件设置
xferlog_enable= YES/NO(YES)
是否启用上传/下载日志记录。如果启用,则上传与下载的信息将被完整纪录在xferlog_file 所定义的档案中。预设为开启。
xferlog_file=/var/log/vsftpd.log
设置日志文件名和路径,默认值为/var/log/vsftpd.log。
xferlog_std_format=YES/NO(NO)
如果启用,则日志文件将会写成xferlog的标准格式,如同wu-ftpd 一般。默认值为关闭。
log_ftp_protocol=YES|NO(NO)
如果启用此选项,所有的FTP请求和响应都会被记录到日志中,默认日志文件在/var/log/vsftpd.log。启用此选项时,xferlog_std_format不能被激活。这个选项有助于调试。默认值为NO。
12.定义用户配置文件
在vsftpd中,可以通过定义用户配置文件来实现不同的用户使用不同的配置。
user_config_dir=/etc/vsftpd/userconf
设置用户配置文件所在的目录。当设置了该配置项后,用户登陆服务器后,系统就会到/etc/vsftpd/userconf目录下,读取与当前用户名相同的文件,并根据文件中的配置命令,对当前用户进行更进一步的配置。
例 如:定义user_config_dir=/etc/vsftpd/userconf,且主机上有使用者 test1,test2,那么我们就在user_config_dir 的目录新增文件名为test1和test2两个文件。若是test1 登入,则会读取user_config_dir 下的test1 这个档案内的设定。默认值为无。利用用户配置文件,可以实现对不同用户进行访问速度的控制,在各用户配置文件中定义local_max_rate=XX, 即可。
13.FTP的工作方式与端口设置
FTP有两种工作方式:PORT FTP(主动模式)和PASV FTP(被动模式)
listen_port=21
设置FTP服务器建立连接所监听的端口,默认值为21。
connect_from_port_20=YES/NO
指定FTP使用20端口进行数据传输,默认值为YES。
ftp_data_port=20
设置在PORT方式下,FTP数据连接使用的端口,默认值为20。
pasv_enable=YES/NO(YES)
若设置为YES,则使用PASV工作模式;若设置为NO,则使用PORT模式。默认值为YES,即使用PASV工作模式。
pasv_max_port=0
在PASV工作模式下,数据连接可以使用的端口范围的最大端口,0 表示任意端口。默认值为0。
pasv_min_port=0
在PASV工作模式下,数据连接可以使用的端口范围的最小端口,0 表示任意端口。默认值为0。
14.与连接相关的设置
listen=YES/NO(YES)
设 置vsftpd服务器是否以standalone模式运行。以standalone模式运行是一种较好的方式,此时listen必须设置为YES,此为默 认值。建议不要更改,有很多与服务器运行相关的配置命令,需要在此模式下才有效。若设置为NO,则vsftpd不是以独立的服务运行,要受到xinetd 服务的管控,功能上会受到限制。
max_clients=0
设置vsftpd允许的最大连接数,默认值为0,表示不受限制。若设置为100时,则同时允许有100个连接,超出的将被拒绝。只有在standalone模式运行才有效。
max_per_ip=0
设置每个IP允许与FTP服务器同时建立连接的数目。默认值为0,表示不受限制。只有在standalone模式运行才有效。
listen_address=IP地址
设置FTP服务器在指定的IP地址上侦听用户的FTP请求。若不设置,则对服务器绑定的所有IP地址进行侦听。只有在standalone模式运行才有效。
setproctitle_enable=YES/NO(NO)
设置每个与FTP服务器的连接,是否以不同的进程表现出来。默认值为NO,此时使用ps aux |grep ftp只会有一个vsftpd的进程。若设置为YES,则每个连接都会有一个vsftpd的进程。
15.虚拟用户设置
虚拟用户使用PAM认证方式。
pam_service_name=vsftpd
设置PAM使用的名称,默认值为/etc/pam.d/vsftpd。
guest_enable= YES/NO(NO)
启用虚拟用户。默认值为NO。
guest_username=ftp
这里用来映射虚拟用户。默认值为ftp。
virtual_use_local_privs=YES/NO(NO)
当该参数激活(YES)时,虚拟用户使用与本地用户相同的权限。当此参数关闭(NO)时,虚拟用户使用与匿名用户相同的权限。默认情况下此参数是关闭的(NO)。
16.其他设置
text_userdb_names= YES/NO(NO)
设置在执行ls –la之类的命令时,是显示UID、GID还是显示出具体的用户名和组名。默认值为NO,即以UID和GID方式显示。若希望显示用户名和组名,则设置为YES。
ls_recurse_enable=YES/NO(NO)
若是启用此功能,则允许登入者使用ls –R(可以查看当前目录下子目录中的文件)这个指令。默认值为NO。
hide_ids=YES/NO(NO)
如果启用此功能,所有档案的拥有者与群组都为ftp,也就是使用者登入使用ls -al之类的指令,所看到的档案拥有者跟群组均为ftp。默认值为关闭。
download_enable=YES/NO(YES)
如果设置为NO,所有的文件都不能下载到本地,文件夹不受影响。默认值为YES。

6.路由追踪(解决网络问题)

情节概要:当FTP Server配置没有问题的时候。并且在内网的环境下有的机器可以访问,有的无法访问的时候需要进行路由追踪查看网络问题。

情景前提:A 服务器 Windows Server 2012 R2 、B 服务器 CentOS 7.2

使用如下命令查看在Windows服务器上的网络返回结果(B服务器的IP假设为 192.168.4.67、ftp 端口假设为 8806 、数据传输端口假设为20000-30000)

ping 192.168.4.67
telnet 192.168.4.67 8806
telnet 192.168.4.67 20000-30000

以上命令均正常返回结果,说明A 服务器 到 B 服务器的网络是通的

使用如下命令查看在CentOS 7.2服务器上的网络返回结果(A服务器的IP假设为192.168.5.100)

ping 192.168.5.100

以上命令无法正常返回结果,提示网络超时。说明B 服务器到 A 服务器的网络是“阻断”的

这个时候就需要使用网络追踪命令查看到底是哪里出了问题

  • Windows服务器上的路由追踪命令
tracert
C:\Users\admin>tracert

用法: tracert [-d] [-h maximum_hops] [-j host-list] [-w timeout]
               [-R] [-S srcaddr] [-4] [-6] target_name

选项:
    -d                 不将地址解析成主机名。
    -h maximum_hops    搜索目标的最大跃点数。
    -j host-list       与主机列表一起的松散源路由(仅适用于 IPv4)。
    -w timeout         等待每个回复的超时时间(以毫秒为单位)。
    -R                 跟踪往返行程路径(仅适用于 IPv6)。
    -S srcaddr         要使用的源地址(仅适用于 IPv6)。
    -4                 强制使用 IPv4。
    -6                 强制使用 IPv6。

C:\Users\admin>

测试使用网络追踪命令测试" w w w.baidu.com"

tracert www.baidu.com

返回结果如下所示:

C:\Users\admin>tracert www.baidu.com

通过最多 30 个跃点跟踪
到 www.a.shifen.com [61.135.169.121] 的路由:

  1     2 ms     1 ms     1 ms  192.168.1.253
  2     *        *        *     请求超时。
  3     *        *        *     请求超时。
  4     2 ms     3 ms     1 ms  10.10.200.1
  5     *        *       87 ms  218.57.136.145
  6     2 ms     2 ms     2 ms  123.233.122.145
  7     7 ms     7 ms     7 ms  119.163.104.45
  8    10 ms     9 ms     9 ms  172.18.28.38
  9    23 ms     *       19 ms  219.158.9.101
 10    27 ms    23 ms    21 ms  202.96.12.186
 11    22 ms    21 ms    21 ms  bt-227-010.bta.net.cn [202.106.227.10]
 12   107 ms    24 ms    19 ms  61.49.168.86
 13     *        *        *     请求超时。
 14    20 ms    20 ms    21 ms  61.135.169.121

跟踪完成。

C:\Users\admin>

默认命令最多支持30个跃点跟踪,可以看到第一个跳跃的站点为192.168.1.253

第二个请求超时表示数据包发送到该IP失败,以此类推,直到跳跃在61.135.169.121

  • Linux服务器上的路由追踪命令
traceroute
root@home ~ % traceroute
Version 1.4a12+Darwin
Usage: traceroute [-adDeFInrSvx] [-A as_server] [-f first_ttl] [-g gateway] [-i iface]
	[-M first_ttl] [-m max_ttl] [-p port] [-P proto] [-q nqueries] [-s src_addr]
	[-t tos] [-w waittime] [-z pausemsecs] host [packetlen]
root@home ~ % 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花有重开日-人无再少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值