1、Vsftp简介
vsftp是一个基于GPL发布的类Unix系统上使用的ftp服务器软件,全称是Very Secure FTP,从此名称可以看出来,编制者的初衷是代码的安全。
2、vsftp安装及vsftp服务的配置文件
2.1 vsftp的安装
通常,vsftp不会自动安装,需要我们自己安装,可以自己下载来安装,但自带安装包里会有,通过Add/Remove Software添加vsftpd包就可以了。
如果不能通过安装包安装,在连网的情况下,用root账号执行yum install vsftpd安装vsftp服务器,用yum install ftp安装ftp客户端。
2.2 vsftp配置文件说明(作为说明,可跳过)
vsftp服务的配置文件默认位于/etc/vsftpd/文件夹中,主要包括用户控制列表文件(ftpusers, user_list)和主配置文件(vsftpd.conf)。ftpusers和user_list文件中包含一份FTP用户的列表,两个文件虽然都用于FTP用户的控制,但是具体作用存在一些差异
ftpusers文件:该文件包含的用户账户将被禁止登录vsftp服务器,不管该用户是否在user_list文件中出现。通常将root,bin,daemon等特殊用户在该文件中,禁止用于登录FTP服务
user_list文件:该文件中包含的用户账户可能被禁止登录,也可能被允许登录,具体在主配置文件vsftpd.conf中决定。当存在“userlist_enable=YES”配置项时,user_list文件生效,如果配置“userlist_deny=YES”则仅禁止列表中的用户账户登录,如果配置“userlist=deny=NO”则仅允许列表中的用户账户登录
ftpusers文件为vsftpd服务器提供了一份用于禁止登录的FTP用户列表,而user_list文件提供了一份可灵活控制的FTP用户列表。二者相互结合,为FTP用户控制提供了良好的基础。
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#不允许匿名用户访问
#anon_root=/var/ftp/pub/
#设定匿名登录的主目录,默认是anon_root=/var/ftp/
## Uncomment this to allow local users to log in.local_enable=YES
#设定本地用户可以访问。主要是为虚拟宿主用户,如果该项设为NO,则所有虚拟用户将无法访问。
## 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=NO
#禁止匿名用户上传
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
anon_mkdir_write_enable=NO
#禁止匿名用户建立目录
#
# 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
#设定端口20进行数据连接
#
# 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=NO
#设定禁止上传文件更改宿主
#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=/var/log/xferlog
#可以把xferlog_file=/var/log/vsftpd.log,设定vsftpd的服务日志保存路径,该文件要手动建立,并且必须给与该vsftpd用户对日志文件vsftpd.log的读写权限,否则服务将启动失败。
#
# 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
nopriv_user=vsftpd
#设定支撑vsftpd服务的宿主用户为手动建立的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_download_enable=YES
#设定支持ASCII模式的上传和下载功能
#
# You may fully customise the login banner string:
ftpd_banner=Welcome to blah FTP service.
#设定vsftpd的登陆标语
#
# 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
#禁止用户登出自己的FTP主目录
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list
#如果开启了chroot_list_enable=YES,那么一定要开启这个,这条是锁定登录用户只能加目录的位置。建立chroot_list文件touch /etc/vsftpd/chroot_list,然后将账户输入一行一个,保存就可以了。如果不需要限制用户,也可以只建立一个空文件,或者将chroot_list_enable=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
#禁止用户登陆FTP后使用“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
#
# 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
#使用虚拟用户需要增加以下部分,默认中不包含这些设定项目,需要自己手动添加:
guest_enable=YES
#设定启动虚拟用户功能
guest_username=vsftpd
#指定虚拟用户的宿主用户。
virtual_use_local_privs=YES
#设定虚拟用户的权限符合他们的宿主用户。
user_config_dir=/etc/vsftpd/vconf
#设定虚拟用户个人vsftpd的配置文件存放路径。这个被指定的目录里,将存放每个vsftpd虚拟用户个性的配置文件,注意,就是这些配置文件名必须和虚拟用户名相同。
匿名用户对应的系统账号为ftp,其宿主目录为“/var/ftp”该目录也就是匿名用户访问vsftp服务时所在的根目录(可以通过anon_root配置项更改)。将其中pub子目录的属主修改为ftp,保留其可写权限,可提供给匿名用户上传文件使用。命令如下:
[root@linux ~]# ls -dl /var/ftp/pub///查看vsftp的默认根目录权限
drwxr-xr-x 2 root root 4096 2009-12-04 /var/ftp/pub/
[root@linux ~]# chown ftp /var/ftp/pub///修改默认根目录的属主
[root@linux ~]# ls -dl /var/ftp/pub/
drwxr-xr-x 2 ftp root 4096 2009-12-04 /var/ftp/pub/
还可以通过修改/var/ftp/pub/目录的读写权限,让本地用户都可以对该目录添加删除操作
[root@linux ~]# chmod a+w /var/ftp/pub/
这里的读写权限是Linux系统下更改普通用户对文件夹的读写操作权限,不涉及ftp,所以即使普通用户可以在/var/ftp/pub/目录下添加删除文件,而通过ftp匿名登录的读写权限需要根据vsftpd.conf的配置。
如果开启匿名访问,且不想让匿名用户访问ftp根目录(假设ftp匿名根目录为anon_root=/var/ftp/),但可以访问ftp根目录下的子目录(例如/var/ftp/pub/文件夹),可以通过修改文件夹的读写操作权限,把/var/ftp/设置成可执行但不可读不可写,/var/ftp/pub/设置成可读可执行不可写。命令如下:
[root@linux var]# chmod 771 ftp
[root@linux ftp]# chmod 777 pub
注意:红色的var和ftp表示需要进入到该目录下执行命令。这样匿名用户通过ftp://[ip]是查看不到东西的,但通过ftp://[ip]/pub/可以看到ftp上的文件。这样做的一个好处是为了不让匿名用户通过简单的地址就探测到ftp上的文件,但可以通过下一级子目录查看。
3、设置防火墙端口
FTP服务器默认使用TCP协议的20、21端口与客户端进行通信。21端口用于建立控制连接,并传输控制命令,20端口用于建立数据连接,并传输文件数据。而Linux系统默认下没有开启这两个端口,因此需要修改/etc/sysconfig/iptables文件,用root命令修改iptables配置文件,添加如下红色部分,注意,只添加红色部分,其他的不要动。
-A RH-Firewall-1-INPUT-p icmp --icmp-type any -j ACCEPT
-ARH-Firewall-1-INPUT -p 50 -j ACCEPT
-ARH-Firewall-1-INPUT -p 51 -j ACCEPT
-ARH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-ARH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-ARH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-ARH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-ARH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp--dport 21 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp--dport 20 -j ACCEPT
-ARH-Firewall-1-INPUT -m state --state NEW -m udp -p udp --dport 137 -j ACCEPT
-ARH-Firewall-1-INPUT -m state --state NEW -m udp -p udp --dport 138 -j ACCEPT
-ARH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 139 -j ACCEPT
-ARH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 445 -j ACCEPT
-ARH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
-ARH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 23 -j ACCEPT
-ARH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-ARH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-ARH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
其中,如果是开放连续的端口,也可以用[port1:port2]来表示从port1到port2,例如
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp--dport 20:22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp--dport 5901:5905 -j ACCEPT
此外,还要修改/etc/sysconf/iptables-config文件,修改
IPTABLES_MODULES=""
为
IPTABLES_MODULES="ip_nat_ftp ip_conntrack_ftp
然后重启防火墙即可。命令如下:
[root@linux ~]# service iptables restart
如果不修改/etc/sysconf/iptables-config文件,会出现这种状况,可以正常登陆ftp,但是不能上传或下载,甚至使用ls命令都不可以,例如当你使用命令行登陆ftp:
……
230 Login successful
Remote system type is UNIX
Using binary mode to transfer files
ftp> ls
227 Entering Passive Mode(…)
ftp:connect:No route to host。
换句话说,当你出现ftp:connect:Noroute to host问题时,就要修改/etc/sysconf/iptables-config文件。
4、修改/etc/selinux/config文件
把默认SELINUX=enforcing改成SELINUX=Disabled #这样就把selinux服务器关掉了,重启系统或者使用命令source /etc/selinux/config使selinux配置生效。如果问题还没有解决,查看ftp的状态:
[root@linux ~]# sestatus -b | grep ftp
在出现的结果中可以看到“ftp_home_diroff”或者“tftpd_disable_trans off"之类的,只要把其中之一设置为on就可以了。
[root@linux ~]# setsebool -P ftp_home_dir on
或者
[root@linux ~]# setsebool -P ftpd_disable_trans on
重启vsftp
5、vsftp启动和停止
做完前面的配置后,就可以启动vsftpd服务了,其实安装完vsftpd就可以启动的,这么写主要是不想重复写或者提高效率。
使用service vsftpd start、service vsftpdstop、service vsftpd restart来对vsftp启动、停止和重启。也可以通过“/etc/init.d/vsftpd”脚本可以控制vsftp服务的启动和终止,或手动执行“/usr/sbin/vsftpd”程序。
6、一些实例说明
6.1、匿名用户的下载
匿名用户默认是可登陆,可下载。vsftpd的默认主目录是/var/ftp,且主目录的权限使755,文件权限百度查看http://zhidao.baidu.com/question/168415347.html,如果把目录权限改成750,则匿名用户登录,既看不到文件也下载不了。
6.2、匿名用户的上传
设置/etc/vsftpd/vsftpd.conf的字段如下
anonymous_enable=YES
anon_upload_enable=YES 允许匿名用户上传文件
anon_mkdir_write_enable=YES 允许匿名用户创建目录
#ftp 202.205.84.163
Connected to 202.205.84.163
220 (vsFTPd 2.0.5)
530 Please login with USER and PASS.
530 Please login with USER and PASS.
KERBEROS_V4 rejected as an authentication type
Name (10.1.1.35:root): ftp
331 Please specify the password.
Password: --ftp用户的默认密码也是ftp
ftp>? #问号去查看命令帮助
ftp>help ls #看命令的帮助,help+空格+某一命令
ftp>ls #ls命令显示ftp服务端目录的信息
ftp>!ls # !ls命令显示ftp客户端目录的信息
ftp>cd upload #cd命令用于在ftp服务端目录的切换
ftp>lcd /etc #lcd命令用于在ftp客户端目录的切换
ftp>put inittab #put命令用于上传文件,如本命令,表示上传/etc/inittab至/var/ftp/upload,ftp的put命令不支持绝对路径。
ftp>cd /var/ftp/download
ftp>get hello.c #get命令用于下载文件,如本命令,表示下载download目录下的hello.c文件到客户端目录下。
6.3、允许匿名用户下载刚上传的文件
默认下,匿名用户是无法下载匿名用户上传的文件的,此时需要一些改变。
方法一:
[root@linux ~]# vi /etc/vsftpd/vsftpd.conf
添加一行:anon_world_readable_only=NO
重启服务:service vsftpd restart
方法二:
[root@linux ~]# vi /etc/vsftpd/vsftpd.conf
把local_umask=022改为local_umask=0222
重启服务。
7、配置vsftpd的要点
1)开启ftp的端口号,或者关闭防火墙。通常前者,否则访问不了
2)selinux关闭,否则连接失败或者上传不了文件
3)配置文件正确配置
4)目录权限的正确设置
8、其他
其他配置请见参考资料。
参考资料:
http://bbs.51cto.com/topic/thread-811192.html
http://hi.baidu.com/dspace/blog/item/a3eef603546c11f909fa935c.html
http://zhidao.baidu.com/question/53971257.html
http://apps.hi.baidu.com/share/detail/15533820
http://www.linuxdiyf.com/viewarticle.php?id=169922