zoukankan      html  css  js  c++  java
  • 【VSFTP服务】vsftpd文件传输协议部署

    vsftpd文件传输协议

    系统环境:CentOS Linux release 7.6.1810 (Core) 

    一、简介

      FTP(文件传输协议)全称是:Very Secure FTP Server。   Vsftpd是linux类操作系统上运行的ftp服务器软件。 vsftp提供三种登陆方式:1.匿名登录  2.本地用户登录  3.虚拟用户登录

    vsftpd的特点:

      1)较高的安全性需求   

      2)带宽的限制   

      3)创建支持虚拟用户   

      4)支持IPV6   

      5)中等偏上的性能   

      6)可分配虚拟IP   

      7)高速

     Ftp会话时采用了两种通道(连接方式):

       1)控制通道:与Ftp服务器进行沟通的通道,链接Ftp发送ftp指令都是通过控制通道来完成的。端口为21,用于发送FTP命令信息。

       2)数据通道:数据通道和Ftp服务器进行文件传输或则列表的通道 ,端口为20,用于上传下载数据。

    二、工作原理

      Ftp协议中控制连接均是由客户端发起,而数据连接有两种工作方式:Port和Pasv方式

     Port模式(主动模式)--> 默认

      Ftp客户端首先和Ftp server的tcp 21端口建立连接,通过这个通道发送命令,客户端要接受数据的时候在这个通道上发送Port命令,Port命令包含了客户端用什么端口(一个大于1024的端口)接受数据,在传送数据的时候,服务器端通过自己的TCP 20端口发送数据。这个时候数据连接由server向client建立一个连接。

     Port交互流程:

    client端:client链接server的21端口,并发送用户名密码和一个随机在1024上的端口及port命令给server,表明采用主动模式,并开放那个随机的端口。

    server端:server收到client发来的Port主动模式命令与端口后,会通过自己的20端口与client那个随机的端口连接后,进行数据传输。

    简而言之:就是服务端从20端口主动向客户端发起连接

      Pasv模式(被动方式)

      建立控制通道和Port模式类似,当客户端通过这个通道发送Pasv命令的时候,Ftp server打开了一个位于1024和5000之间的随机端口并且通知客户端在这个端口上进行传输数据请求,然后Ftp server将通过这个端口进行数据传输。这个时候数据连接由client向server建立连接。

      Pasv交互流程

    Clietn:client连接server的21号端口,发送用户名密码及pasv命令给server,表明采用被动模式。

    server:server收到client发来的pasv被动模式命令之后,把随机开放在1024上的端口告诉client,client再用自己的20 端口与server的那个随机端口进行连接后进行数据传输。

    简而言之:就是服务端在指定范围内的某个端口被动等待客户端发起连接

      如果从C/S模型这个角度来说,PORT对于服务器来说是OUTBOUND,而PASV模式对于服务器是INBOUND,这一点请特别注意,尤其是在使用防火墙的企业里,这一点非常关键,如果设置错了,那么客户将无法连接。

    三、安装vsfpd

    1.安装vsftpd相关组件

      [root@VM_0_10_centos tmp]# rpm -qa | grep vsftpd

     [root@VM_0_10_centos shellScript]# yum -y install vsftpd*

    2.安装PAM服务相关组件

    [root@VM_0_10_centos shellScript]# yum -y install pam*

    安装pam报错:

    --> Finished Dependency Resolution
    Error: Package: 2:postfix-2.10.1-7.el7.x86_64 (@anaconda)
               Requires: libmysqlclient.so.18(libmysqlclient_18)(64bit)
    Error: Package: libmapi-7.1.14-3.el7.x86_64 (epel)
               Requires: libmysqlclient.so.18(libmysqlclient_18)(64bit)
    Error: Package: libmapi-7.1.14-3.el7.x86_64 (epel)
               Requires: libmysqlclient.so.18()(64bit)
    Error: Package: 2:postfix-2.10.1-7.el7.x86_64 (@anaconda)
               Requires: libmysqlclient.so.18()(64bit)
     You could try using --skip-broken to work around the problem
    ** Found 2 pre-existing rpmdb problem(s), 'yum check' output follows:
    2:postfix-2.10.1-7.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
    2:postfix-2.10.1-7.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)

    解决:

    缺少Percona-XtraDB-Cluster-shared-55-5.5.37-25.10.756.el6.x86_64.rpm这个包

    [root@VM_0_10_centos tmp]# wget http://www.percona.com/redir/downloads/Percona-XtraDB-Cluster/5.5.37-25.10/RPM/rhel6/x86_64
    /Percona-XtraDB-Cluster-shared-55-5.5.37-25.10.756.el6.x86_64.rpm
    [root@VM_0_10_centos tmp]# rpm -ivh Percona-XtraDB-Cluster-shared-55-5.5.37-25.10.756.el6.x86_64.rpm 
    warning: Percona-XtraDB-Cluster-shared-55-5.5.37-25.10.756.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: N
    OKEYPreparing...                          ################################# [100%]
    Updating / installing...
       1:Percona-XtraDB-Cluster-shared-55-################################# [100%]

    参考网址:https://blog.csdn.net/debimeng/article/details/78143071

    最后再运行安装pam即可

    # yum -y install pam*

    3.安装DB4部件包

    用来支持文件数据库

    [root@VM_0_10_centos tmp]# yum -y install db4*

    四、系统账户

    1.建立Vsftpd服务的宿主用户

      默认的Vsftpd的服务宿主用户是root,但不符合安全性的需要。这里建立名字为vsftpd的用户,用他来作为支持Vsftpd的服务宿主用户。由于该用户仅用来支持Vsftpd服务用,因此没有许可他登陆系统的必要,并设定他为不能登陆系统的用户。

    nologin参考网址:https://blog.csdn.net/danson_yang/article/details/65629948

    [root@VM_0_10_centos tmp]# useradd vsftpd -s /sbin/nologin

    /sbin/nologin更改用户是否可以使用ssh登录

    2.建立Vsftpd虚拟宿主用户

    [root@VM_0_10_centos tmp]# useradd vrvsftpd -s /sbin/nologin

    虚拟用户并不是系统用户,也就是说这些FTP的用户在系统中是不存在的。他们的总体权限其实是集中寄托在一个在系统中的某一个用户身上的,所谓Vsftpd的虚拟宿主用户,就是这样一个支持着所有虚拟用户的宿主用户。由于他支撑了FTP的所有虚拟的用户,那么他本身的权限将会影响着这些虚拟的用户,因此,处于安全性的考虑,也要非分注意对该用户的权限的控制,该用户也绝对没有登陆系统的必要,这里也设定他为不能登陆系统的用户。

    五、修改vsftpd配置文件

    # 1. 匿名用户的有关设置
    anonymous_enable =YES/NO    # 是否允许匿名用户登录FTP服务器,默认值为YES。
    no_anon_password =YES/NO     # 控制匿名用户登入时是否需要密码,YES不需要,NO需要。默认值为NO。
    anon_world_readable_only =YES/NO     # 匿名用户是否允许下载可阅读的文档。默认值为YES。 
    anon_upload_enable =YES/NO    # 是否允许匿名用户上传文件,默认为NO。
    anon_mkdir_write_enable =YES/NO    # 是否允许匿名用户有创建目录的写入权限,默认值为NO
    anon_other_write_enable=YES/NO     # 是否允许匿名用户有其他的写入权限,如对文件改名、覆盖及删除文件。默认值为NO。
    ftp_username=           (自添)# 匿名用户所使用的系统用户名。默认下,此参数在配置文件中不出现,默认值为ftp。
    anon_root=/var/ftp         (自添)# 设置匿名用户登录后所在的目录(默认为/var/ftp)
    anon_umask=022            (自添)# 匿名用户所上传文件的默认权限掩码值
    anon_max_rate=200000     # 设置匿名用户的最大传输速度,单位为bytes/sec,值为0表示不限制 
    # 2.本地用户的设置
    local_enable=YES|NO     # 是否允许本地用户登录FTP服务器,默认值为YES。 
    local_umask=022    # 本地用户上传文件的默认权限掩码值
    local_max_rate=500000    # 设置本地用户的最大传输速度,单位为bytes/sec,值为0时表示不限制
    local_root=/var/ftp    (自添)# 设置本地用户登录后所在目录(缺省为为用户主目录)。如:user1用户为:/home/user1
    # 3. 全局设置
    write_enable=YES|NO    # 允许用户访问时,是否允许他们有写入的权限,默认值为YES。
    listen=YES|NO    # 设置vsftpd服务器是否以独立(standalone)模式运行 ,默认值为YES,建议不要更改。很多与服务器运行相关的配置命令,需要此运行模式才有效。若设置为NO,则vsftpd不是以独立的服务运行,要受xinetd服务的管理控制,功能上会受限制。
    listen_port=21    # 设置FTP服务器建立连接所侦听的端口,默认值为21。
    max_clients=0    # 设置FTP服务器所允许的最大客户端连接数,默认值为0,表示不限制。若设置为150时,则同时允许有150个连接,超出的将拒绝建立连接。只有在以standalone模式运行时才有效。
    max_per_ip=5    # 设置每个IP地址允许与FTP服务器同时建立连接的数目。默认为0,不受限制。通常可对此配置进行设置,防止同一个用户建立太多的连接。只有在以standalone模式运行时才有效。
    xferlog_enable=YES    # 是否启用日志
    xferlog_file=var/log/vsftpd.log      # 设置日志文件名及路径。需启用xferlog_enable选项
    xferlog_std_format=YES     # 是否用标准格式存储日志
    pam_service_name=vsftpd     # 设置PAM认证服务的配置文件名,该文件位于/etc/pam.d目录下
    
    # 3. 全局设置——欢迎信息 
    ftpd_banner=Welcome blah FTP service    # 这边可定义欢迎话语的字符串,相较于banner_file 是档案的形式,而ftpd_banner 是字串的格式。预设为无。
    banner_file =/etc/vsftpd/banner    # 当使用者登入时,会显示此设定所在的文件的内容,通常为欢迎话语或是说明。默认值为无。 
    dirmessage_enable =YES    # 如果启动这个选项,使用者第一次进入一个目录时,会检查该目录下是否有.message这个档案,若是有,则会出现此档案的内容,通常这个档案会放置欢迎话语,或是对该目录的说明。默认值为开启。
    message_file=.message    # 设置目录消息文件。当使用者第一次进入一个目录时,是否显示该目录中的.message文件(需用编辑器手工创建)的内容,该文件通常放置欢迎话语,或是对该目录的说明信息 

    # 4. 控制用户是否允许切换到上级目录 
    # 1)配置所有登录不能改变自己的FTP根目录(本地用户是否锁定在宿主目录中,要对本地用户查看效果,需先设置 local_root=/var/ftp )
    chroot_local_user=YES|NO 
    
    # 2)配置部分账户不允许改变自己的FTP根目录
    #chroot_list_enable=YES|NO    # 是否启用chroot_list_file配置项指定的用户列表文件
    #chroot_list_file=/etc/vsftpd/chroot_list     # 此文件需自己建立,被列入此文件的用户,在登录后将不能切换到自己目录以外的其他目录
    
    # chroot_list中的用户未锁定, chroot_list外的用户锁定
    chroot_list_enable=YES
    chroot_local_user=NO
    # chroot_list中的用户锁定, chroot_list外的用户未锁定
    chroot_list_enable=NO
    chroot_local_user=YES
    # 所有用户锁定
    chroot_list_enable=NO
    chroot_local_user=NO
    # 所有用户未锁定
    chroot_list_enable=YES
    chroot_local_user=YES
    # 5. 设置访问控制
    # 1)设置允许或不允许访问的主机
    tcp_wrappers=YES
    # 设置vsftpd服务器是否与tcp wrapper相结合,进行主机的访问控制。默认为YES,vsftpd服务器会检查/etc/hosts.allow和/etc/hosts.deny中的设置,以决定请求连接的主机是否允许访问该FTP服务器。这两个文件可以起到简易的防火墙功能。
    # 如:若仅允许192.168.168.1~192.168.168.254的用户,可以访问连接vsftpd服务器,则可在/etc/hosts.allow文件中添加以下内容:
    vsftpd:192.168.168.0/255.255.255.0 :allow
    all:all:deny
    
    # 2)设置允许或不允许访问的用户
    # 对用户的访问控制由/etc/vsftpd/user_list和/etc/vsftpd/ftpusers文件来控制实现。
    # 相关配置命令如下:
    userlist_enable=YES | NO        # 设置/etc/vsftpd/user_list文件是否启用生效。YES则生效,NO不生效。
    userlist_deny=YES | NO    #设置/etc/vsftpd/user_list文件中的用户是允许访问还是不允许访问。
    # 若设置为YES,则/etc/vsftpd/user_list文件中的用户将不允许访问FTP服务器;若设置为NO,则只有vsftpd.user_list文件中的用户,才能访问FTP服务器。
    # 用户文件列表
    /etc/vsftpd/ftpusers     # 指定了不允许访问FTP服务器的本地用户账号(黑名单) ,例如root等。这些账号不是普通用户账号,而是在系统中具有较高权限的账号,禁止这些账号进行FTP登录可提高系统的安全性。
    /etc/vsftpd/user_list     # 指定允许或不允许登陆的用户列表,使用起来比ftpusers更加灵活。需要在主配置文件中进行设置,如下图

    userlist_enable=YES

    userlist_deny=YES

    ●ftpusers中用户禁止访问

    ● user_list中用户禁止访问(登录时不会出现密码提示,直接被服务器拒绝 )

    ●其他的ftp用户都可以登录

    userlist_enable=YES

    userlist_deny=NO

    ●只允许 user_list中的用户访问

    ●其他的ftp用户都不可以登录

    # 6.设置用户配置文件所在的目录
    # 在vsftpd服务器中,不同用户还可使用不同的配置,这要通过用户配置文件来实现。
    user_config_dir=/etc/vsftpd/userconf
    # 用于设置用户配置文件所在的目录。
      # 设置了该配置项后,当用户登录FTP服务器时,系统就会到/etc/vsftpd/userconf目录下读取与当前用户名相同的文件,并根据文件中的配置命令,对当前用户进行更进一步的配置。比如,利用用户配置文件,可实现对不同用户进行访问的速度进行控制,在各用户配置文件中,定义local_max_rate配置,以决定该用户允许的访问速度。
    # 7.FTP工作方式与服务端口
    connect_from_port_20=YES|NO    # 指定FTP数据传输连接是否使用20端口,默认值为YES 。若设置为NO,则进行数据连接时,所使用的端口由ftp_data_port指定
    ftp_data_port=20     # 设置PORT方式下FTP数据连接所使用的端口,默认值为20。
    pasv_enable=YES|NO     # 若设置为YES,则使用PASV工作模式;若设置为NO,使用PORT模式。默认为YES,即使用PASV模式。
    pasv_max_port=0    # 设置在PASV工作方式下,数据连接可以使用的端口范围的上界。默认值为0,表示任意端口。
    pasv_mim_port=0    # 设置在PASV工作方式下,数据连接可以使用的端口范围的下界。默认值为0,表示任意端口。 
    # 8.设置传输模式
    # FTP在传输数据时,可使用二进制(Binary)方式,也可使用ASCII模式来上传或下载数据。
    ascii_download_enable=YES    # 设置是否启用ASCII模式下载数据。默认为NO
    ascii_upload_enable=YES    # 设置是否启用ASCII模式上传数据。默认为NO

    1.编辑配置文件前先备份

    [root@VM_0_10_centos tmp]# cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.backup

    内容如下:

    [root@VM_0_10_centos tmp]# cat /etc/vsftpd/vsftpd.conf
    
    # Allow anonymous FTP? (Beware - allowed by default if you comment this out).
    # anonymous_enable=YES
    # 是否允许匿名用户访问,默认为YES,这里设置不允许匿名访问
    anonymous_enable=NO
    
    #
    # Uncomment this to allow local users to log in.
    # When SELinux is enforcing check for SE bool ftp_home_dir
    # 设置本地用户可以访问。PS:主要为虚拟宿主用户,如该项目设定为No,那么所有虚拟用户将无法访问
     local_enable=YES
    
    #
    # Uncomment this to enable any form of FTP write command.
    # 设置可进行写操作
    write_enable=YES
    
    #
    # Default umask for local users is 077. You may wish to change this to 022,
    # if your users expect that (022 is used by most other ftpd's)
    # 设置上传后文件的权限掩码
    local_umask=022
    
    #
    # Uncomment this to allow the anonymous FTP user to upload files. This only
    # has an effect if the above global write enable is activated. Also, you will
    # obviously need to create a directory writable by the FTP user.
    # When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
    #anon_upload_enable=YES
    # 禁止匿名用户上传文件
    anonymous_enable=NO
    
    #
    # Uncomment this if you want the anonymous FTP user to be able to create
    # new directories.
    #anon_mkdir_write_enable=YES
    # 禁止匿名用户建立目录
    anon_mkdir_write_enable=NO
    
    #
    # Activate directory messages - messages given to remote users when they
    # go into a certain directory.
    # 设置开启目录标语功能
    dirmessage_enable=YES
    
    #
    # Activate logging of uploads/downloads.
    # 开启日志记录功能
    xferlog_enable=YES
    
    #
    # Make sure PORT transfer connections originate from port 20 (ftp-data).
    # 设置端口20进行数据库连接
    connect_from_port_20=YES
    
    #
    # If you want, you can arrange for uploaded anonymous files to be owned by
    # a different user. Note! Using "root" for uploaded files is not
    # recommended!
    #chown_uploads=YES
    #chown_username=whoever
    # 设置禁止上传文件更改宿主
    chown_uploads=NO
    
    #
    # You may override where the log file goes if you like. The default is shown
    # below.
    #xferlog_file=/var/log/xferlog
    # 设置vsftpd服务日志保存路径。PS:该文件默认不存在,须手动创建。
    # 由于这里手动更改了vsftpd宿主用户为vsftpd,须注意给与改用户对日志的写入权限
    xferlog_file=/var/log/vsftpd.log
    
    #
    # If you want, you can have your log file in standard ftpd xferlog format.
    # Note that the default log file location is /var/log/xferlog in this case.
    # 设置日志使用标准的记录格式
    xferlog_std_format=YES
    
    #
    # You may change the default value for timing out an idle session.
    # 设置空闲连接超时时间,这里使用默认。
    # 将具体数值留给每个具体用户具体指定,当然如果不指定的话,还是使用这里的默认值600,单位秒
    #idle_session_timeout=600
    
    #
    # You may change the default value for timing out a data connection.
    # 设定单次最大连续传输时间,这里使用默认。
    # 将具体数值留给每个具体用户具体指定,当然如果不指定的话,还是使用这里的默认值120,单位秒。
    #data_connection_timeout=120
    
    #
    # It is recommended that you define on your system a unique user which the
    # ftp server can use as a totally isolated and unprivileged user.
    #nopriv_user=ftpsecure
    # 设定支撑Vsftpd服务的宿主用户为手动建立的Vsftpd用户。
    # PS:一旦做出更改宿主用户后,必须注意一起与该服务相关的读写文件的读写赋权问题。比如日志文件就必须给与该用户写入权限等。
    nopriv_user=vsftpd
    
    #
    # Enable this and the server will recognise asynchronous ABOR requests. Not
    # recommended for security (the code is non-trivial). Not enabling it,
    # however, may confuse older FTP clients.
    # 设置支持异步传输功能
    async_abor_enable=YES
    
    #
    # ASCII mangling is a horrible feature of the protocol.
    # 设置支持ASCII模式上传和下载功能
    ascii_upload_enable=YES
    ascii_download_enable=YES
    
    #
    # You may fully customise the login banner string:
    #ftpd_banner=Welcome to blah FTP service.
    # 设置vsftpd的登录标语
    ftpd_banner=This Vsftp server supports virtual users ^_^
    
    #
    # You may specify a file of disallowed anonymous e-mail addresses. Apparently
    # useful for combatting certain DoS attacks.
    #deny_email_enable=YES
    # (default follows)
    #banned_email_file=/etc/vsftpd/banned_emails
    #
    # You may specify an explicit list of local users to chroot() to their home
    # directory. If chroot_local_user is YES, then this list becomes a list of
    # users to NOT chroot().
    # (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
    # the user does not have write access to the top level directory within the
    # chroot)
    #chroot_local_user=YES
    #chroot_list_enable=YES
    # 禁止用户登出自己的ftp主目录
    chroot_list_enable=NO
    
    # (default follows)
    #chroot_list_file=/etc/vsftpd/chroot_list
    #
    # You may activate the "-R" option to the builtin ls. This is disabled by
    # default to avoid remote users being able to cause excessive I/O on large
    # sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
    # the presence of the "-R" option, so there is a strong case for enabling it.
    #ls_recurse_enable=YES
    # 禁止用户登录ftp后使用"ls -R"命令。该命令会对服务器性能造成巨大开销。如果该项被允许,那么挡多用户同时使用该命令时将会对该服务器造成威胁。
    ls_recurse_enable=NO
    
    #
    # When "listen" directive is enabled, vsftpd runs in standalone mode and
    # listens on IPv4 sockets. This directive cannot be used in conjunction
    # with the listen_ipv6 directive.
    # listen=NO
    # 设定该Vsftpd服务工作在StandAlone模式下。
    # 顺便展开说明一下,所谓StandAlone模式就是该服务拥有自己的守护进程支持,
    # 在ps -A命令下我们将可用看到vsftpd的守护进程名。如果不想工作在StandAlone模式下,则可以选择SuperDaemon模式,
    # 在该模式下 vsftpd将没有自己的守护进程,而是由超级守护进程Xinetd全权代理,与此同时,Vsftp服务的许多功能将得不到实现
    listen=YES
    
    #
    # This directive enables listening on IPv6 sockets. By default, listening
    # on the IPv6 "any" address (::) will accept connections from both IPv6
    # and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
    # sockets. If you want that (perhaps because you want to listen on specific
    # addresses) then you must run two copies of vsftpd with two configuration
    # files.
    # Make sure, that one of the listen options is commented !!

     # listen_ipv6=YES
     # 设置pam服务下vsftpd的验证配置文件名。因此,PAM验证将参考/etc/pam.d/下的vsftpd文件配置
      pam_service_name=vsftpd
     # 设定userlist_file中的用户将不得使用FTP。
      userlist_enable=YES
     # 设定支持TCP Wrappers。
      tcp_wrappers=YES

    # 以下这些是关于Vsftpd虚拟用户支持的重要配置项目。默认Vsftpd.conf中不包含这些设定项目,需要自己手动添加配置。
    # 设置启用虚拟用户功能
    guest_enable=YES
    
    # 指定虚拟用户宿主用户
    guest_username=vrvsftpd
    
    # 设定虚拟用户的权限符合他们的宿主用户。
    virtual_use_local_privs=YES
    
    # 设定虚拟用户个人Vsftp的配置文件存放路径。
    # 也就是说,这个被指定的目录里,将存放每个Vsftp虚拟用户个性的配置文件,一个需要注意的地方就是这些配置文件名必须和虚拟用户名相同。
    user_config_dir=/etc/vsftpd/vconf

     3.建立Vsftpd的日志文件,并更该属主为Vsftpd的服务宿主用户

    [root@VM_0_10_centos tmp]# touch /var/log/vsftpd.log
    [root@VM_0_10_centos tmp]# chown vsftpd.vsftpd /var/log/vsftpd.log

    4.建立虚拟用户配置文件存放路径

    [root@VM_0_10_centos tmp]# mkdir /etc/vsftpd/vconf

    六、制作虚拟用户数据库文件

     1.先建立虚拟用户名单文件

    建立了一个虚拟用户名单文件,这个文件就是来记录vsftpd虚拟用户的用户名和口令的数据文件,我这里给它命名为virtusers。为了避免文件的混乱,我把这个名单文件就放置在/etc/vsftpd/下。

    [root@VM_0_10_centos tmp]# touch /etc/vsftpd/virtusers

    2.编辑虚拟用户名单文件

    格式为:“一行用户名,一行口令”。

    [root@VM_0_10_centos tmp]# cat /etc/vsftpd/virtusers 
    zs
    设置的密码
    thy 设置的密码

    3.生成虚拟用户数据文件

    db_load主要用来生成db数据库使用。在Vsftpd的虚拟用户设置中先新建一个文件users.txt 把用户名密码放入其中

    接着 db_load -T -t hash -f   */users.txt    */users.db

    PS: * 表示目录

    这样就生成了一个users.db文件(hash码型的数据库文件)

     参考网址:http://blog.itpub.net/20943428/viewspace-661714/

    [root@VM_0_10_centos tmp]# db_load -T -t hash -f /etc/vsftpd/virtusers /etc/vsftpd/virtusers.db

    参数:

      选项-T允许应用程序能够将文本文件转译载入进数据库。由于我们之后是将虚拟用户的信息以文件方式存储在文件里的,为了让Vsftpd这个应用程序能够通过文本来载入用户数据,必须要使用这个选项。  

      子选项-t,追加在在-T选项后,用来指定转译载入的数据库类型。扩展介绍下,-t可以指定的数据类型有Btree、Hash、Queue和Recon数据库

     PS:如果指定了选项-T,那么一定要追跟子选项 -t

    4.察看生成的虚拟用户数据文件

    [root@VM_0_10_centos tmp]# ll /etc/vsftpd/virtusers.db 
    -rw-r--r-- 1 root root 12288 Oct  9 11:02 /etc/vsftpd/virtusers.db

    PS:以后再添加虚拟用户的时候,只需要按照“一行用户名,一行口令”的格式将新用户名和口令添加进虚拟用户名单文件。但是光这样做还不够,不会生效的哦!还要再执行一遍“ db_load -T -t hash -f 虚拟用户名单文件 虚拟用户数据库文件.db ”的命令使其生效才可以!

    七、设定PAM验证文件,并指定虚拟用户数据库文件进行读取

    1.察看原来的Vsftp的PAM验证配置文件

    [root@VM_0_10_centos tmp]# cat /etc/pam.d/vsftpd 
    #%PAM-1.0
    session    optional     pam_keyinit.so    force revoke
    auth       required    pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
    auth       required    pam_shells.so
    auth       include    password-auth
    account    include    password-auth
    session    required     pam_loginuid.so
    session    include    password-auth

    2.在编辑前做好备份

    [root@VM_0_10_centos tmp]# cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.backup
    [root@VM_0_10_centos tmp]# vi /etc/pam.d/vsftpd

    内容如下

    [root@VM_0_10_centos tmp]# vi /etc/pam.d/vsftpd
    #%PAM-1.0
    session    optional     pam_keyinit.so    force revoke
    # 以下两条是手动添加的,内容是对虚拟用户的安全和帐户权限进行验证。
    # 这里的auth是指对用户的用户名口令进行验证
    auth    sufficient      /usr/lib64/security/pam_userdb.so     db=/etc/vsftpd/virtusers
    # 这里的accout是指对用户的帐户有哪些权限哪些限制进行验证。
    # 其后的sufficient表示充分条件,也就是说,一旦在这里通过了验证,那么也就不用经过下面剩下的验证步骤了。
    # 相反,如果没有通过的话,也不会被系统立即挡之门外,因为sufficient的失败不决定整个验证的失败,意味着用户还必须将经历剩下来>的验证审核。
    account sufficient /usr/lib64/security/pam_userdb.so db=/etc/vsftpd/virtusers # 后面的/lib/security/pam_userdb.so表示该条审核将调用pam_userdb.so这个库函数进行。 # 最后的db=/etc/vsftpd/virtusers则指定了验证库函数将到这个指定的数据库中调用数据进行验证。 auth required pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed auth required pam_shells.so auth include password-auth account include password-auth session required pam_loginuid.so session include password-auth

    八、虚拟用户的配置

    1.规划好虚拟用户的主路径

    [root@VM_0_10_centos tmp]# mkdir /opt/vsftpd

    2.建立测试用户的FTP用户目录

    [root@VM_0_10_centos tmp]# mkdir /opt/vsftpd/{zs,thy}
    [root@VM_0_10_centos tmp]# ls /opt/vsftpd/
    thy  zs

    3.建立虚拟用户配置文件模版

    [root@VM_0_10_centos tmp]# cp /etc/vsftpd/vsftpd.conf.backup /etc/vsftpd/vconf/vconf.tmp

    4.定制虚拟用户模版配置文件

    [root@VM_0_10_centos tmp]# vi /etc/vsftpd/vconf/vconf.tmp 
    # 指定虚拟用户的具体主路径。
    local_root=/opt/vsftpd/virtuser
    # 设定不允许匿名用户访问。
    anonymous_enable=NO
    # 设定允许写操作。
    write_enable=YES
    # 设定上传文件权限掩码。
    local_umask=022
    # 设定不允许匿名用户上传。
    anon_upload_enable=NO
    # 设定不允许匿名用户建立目录。
    anon_mkdir_write_enable=NO
    # 设定空闲连接超时时间。
    idle_session_timeout=600
    # 设定单次连续传输最大时间。
    data_connection_timeout=120
    # 设定并发客户端访问个数。
    max_clients=10
    # 设定单个客户端的最大线程数,这个配置主要来照顾Flashget、迅雷等多线程下载软件。
    max_per_ip=5
    # 设定该用户的最大传输速率,单位b/s。
    local_max_rate=50000

    这里将原vsftpd.conf配置文件经过简化后保存作为虚拟用户配置文件的模版。将并不需要指定太多的配置内容,主要的框架和限制交由 Vsftpd的主配置文件vsftpd.conf来定义,即虚拟用户配置文件当中没有提到的配置项目将参考主配置文件中的设定。而在这里作为虚拟用户的配置文件模版只需要留一些和用户流量控制,访问方式控制的配置项目就可以了。这里的关键项是local_root这个配置,用来指定这个虚拟用户的FTP主路径。

    5.更改虚拟用户的主目录的属主为虚拟宿主用户:

    [root@VM_0_10_centos tmp]# chown -R vrvsftpd.vrvsftpd /opt/vsftpd/
    [root@VM_0_10_centos tmp]# ll /opt/vsftpd/
    total 8
    drwxr-xr-x 2 vrvsftpd vrvsftpd 4096 Oct  9 11:24 thy
    drwxr-xr-x 2 vrvsftpd vrvsftpd 4096 Oct  9 11:24 zs

    九、给测试用户定制

    1.从虚拟用户模版配置文件复制

    [root@VM_0_10_centos tmp]# cp /etc/vsftpd/vconf/vconf.tmp /etc/vsftpd/vconf/thy

    2.针对具体用户进行定制

    [root@VM_0_10_centos tmp]# vi /etc/vsftpd/vconf/thy 
    # 指定虚拟用户的具体主路径。
    local_root=/opt/vsftpd/thy
    # 设定不允许匿名用户访问。
    anonymous_enable=NO
    # 设定允许写操作。
    write_enable=YES
    # 设定上传文件权限掩码。
    local_umask=022
    # 设定不允许匿名用户上传。
    anon_upload_enable=NO
    # 设定不允许匿名用户建立目录。
    anon_mkdir_write_enable=NO
    # 设定空闲连接超时时间。
    idle_session_timeout=300
    # 设定单次连续传输最大时间。
    data_connection_timeout=90
    # 设定并发客户端访问个数。
    max_clients=1
    # 设定单个客户端的最大线程数,这个配置主要来照顾Flashget、迅雷等多线程下载软件。
    max_per_ip=1
    # 设定该用户的最大传输速率,单位b/s。
    local_max_rate=25000

    十、启动服务

    [root@VM_0_10_centos tmp]# systemctl restart vsftpd
    [root@VM_0_10_centos tmp]# systemctl status vsftpd

    十一、测试

    1.在虚拟用户目录中预先放入文件

    [root@VM_0_10_centos tmp]# touch /opt/vsftpd/thy/thy.test

    2.从其他机器作为客户端登陆FTP

    前提这台机器安装了ftp服务,能使用ftp命令

     解决:

    可能是pam问题,上面配置vsftpd.conf配置文件时,没有开启pam的验证,将pam验证开启即可

    参考网址:https://blog.csdn.net/junjunjiao/article/details/50738009

    测试登录:

    3.测试列单操作

    解决:

    将主动改为被动,然后重新登录ftp

    ftp> passive
    Passive mode on.

    参考网址:https://blog.csdn.net/indexman/article/details/42649329

    4.测试上传操作

    参数使用格式参考网址:https://www.jb51.net/article/124033.htm

    首先现在客户端服务器上当前目录创建hello.txt文件,登录ftp,测试上传

    [root@VM_0_16_centos ~]# touch hello.txt
    [root@VM_0_16_centos ~]# ftp
    ftp> ls
    227 Entering Passive Mode (106,53,73,200,241,96).
    150 Here comes the directory listing.
    -rw-r--r--    1 0        0               0 Oct 09 03:51 thy.test
    226 Directory send OK.
    ftp> put
    (local-file) hello.txt
    (remote-file) ftp_hello.txt
    local: hello.txt remote: ftp_hello.txt
    227 Entering Passive Mode (106,53,73,200,91,237).
    150 Ok to send data.
    226 Transfer complete.
    30 bytes sent in 3e-05 secs (1000.00 Kbytes/sec)

    在vsftpd服务端就能看到刚刚上传的文件了

    [root@VM_0_10_centos tmp]# ls /opt/vsftpd/thy/
    ftp_hello.txt  thy.test

    5.测试建立目录操作

    ftp> mkdir ftp_test
    257 "/opt/vsftpd/thy/ftp_test" created

    6.测试下载操作

     需要下载的文件,vsftpd服务端必须要有这个文件才行

    ftp> get thy.test
    local: thy.test remote: thy.test
    227 Entering Passive Mode (106,53,73,200,245,241).
    150 Opening BINARY mode data connection for thy.test (0 bytes).
    226 Transfer complete.

     在/etc/vsftpd/vsftpd.conf中,local_enable的选项必须打开为Yes,使得虚拟用户的访问成为可能,否则会出现以下现象:

    [root@KcentOS5 ~]# ftp
    ftp> open ip地址
    Connected to ip地址.
    500 OOPS: vsftpd: both local and anonymous access disabled!

    原因:虚拟用户再丰富,其实也是基于它们的宿主用户overlord的,如果overlord这个虚拟用户的宿主被限制住了,那么虚拟用户也将受到限制。
    补充:

    500 OOPS:错误

    有可能是你的vsftpd.con配置文件中有不能被识别的命令,还有一种可能是命令的YES 或 NO 后面有空格。

    550 权限错误,不能创建目录和文件

    解决方法: 关闭selinux

    # vi /etc/selinux/config

    将 SELINUX=XXX -->XXX 代表级别

    改为

    SELINUX=disabled

    修改配置文件需要重启

    7、通过浏览器访问

    ftp://106.53.73.200/

    输入配置的用户名和密码即可

    参考网址:https://www.cnblogs.com/hhuai/archive/2011/02/12/1952647.html

    参数使用格式参考网址:https://www.jb51.net/article/124033.htm

  • 相关阅读:
    单div绘制多元素图
    js笔试题系列之二——数组与对象
    JS设计模式——策略模式
    js笔试题系列之三——函数
    zepto.js中的Touch事件
    java定时任务之Scheduled注解
    汤姆大叔送书,咱也科学抢书
    Asp.net Mvc自定义客户端验证(CheckBox列表的验证)
    摆脱烂项目
    我的ORM发展史
  • 原文地址:https://www.cnblogs.com/HeiDi-BoKe/p/11642444.html
Copyright © 2011-2022 走看看