zoukankan      html  css  js  c++  java
  • Linux ftp VSftp

    一、Linux FTP服务器分类:

    <1>wu-ftp

    <2>proftp=profession ftp

    <3>vsftp=very security ftp  本文介绍

     

     


     

    配置文件:

    [root@localhost vsftpd]# ls /etc/vsftpd/
           chroot_list     userconfig        vsftpd.conf             vusers.list

    ftpusers          user_list           vsftpd_conf_migrate.sh

     

           chroot_list    例外 权限  用户 列表(结合配置文件中的 chroot_list_file

           vsftpd.conf   //主配置文件(一般位置在此处)

    ftpusers       //被禁止登录FTP的用户(例如root用户、bin用户……)

     

           user_list     //FTP用户访问控制文件

    userconfig     可以设置不同账号的登陆位置等信息

     

     

     

     

    二、访问方式

    <1>匿名登录

    <2>帐号登录

    启动FTP服务器:

    #service vsftpd restart

    vsftpd.conf  配置文件内容

    # Example config file /etc/vsftpd/vsftpd.conf
    #
    # The default compiled in settings are fairly paranoid. This sample file
    # loosens things up a bit, to make the ftp daemon more usable.
    # Please see vsftpd.conf.5 for all compiled in defaults.
    #
    # READ THIS: This example file is NOT an exhaustive list of vsftpd options.
    # Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
    # capabilities.
    #
    # Allow anonymous FTP? (Beware - allowed by default if you comment this out).
    anonymous_enable=YES          控制匿名登录  将YES  改为 NO
    #
    # Uncomment this to allow local users to log in.
    local_enable=YES                   允许本地帐号登录 YES/NO
    #
    # Uncomment this to enable any form of FTP write command.
    write_enable=YES                  控制可写权限   YES/NO
    #
    # 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                   本地文件的权限掩码 默认是077
    #
    # 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.
    #anon_upload_enable=YES     是否允许匿名上传
    #
    # Uncomment this if you want the anonymous FTP user to be able to create
    # new directories.
    #anon_mkdir_write_enable=YES     是否允许匿名写及创建目录的权限          
    #
    # Activate directory messages - messages given to remote users when they
    # go into a certain directory.
    dirmessage_enable=YES           切换目录时,是否显示目录下.message的内
    #
    # The target log file can be vsftpd_log_file or xferlog_file.
    # This depends on setting xferlog_std_format parameter
    xferlog_enable=YES              是否开启日志
    #
    # Make sure PORT transfer connections originate from port 20 (ftp-data).
    connect_from_port_20=YES            启用 ftp数据连接端口
    #
    # 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=YES时,所指定的属主用户账号
    #                                                           此处的whoever自然要用合适的用户账号来代替
    # The name of log file when xferlog_enable=YES and xferlog_std_format=YES
    # WARNING - changing this filename affects /etc/logrotate.d/vsftpd.log
    #xferlog_file=/var/log/xferlog         服务日志保存路径
    #
    # Switches between logging into vsftpd_log_file and xferlog_file files.
    # NO writes to vsftpd_log_file, YES to xferlog_file
    xferlog_std_format=YES            使用标准的日志格式
    #
    # You may change the default value for timing out an idle session.
    #idle_session_timeout=600      session超时时间(600秒)
    #
    # You may change the default value for timing out a data connection.
    #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    指定一个安全用户账号,让FTP服务器用作完全隔离和没有特权的独立用户
    #                                               这是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    异步传输 强烈建议不要启用该选项,否则将可能导致出错!
    #
    # By default the server will pretend to allow ASCII mode but in fact ignore
    # the request. Turn on the below options to have the server actually do ASCII
    # mangling on files when in ASCII mode.
    # Beware that on some FTP servers, ASCII support allows a denial of service
    # attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
    # predicted this attack and has always been safe, reporting the size of the
    # raw file.
    # ASCII mangling is a horrible feature of the protocol.
    #ascii_upload_enable=YES               可否用ascii 码上传
    #ascii_download_enable=YES         可否用ascii 码下载

             默认情况下服务器会假装接受ASCⅡ模式请求但实际上是忽略这样的请求,启用上述的两个选项可以让服务器真正实现ASCⅡ模式的传输
            
    注意:启用ascii_download_enable选项会让恶意远程用户们在ASCⅡ模式下用“SIZE/big/file”这样的指令大量消耗FTP服务器的I/O资源.
             这些ASCⅡ模式的设置选项分成上传和下载两个,这样我们就可以允许ASCⅡ模式的上传(可以防止上传脚本等恶意文件而导致崩溃),而不会遭受拒绝服务攻击的危险.

    #
    # You may fully customise the login banner string:
    #ftpd_banner=Welcome to blah FTP service.       ftp欢迎语
    #
    # You may specify a file of disallowed anonymous e-mail addresses. Apparently
    # useful for combatting certain DoS attacks.
    #deny_email_enable=YES       若是启动这项功能,则必须提供一个/etc/vsftpd/banner_emails,内容为email address。
            若是使用匿名登入,则会要求输入email address,若输入的email address 在此档案内,则不允许进入。      
     
           
    可以创建一个文件保存某些匿名电子邮件的黑名单,以防止这些人使用Dos攻击
    # (default follows)
    #banned_email_file=/etc/vsftpd/banned_emails   当启用deny_email_enable时,所需的电子邮件黑名单保存路径

     

     


    #
    # 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().
    #chroot_local_user=YES            本地所有帐户都只能在自己的home目录
    #chroot_list_enable=YES         (启用与上条限制相反的列外列表) 
    # (default follows)
    #chroot_list_file=/etc/vsftpd/chroot_list    chroot_list 文件地址
    注意:vsftpd.chroot_list 是没有创建的需要自己添加,要想控制帐号就直接在文件中加帐号即可

     

    这里需要详细说明:
          chroot_local_user  含义为  是否将所有用户限制在主目录     YES为启用限制      NO禁用限制 
                                                默认不限制,注释也为不限制  

           chroot_list_enable  是否(YES NO)  启用例外名单,如果启用则名单中用户不受 上条语句限制

     

     

     

     

     

     

     

     

    #     
    # 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   是否允许使用ls -R等命令
    #
    # 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=YES       监听IP4 地址 设置vsftp为独立运行模式
    #
    # This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6
    # sockets, you must run two copies of vsftpd with two configuration files.
    # Make sure, that one of the listen options is commented !!
    #listen_ipv6=YES    监听IP6

    pam_service_name=vsftpd    这个是 pam模块的名称,放置在 /etc/pam.d/vsftpd 即是这个
    userlist_enable=YES
           
    此选项被激活后,VSFTPD将读取 /etc/vsftpd/user_list  文件中的用户列表。当列表中的用户登录FTP服务器时,该用户在提示输入密码之前就被禁止了。
    tcp_wrappers=YES      简易的防火墙,主机访问控制,在/etc/hosts.allow 和 /etc/hosts.deny   中设置


     

    几种配置模式:

    1.配置匿名登录
     第一步:#vi /etc/vsftpd/vsftpd.conf     

     anonymous_enable=YES               控制匿名登录
     anon_upload_enable=YES            是否允许匿名上传
     anon_mkdir_write_enable=YES   是否允许匿名写及创建目录的权限


    anon_root=/var/ftp/pub                       设置匿名用户的登录目录(如需要,需自己添加并修改)(权限不能为777)
    anon_other_write_enable=YES        
    打开匿名用户删除和重命名的权限(如需要,需自己添加)
    anon_umask=022                               
      匿名用户的掩码(如需要,需自己添加)含义:
                                                                      如umask是022,这时创建一个权限为666的文件,文件的实际权限为666-022=644
    anon_max_rate=100000                     匿名用户最大传输速率(如需要,需自己添加,
    默认不存在)
    anon_world_readable_only=NO    
    默认值为YES ,当所有人对该文件有读权限时,匿名用户才可下载
                                                                       设置为NO,允许匿名用户下载不具有全部读权限的文件

    chown_uploads=YES
                          改变上传文件的所有者为
    chown_username=whoever         当启用chown_uploads=YES时,所指定的属主用户账号

                                                                  此处的whoever自然要用合适的用户账号来代替

     
    第二部:打开默认共享目录的权限

             默认情况下,ftp的根目录为/var/ftp,为了安全,这个目录默认不允许设置为777权限,否则ftp将无法访问。但是我们要匿名上传文件,需要“other”用户的写权限,正确的做法:

      在/var/ftp中建立一个pub(名字自己设置)文件夹,将个文件夹权限设置为777(视具体需要自己设),在upload这个文件夹中,匿名用户可以上传文件、创建文件夹、删除文件等。

      一般至此,便实现vsftpd匿名用户的上传下载了

    #chmod 777 /var/ftp/pub

    第三部:启动vsftpd服务器:

    #service vsftpd restart

    #service iptables stop

     
    第四:常见问题
    (有时发现匿名用户,可以上传文件,可以创建目录,但是不能下载文件!!!)
            问题出在哪里 首先排除selinux的问题,我们看一下文件和目录的权限设置

    [root@localhost ftp]# ll
    total 4
    drwxrwxrwx 3 root root 4096 Feb  3 17:01 pub   这里权限是777

    [root@localhost pub]# ll
    total 348
    -rw------- 1 ftp ftp 348559 Feb  3 16:58 1062321425.png        这里是600
    drwx------ 2 ftp ftp   4096 Feb  3 17:00 123                                这里是700
    -rw------- 1 ftp ftp      0 Feb  3 17:01 新建文本文档.txt                这里是600
             /etc/vsftpd/vsftpd.conf下只有local_umask=022(文件644,目录755)的选项,它决定了上传以后的文件的权限。默认文件权限是666,目录权限是777,然而我们的匿名用户上传的文件的权限却没有写明,那么默认的vsftp服务对匿名用户的umask值是多少呢?根据我们的测试,应该是066(文件600  目录711)。所以我们要修改匿名用户的umask值使我们可以进行一些相关操作。
    解决方案:

    增加anon_umask=022,重启服务 


    2.本地账号登录

     第一步:修改配置文件 #vi /etc/vsftpd/vsftpd.conf

    anonymous_enable=NO     禁止匿名用户登录
            local_enable=YES                
    允许本地帐号登录 
            
    write_enable=YES                控制本地用户可写权限      
            local_umask=022
                      本地文件的权限掩码 (目录755  文件644)



       
    local_root=/var/www/html
     
    修改系统用户登录后的目录(默认为用户家目录)(如需要,需自己添加)
                                任何一个用户ftp登录到这个服务器上都会chroot到/var/www/html目录下

    local_max_rate=5000     
    以(字节/秒 B/S)为单位限制本地帐号的上传速度  (如需要,需自己添加)

    第二步:设置用户登陆权限
    • 首先在  /etc/vsftpd/ftpusers文件中的用户是肯定不能访问ftp服务器的
    • 当参数设置为
      userlist_enable=YES           启用/etc/vsftpd/user_list列表
      userlist_deny=NO                 (不启用)只允许/etc/vsftpd/user_list列表中的用户登录(ftpusers限制仍然起作用)
    • 当参数设置为 (默认)
      userlist_enable=YES           启用/etc/vsftpd/user_list列表
      userlist_deny=YES              (启用)禁止/etc/vsftpd/user_list列表中的用户登录(ftpusers限制仍然起作用)

    第三步:设置用户登陆后位置

    • chroot_local_user=YES     把所有的本地用户限制在自己的家目录下(下面的chroot设置将无效)
                                                        YES为启用 NO禁用.(包括注释掉也为禁用)
    • chroot_local_user=NO       如需限制部份用户把配置设为NO,并把下面的两个启用
      chroot_list_enable=YES
         (启用例外列表) 本选项为YES 则 chroot_list文件中的名单可以不受限制
      chroot_list_file=/etc/vsftpd/chroot_list       
      文件地址
                    (注意:vsftpd.chroot_list 是没有创建的需要自己添加,要想控制帐号就直接在文件中加帐号即可)


    (在实际部署中,一旦启用 chroot_local_user=YES   发现ftp就不能登录了,报错内容如下

    500 OOPS: vsftpd: refusing to run with writable root inside chroot()
    经查询,
    这个问题发生在最新的这是由于下面的更新造成的:

    1. - Add stronger checks for the configuration error of running with a writeable root directory inside a chroot(). This may bite people who carelessly turned on chroot_local_user but such is life.  

     

     

    从2.3.5之后,vsftpd增强了安全检查,如果用户被限定在了其主目录下,则该用户的主目录不能再具有写权限了

    这样对于ftp根直接为网站根目录的用户不方便,所以建议假如ftp根目录是/home/centos,那么网站结构可以这样分,/home/centos/log为日志目录,/home/centos/web为网站根目录,这样我们就可以去掉/home/centos目录的写入权限而不影响网站的正常运行
     
     
     
    2.2本地多用户 登陆不同目录
    创建三个用户(只能登陆ftp,不能远程登录shell) 
     useradd -s /sbin/nologin aaa
     useradd -s /sbin/nologin bbb
     useradd -s /sbin/nologin ccc
     
     
    设置密码:
    passwd aaa 
    ……
    passwd bbb
    ……
    passwd ccc 
    ……
     
    设置vsftp.conf
    local_root=/      用户默认登录路径
    chroot_local_user=YES 锁定用户到各自目录为其根目录
    user_config_dir=/etc/vsftpd/userconfig     用户配置目录
     
    创建/etc/vsftpd/userconfig 文件夹
    [root@ vsftpd]# mkdir userconfig

    创建各用户配置文件
    [root@ vsftpd]# cd userconfig/
    [root@ userconfig]# vim aaa
    [root@ userconfig]# vim bbb
    [root@ userconfig]# vim ccc
     
    编辑配置文件内容
    在各个文件中 写入  local_root=/各用户登陆后的目录
    例如:
    [root@ userconfig]# cat aaa
    local_root=/usr/local/
     
     
     
     
     
     

    3.一些其它设置
       
    listen_port=2121  设置非标准监听端口
         设置基于不同IP 的虚拟合主机(不做详细介绍)


    4.配置虚拟用户

    1.生成虚拟用户口令库文件。为了建立此口令库文件,先要生成一个文本文件。该文件的格式如下,单数行为用户名,偶数行为口令:

       #vi account.txt
    ylg
    1234
    zhanghong
    4321
    gou
    5678
     
        2.生成口令库文件,并修改其权限:

    #db_load -T -t hash -f ./account.txt /etc/vsftpd/account.db
    #chmod 600 /etc/vsftpd/account.db
     
        3.新建一个虚拟用户的PAM文件。加上如下两行内容:

    #vi /etc/pam.d/vsftp.vu
    auth required /lib/security/pam_userdb.so db=/etc/vsftpd/account
    account required /lib/security/pam_userdb.so db=/etc/vsftpd/account
     
        4.建立虚拟用户,设置该用户所要访问的目录,并设置虚拟用户访问的权限:

    #useradd -d /home/ftpsite virtual_user
    #chmod 700 /home/ftpsite
     


        经过该步骤的设置,/home/ftpsite就是virtual_user用户的主目录,该用户也是ftpsite目录的拥有者。除root用户之外,只有该用户具有对该目录的读、写和执行的权限。


        5.编辑/etc/vsftpd/vsftpd.conf文件,使其整个文件内容如下所示(去掉了注释内容):

    anonymous_enable=NO
    local_enable=YES
    local_umask=022
    xferlog_enable=YES
    connect_from_port_20=YES
    xferlog_std_format=YES
    listen=YES
    write_enable=YES
    anon_upload_enable=YES
    anon_mkdir_write_enable=YES
    anon_other_write_enable=YES
    one_process_model=NO
    chroot_local_user=YES
    ftpd_banner=Welcom to my FTP server.
    anon_world_readable_only=NO
    guest_enable=YES                 激活虚拟用户登陆功能
    guest_username=virtual_user   指定虚拟用户对应的系统真实用户
    pam_service_name=vsftp.vu     修改原配置文件中此选项的值(要与 3  中的文件名一致)
     


        上面代码中,guest_enable=YES表示启用虚拟用户;guest_username=virtual则是将虚拟用户映射为本地用户,这样虚拟 用户登录后才能进入本地用户virtual的目录/ftpsite;pam_service_name=vsftp.vu指定PAM的配置文件为 vsftp.vu。

        6.重新启动VSFTP:
        #service vsftpd restart

        7.以虚拟用户gou(Linux中并无该账号)进行测试:

    # ftp 127.0.0.1
    Connected to 127.0.0.1 (127.0.0.1).
    220 Welcom to my FTP server.
    Name (127.0.0.1:root): gou
    331 Please specify the password.
    Password:
    230 Login successful. Have fun.
    Remote system type is UNIX.
    Using binary mode to transfer files.
     


        在虚拟FTP服务器中,也可以对各个用户的权限进行设置。方法是在/etc/vsftpd.conf文件中添加如下一行:
        user_config_dir=用户配置文件目录

        然后在用户配置文件目录下创建相应的用户配置文件,比如为上述名为mike的用户创建一个配置文件(假设配置文件目录为/etc/vsftpd_user_conf):

    #vi /etc/vsftpd_user_conf/mike
    write_enable=NO
    anono_upload_enable=NO
     
       8.虚拟用户个人目录设置

      大家可以发现,无论是哪个虚拟用户,登录后所在的目录都是/home/ftpsite,即都是guest用户的自家目录。下面,介绍如何为每个虚拟用户建立自家目录。
      一种作法是在虚拟用户的个人配置文件中使用local_root选项指定虚拟用户的自家目录。以mike为例,在第上步的基础上,
        首先/etc/vsftpd_user_conf/mike文件中加入:
      local_root=/home/ftpsite/mike

      /home/ftpsite下新建mike目录,并将权限设为virtual_user:


      9.添加FTP用户的步骤

          1.在account.txt中添加用户名和密码

          2.运行如下命令,将用户名和密码添加到数据库中

            db_load -T -t hash -f ./account.txt /etc/vsftpd/account.db

          3.在/home/ftpsite中新建一个文件夹,与用户明相同

          4.在vsftpd_user_conf文件夹下新建和用户名相同的文件,并在其中加入
           local_root=/home/ftpsite/用户名



  • 相关阅读:
    基于redis实现的延迟消息队列
    Redis实现求交集操作结果缓存的设计方案
    限流算法之漏桶算法、令牌桶算法
    Apache设置防DDOS模块mod_evasive
    FastCGI技术
    详解强大的SQL注入工具——SQLMAP
    nginx根据域名做http,https分发
    Nginx配置SSL证书部署HTTPS网站
    JProfiler学习笔记
    Mysql压测工具mysqlslap 讲解
  • 原文地址:https://www.cnblogs.com/centos2017/p/7896811.html
Copyright © 2011-2022 走看看