在Centos6.3上搭建FTP服务器vsftpd
vsftpd是Linux平台下非常著名的一个ftp软件。vsftpd是 very secure ftp daemon 的缩写。

 一、安装vsftpd
 先检查系统中是否安装了vsftpd
 [root@localhost Packages]# rpm -qa | grep vsftpd
 [root@localhost Packages]# 

 如果没安装,就继续下面安装。把 CentOS 的第一张安装光盘插入光驱,在光盘的Packages目录下找到vsftpd安装包,然后安装。
 [root@localhost Packages]# ls *vsftpd*
 vsftpd-2.2.2-11.el6.x86_64.rpm
 [root@localhost Packages]# rpm -ivh vsftpd-2.2.2-11.el6.x86_64.rpm 
 Preparing...                ########################################### [100%]
    1:vsftpd                 ########################################### [100%]

                                                                                        
 二、启动vsftpd服务

 [root@localhost Packages]# service vsftpd status
 vsftpd is stopped
 [root@localhost Packages]# service vsftpd start
 Starting vsftpd for vsftpd:                                [  OK  ]

 vsftpd的匿名用户的默认主目录是/var/ftp,vsftpd 的配置文件位于/etc/vsftpd目录下。 三、测试在客户端访问vsftpd服务器的/var/ftp目录。
在Windows系统的资源管理器的“地址”栏中输入“ftp://Centos系统的IP地址”,回车即显示当前/var/ftp目录下的内容啦。

四、vsftpd 匿名用户的上传和下载权限的配置

 很多朋友配置vsftpd时不能使用匿名用户上传和下载(创建目录或删除、重命名文件夹),本文主要解决vsftpd的匿名用户权限配制问题。

 配置要注意三部分,请一一仔细对照:

 1. vsftpd.conf文件的配置(vi /etc/vsftpd/vsftpd.conf)
 #允许匿名用户登录FTP(如果不允许匿名访问就设置为NO,后面有添加FTP用户的详细介绍)
 anonymous_enable=YES
 #打开匿名用户的上传权限
 anon_upload_enable=YES
 #打开匿名用户创建目录的权限
 anon_mkdir_write_enable=YES#允许匿名用户具有建立目录和上传之外的权限,如重命名、删除等。
anon_other_write_enable=YES

 2. ftp目录的权限设置
 ftp的根目录为/var/ftp,为了安全,这个目录默认不允许设置为777权限,否则ftp将无法访问。/var/ftp/pub是匿名用户的默认访问目录,即vsftpd.conf文件中没有anon_root设置时的默认目录。不知为何,如果用anon_root指定其它目录后,在终端用ftp命令匿名登录就失败,并提示:500 OOPS: vsftpd: refusing to run with writable anonymous root 错误,目前还没有找到具体原因和解决办法。一般至此,便实现vsftpd匿名用户的上传下载了,如果还不行,就按下面说明配置一下selinux。

 3. selinux的配置
 SELinux(Security-Enhanced Linux) 是美国国家安全局(NAS)对于强制访问控制的实现,是 Linux上最杰出的新安全子系统。NSA是在Linux社区的帮助下开发了一种访问控制体系,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。SELinux 默认安装在 Fedora 和 Red Hat Enterprise Linux 上,也可以作为其他发行版上容易安装的包得到。
   
 最简单的办法是关闭selinux
 方法1:修改/etc/selinux/config文件中的SELINUX="" 为 disabled ,然后重启。(验证通过)
 方法2:用命令setenforce 0,无需重启。(setenforce的格式:setenforce [ Enforcing | Permissive | 1 | 0 ]) (转述的,没试过)
 方法3:在lilo或者grub的启动参数中增加:selinux=0,也可以关闭selinux。(转述的,没试过)
   
 使用getenforce查看当前selinux是否正在运行。
   
 如果不关闭selinux,就要设置selinux的ftp权限。(转述的,没试过)
 (1)使用getsebool -a | grep ftp查看ftp相关设置状态,我们要将allow_ftpd_anon_write设为on。
 (2)使用setsebool -P 进行设置。例:setsebool -P allow_ftpd_anon_write=on。
    或使用togglesebool进行bool值取反,例如togglesebool allow_ftpd_anon_write。
 (3)修改selinux安全上下文,先介绍两个命令:
    命令1:ls -Z  ps -Z  id -Z  # 分别可以看到文件,进程和用户的SELinux属性
    命令2:#chcon 改变SELinux安全上下文
    chcon -u   对象
        -r 
        -t 
        -R 递归
        --reference 源文件 目标文件          # 复制安全上下文
    使用方法:
     步骤1:ls -Zd /var/ftp/upload/ 通常会看到:
      drwxr-xr-x ftp root system_u:object_r:public_content_t /var/ftp/upload/
     步骤2、chcon -R -t public_content_rw_t /var/ftp/upload/
     步骤3、ls -Zd /var/ftp/upload/ 如果看到如下信息就OK了:
      drwxr-xr-x ftp root system_u:object_r:public_content_rw_t /var/ftp/upload/
   最后还是重启下selinux和vsftpd吧,不重启其实也没关系。重新登录到ftp上,应该就能解决问题了。
   另,selinux的图形界面 可由system-config-selinux命令进入。
