FTP连接出现:Failed to retrieve directory listing

想将一些博客上面的配置信息下载到本地来,所以为了方便,就临时用vsftp软件建立了FTP服务。不过,在使用FillaZilla客户端连接该FTP时,却出现了如下的错误信息:

Status:	Resolving address of smilejay.cn
Status:	Connecting to 192.3.170.172:21...
Status:	Connection established, waiting for welcome message...
Response:	220 (vsFTPd 2.2.2)
Command:	USER anonymous
Response:	331 Please specify the password.
Command:	PASS **************
Response:	230 Login successful.
Command:	SYST
Response:	215 UNIX Type: L8
Command:	FEAT
Response:	211-Features:
Response:	 EPRT
Response:	 EPSV
Response:	 MDTM
Response:	 PASV
Response:	 REST STREAM
Response:	 SIZE
Response:	 TVFS
Response:	 UTF8
Response:	211 End
Command:	OPTS UTF8 ON
Response:	200 Always in UTF8 mode.
Status:	Connected
Status:	Retrieving directory listing...
Command:	PWD
Response:	257 "/"
Command:	TYPE I
Response:	200 Switching to Binary mode.
Command:	PAS
Response:	227 Entering Passive Mode (192,3,170,172,132,153).
Command:	LIST
Error:	Connection timed out
Error:	Failed to retrieve directory listing

简单排查原因,发现FTP 21端口也打开了的,防火墙也打开对TCP 21端口的访问,登录权限也是配置好了的。
后来,将iptables防火前关闭后,就可以正常了使用了。
但是,为什么呢?

在服务器端用 watch netstat -tnl 命令查看网络连接情况,然后用FileZilla去连接FTP,会看到某个时刻(其实是发送了PASV命令后),服务器端会看到多了一个奇怪的IP端口连接,连接好后就消失了。
其实,这是FTP passive mode 和 active mode 的区别(不细说了看后面的参考资料吧),然后FileZilla默认使用的是passive mode(当然自己也可以去“设置”中更改)。

当然,我前面说将iptables关闭了,我也想重新把iptables重新启用,那么怎么设置访问哪个端口的规则呢?
当使用passive模式时,FTP服务器端会使用一个TCP端口,可通过下面的来配置:

pasv_enable=YES  # default YES. When enabled, passive mode connects are allowed.
pasv_min_port=9900
pasv_max_port=9903
port_enable=YES   # default NO. When enabled, active mode connects are allowed.

然后将9900、9901、9902、9903等4个端口的访问权限都在iptables中打开即可。

参考资料:
http://en.wikipedia.org/wiki/File_Transfer_Protocol
http://www.centos.org/docs/rhel-rg-en-3/s1-ftp-vsftpd-conf.html
http://stackoverflow.com/questions/4723023/vsftpd-error-listing-directories

master

Stay hungry, stay foolish.

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

*