===============================================================================
下面转载一个关于FTP的好帖子,转自:  http://www.bccn.net/Article/czxt/linux/200806/7515.html

FTP服务器的简介就不介绍了,相信大家都知道是什么东东。不了解的就google吧!这里用到的FTP服务器软件是非常著名的vsftpd.

 1.安装vsFTPd软件
 源码安装就不说了,可以google,因为在线安装很方便的.
 Fedora下安装很简单 yum install vsftpd 即可.

 2.启动/重启/关闭vsftpd服务器
 [root@localhost ftp]# /sbin/service vsftpd restart
 Shutting down vsftpd:                                      [  OK  ]
 Starting vsftpd for vsftpd:                                [  OK  ]
 OK表示重启成功了.
 启动和关闭分别把restart改为start/stop即可.
 如果是源码安装的,到安装文件夹下找到start.sh和shutdown.sh文件,执行它们就可以了.

 3.与vsftpd服务器有关的文件和文件夹
 vsftpd服务器的配置文件的是: /etc/vsftpd/vsftpd.conf

 vsftpd服务器的根目录,即FTP服务器的主目录:
 [root@localhost ftp]# more /etc/passwd|grep ftp
 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
 这样你就能看到FTP的服务器的目录在/var/ftp处
 如果你想修改服务器目录的路径,那么你只要修改/var/ftp到别处就行了

 4.添加FTP用户
 有的FTP服务器需要用户名和密码才能登录,就是因为设置了FTP用户和权限.
 FTP用户一般是不能登录系统的,只能进入FTP服务器自己的目录中,这是为了安全.这样的用户就叫做虚拟用户了.实际上并不是真正的虚拟用户,只是不能登录SHELL了而已,没能力登录系统.

 /usr/sbin/adduser -d /opt/test_ftp -g ftp -s /sbin/nologin test
 这个命令的意思是:
 使用命令(adduser)添加test用户,不能登录系统(-s /sbin/nologin),自己的文件夹在(-d /opt/test_ftp)),属于组ftp(-g ftp)
 然后你需要为它设置密码:passwd test
 这样就添加了一个FTP用户了.下面的示例可以帮助你进入FTP服务器了.

 [root@localhost ftp]# ftp
 ftp> open 192.168.0.33
 Connected to 192.168.0.33 (192.168.0.33).
 220 (vsFTPd 2.0.5)
 Name (192.168.0.33:gxl): test
 331 Please specify the password.
 Password:
 230 Login successful.
 Remote system type is UNIX.
 Using binary mode to transfer files.
 ftp> quit
 221 Goodbye.

 在windows中,只要在浏览器中输入 ftp://192.168.0.33 进入FTP服务器,然后 右键 登录,输入用户名和密码就可以登录自己的目录了.
 当然你要保证自己能读写自己的目录,就要在配置文件vsftpd.conf里设置一下就可以读写了.
 local_enable=yes
 write_enable=yes
 local_umask=022

 5.匿名上传下载
 修改配置文件即可vsftpd.conf,确定有以下几行,没有自己添加进去就可以了.
 anonymous_enable=yes
 anon_upload_enable=yes
 anon_mkdir_write_enable=yes
 anon_umask=022

 然后你可以新建一个文件夹,修改它的权限为完全开放,任何用户就可以登录这个文件夹,并上传下载文件:
 mkdir /var/ftp/guest
 chmod 777 /var/ftp/guest

 6.定制进入FTP服务器的欢迎信息
 在vsftpd.conf文件中设置:
 dirmessage_enable=yes
 然后进入用户目录建立一个.message文件,输入欢迎信息即可(我这里写入的是Welcome to gxlinux's FTP!):
 [root@localhost test_ftp]# ftp 192.168.0.33
 Connected to 192.168.0.33 (192.168.0.33).
 220 (vsFTPd 2.0.5)
 Name (192.168.0.33:gxl): test
 331 Please specify the password.
 Password:
 230-Welcome to gxlinux's FTP!
 230 Login successful.
 Remote system type is UNIX.
 Using binary mode to transfer files.

 7.实现虚拟路径
 将某个目录挂载到FTP服务器下供用户使用,这就叫做虚拟路径.
 比如将gxl用户的目录挂载到FTP服务器中,供FTP服务器的用户使用,使用如下命令即可:
 [root@localhost opt]# mount --bind /home/gxl /var/ftp/pub #使用挂载命令
 [root@localhost opt]# ls /var/ftp/pub
 LumaQQ  Screenshot.png 桌面

 8.打开vsFTPd的日志功能
 添加下面一行到vsftpd.conf文件中,一般情况下该文件中有这一行,只要把前面的注释符号#去掉即可,没有的话就添加,或者修改:
 xferlog_file=/var/log/vsftpd.log

 9.限制链接数,以及每个IP最大的链接数
 修改配置文件中,例如vsftp最大支持链接数100个,每个IP能支持5个链接:
 max_client=100
 max_per=5

 10.限制传输速度
 修改配置文件中,例如让匿名用户和vsftd上的用户(即虚拟用户)都以80KB=1024*80=81920的速度下载
 anon_max_rate=81920
 local_max_rate=81920

 11.将用户(一般指虚拟用户)限制在自家目录
 修改配置文件中,这样用户就只能访问自己家的目录了:
 chroot_local_user=yes
 如果只想某些用户仅能访问自己的目录,其它用户不做这个限制,那么就需要在chroot_list文件(此文件一般是在/etc/vsftpd/中)中添加此用户.
 编辑此文件,比如将test用户添加到此文件中,那么将其写入即可.一般的话,一个用户占一行.
 [root@localhost vsftpd]# cat chroot_list
 test

 12.绑定某个IP到vsFTPd
 有时候要限制某些IP访问服务器,只允许某些IP访问,例如只允许192.168.0.33访问这个FTP,同样修改配置文件:
 listen_address=192.168.0.33
======================================================================= 
自己配置的 vsftpd.conf (主动模式和被动模式都支持(已验证),还可以输出日志文件)

[root@localhost vsftpd]# pwd
 /etc/vsftpd
 [root@localhost vsftpd]# cat 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=NO
 #
 # Uncomment this to allow local users to log in.
 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.
 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
 #
 # 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
 #
 # 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
 #
 # 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=/ftptest/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
 #
 # 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
 #
 # 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_download_enable=YES
 #
 # You may fully customise the login banner string:
 #ftpd_banner=Welcome to blah FTP service.
 #
 # 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().
 #chroot_local_user=YES
 #chroot_list_enable=YES
 # (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
 #
 # 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
 #
 # 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

 pam_service_name=vsftpd
 userlist_enable=YES
 tcp_wrappers=YES
 #
 # other...

 anon_other_write_enable=YES
 #anon_root=/var/ftp/pub
 dual_log_enable=YES
 # 指定日志目录:
 vsftpd_log_file=/ftptest/vsftpd.log    
 # 开启主动模式:  
 port_enable=YES
 # 开启被动模式(默认开启被动模式,想关闭被动模式需要设置NO):  
 pasv_enable=YES
 # 指定被动模式端口范围:  
 pasv_min_port=50000
 pasv_max_port=60000

 [root@localhost vsftpd]#