FTP

----//服务配置

'a'.匿名下载:vsftpd安装好后,默认开启此项功能;

'b'.匿名上传:

1>.write_enable=YES //只有write_enable=YES之后,其他写权限才能生效(such as:anon_mkdir_enable)
anon_upload_enable=YES
/*anon_mkdir_write_enable=YES
anon_other_write_enable=NO*/

2>.为/var/ftp/pub目录o+w //匿名上传时,匿名用户都属于others,所以o+w
chmod o+w pub

3>.设置SELinux
setsebool -P setsebool allow_ftpd_full_access on

'c'.测试
1>.ftp 192.168.0.4


'注:'匿名上传/var/ftp/pub时,传上去的文件属于[ftp用户]和 [ftp组]:
ftp> ls
227 Entering Passive Mode (127,0,0,1,76,31).
150 Here comes the directory listing.
1>-rw------- 1 14 50 943 Dec 27 10:05 dfdfdfdf //1,2,3为上传的文件,文件属于 匿名用户和组,[断开ftp链接后,查看为ftp用户和ftp组;]
2>-rw------- 1 14 50 943 Dec 27 10:03 fsss //ftp用户的umask使得,上传的文件 -rw------
3>-rw------- 1 14 50 42048 Dec 26 06:30 install.log
4>drwx------ 2 14 50 4096 Dec 26 06:32 aaa
5>drwx------ 2 14 50 4096 Dec 26 06:33 bbb
6>-rw-r--r-- 1 0 0 60 Dec 26 06:24 resolv.conf

'注':文件要想下载,匿名用户(即others)对该文件至少要有 r 权限,而以上上传的1,2,3文件不可以被下载。
若想使得上传的文件,others有 r(能被下载),可以在/etc/vsftpd/vsftpd.conf 加入 anon_mask=000:则上传文件的权限为666-000=666=rw-rw-rw-

详解
连接ftp时出现Entering Passive Mode是怎么回事解决办法
处理防火墙和其他网络连接问题时最常见的一个难题是主动 FTP 与被动 FTP 的区别以及如何完美地支持它们。幸运地是,本文能够帮助你清除在防火墙环境中如何支持 FTP 这个问题上的一些混乱。

  本文也许不像题目声称的那样是一个权威解释,但我已经听到了很多好的反馈意见,也看到了本文在许多地方被引用,知道了很多人都认为它很有用。虽然我一直在找寻改进的方法,但如果你发现某个地方讲的不够清楚,需要更多的解释,请告诉我!最近的修改是增加了主动 FTP 和被动 FTP 会话中命令的例子 。这些会话的例子应该对更好地理解问题有所帮助 。例子中还提供了非常棒的图例来解释 FTP 会话过程的步骤 。现在,正题开始了 ...

  基础

  FTP 是仅基于 TCP 的服务,不支持 UDP 。与众不同的是 FTP 使用 2 个端口,一个数据端口和一个命令端口(也可叫做控制端口) 。通常来说这两个端口是 21 -命令端口和 20 -数据端口 。但当我们发现根据( FTP 工作)方式的不同数据端口并不总是 20 时,混乱产生了 。

  主动 FTP

  主动方式的 FTP 是这样的:客户端从一个任意的非特权端口 N ( N>1024 )连接到 FTP 服务器的命令端口,也就是 21 端口 。然后客户端开始监听端口 N+1 ,并发送 FTP 命令 “port N+1” 到 FTP 服务器。接着 服务器会从它自己的数据端口( 20 )连接到客户端指定的数据端口( N+1 ) 。

  针对 FTP 服务器前面的防火墙来说,必须允许以下通讯才能支持主动方式 FTP :

  任何端口到 FTP 服务器的 21 端口 (客户端初始化的连接 S<-C )

  FTP 服务器的 21 端口到大于 1024 的端口( 服务器响应客户端的控制端口 S->C )

  FTP 服务器的 20 端口到大于 1024 的端口( 服务器端初始化数据连接到客户端的数据端口 S->C )

  大于 1024 端口到 FTP 服务器的 20 端口(客户端发送 ACK 响应到 服务器的数据端口 S<-C )

  画出来的话,连接过程大概是下图的样子:



  在第 1 步中,客户端的命令端口与 FTP 服务器的命令端口建立连接,并发送命令 “PORT 1027” 。然后在第 2 步中, FTP 服务器给客户端的命令端口返回一个 "ACK" 。在第 3 步中, FTP 服务器发起一个从它自己的数据端口( 20 )到客户端先前指定的数据端口( 1027 )的连接,最后客户端在第 4 步中给 服务器端返回一个 "ACK"

  主动方式 FTP 的主要问题实际上在于客户端 。FTP 的客户端并没有实际建立一个到 服务器数据端口的连接,它只是简单的告诉 服务器自己监听的端口号, 服务器再回来连接客户端这个指定的端口 。对于客户端的防火墙来说,这是从外部系统建立到内部客户端的连接,这是通常会被阻塞的 。

  主动 FTP 的例子

  下面是一个主动 FTP 会话的实际例子 。当然 服务器名、 IP 地址和用户名都做了改动 。在这个例子中, FTP 会话从 testbox1.slacksite.com (192.168.150.80) ,一个运行标准的 FTP 命令行客户端的 Linux 工作站,发起到 testbox2.slacksite.com (192.168.150.90) ,一个运行 ProFTPd 1.2.2RC2 的 Linux 工作站 。debugging ( -d )选项用来在 FTP 客户端显示连接的详细过程 。红色的文字是 debugging 信息,显示的是发送到 服务器的实际 FTP 命令和所产生的回应信息 。服务器的输出信息用黑色字表示,用户的输入信息用粗体字表示 。

  仔细考虑这个对话过程我们会发现一些有趣的事情 。我们可以看到当 PORT 命令被提交时,它指定了客户端 (192.168.150.80) 上的一个端口而不是 服务器的 。当我们用被动 FTP 时我们会看到相反的现象 。我们再来关注 PORT 命令的格式 。就象你在下面的例子看到的一样,它是一个由六个被逗号隔开的数字组成的序列 。前四个表示 IP 地址,后两个组成了用于数据连接的端口号 。用第五个数乘以 256 再加上第六个数就得到了实际的端口号 。下面例子中端口号就是 ( (14*256) + 178) = 3762 。我们可以用 netstat 来验证这个端口信息 。

  testbox1: {/home/p-t/slacker/public_html} % ftp -d testbox2

  Connected to testbox2.slacksite.com.

  220 testbox2.slacksite.com FTP server ready.

  Name (testbox2:slacker): slacker

  ---> USER slacker

  331 Password required for slacker.

  Password: TmpPass

  ---> PASS XXXX

  230 User slacker logged in.

  ---> SYST

  215 UNIX Type: L8

  Remote system type is UNIX.

  Using binary mode to transfer files.

  ftp> ls

  ftp: setsockopt (ignored): Permission denied

  ---> PORT 192,168,150,80,14,178

  200 PORT command successful.

  ---> LIST

  150 Opening ASCII mode data connection for file list.

  drwx------ 3 slacker users 104 Jul 27 01:45 public_html

  226 Transfer complete.

  ftp> quit

  ---> QUIT

  221 Goodbye.

  被动 FTP

  为了解决 服务器发起到客户的连接的问题,人们开发了一种不同的 FTP 连接方式 。这就是所谓的被动方式,或者叫做 PASV ,当客户端通知 服务器它处于被动模式时才启用 。

  在被动方式 FTP 中,命令连接和数据连接都由客户端,这样就可以解决从 服务器到客户端的数据端口的入方向连接被防火墙过滤掉的问题 。当开启一个 FTP 连接时,客户端打开两个任意的非特权本地端口( N > 1024 和 N+1 ) 。第一个端口连接 服务器的 21 端口,但与主动方式的 FTP 不同,客户端不会提交 PORT 命令并允许 服务器来回连它的数据端口,而是提交 PASV 命令 。这样做的结果是 服务器会开启一个任意的非特权端口( P > 1024 ),并发送 PORT P 命令给客户端 。然后客户端发起从本地端口 N+1 到 服务器的端口 P 的连接用来传送数据 。

  对于 服务器端的防火墙来说,必须允许下面的通讯才能支持被动方式的 FTP:

  从任何端口到 服务器的 21 端口 (客户端初始化的连接 S<-C )

  服务器的 21 端口到任何大于 1024 的端口 ( 服务器响应到客户端的控制端口的连接 S->C )

  从任何端口到 服务器的大于 1024 端口 (入;客户端初始化数据连接到 服务器指定的任意端口 S<-C )

  服务器的大于 1024 端口到远程的大于 1024 的端口(出; 服务器发送 ACK 响应和数据到客户端的数据端口 S->C )

  画出来的话,被动方式的FTP连接过程大概是下图的样子:



  在第 1 步中,客户端的命令端口与 服务器的命令端口建立连接,并发送命令 “PASV” 。然后在第 2 步中, 服务器返回命令 "PORT 2024" ,告诉客户端( 服务器)用哪个端口侦听数据连接 。在第 3 步中,客户端初始化一个从自己的数据端口到 服务器端指定的数据端口的数据连接 。最后 服务器在第 4 步中给客户端的数据端口返回一个 "ACK" 响应 。

  被动方式的 FTP 解决了客户端的许多问题,但同时给 服务器端带来了更多的问题 。最大的问题是需要允许从任意远程终端到 服务器高位端口的连接 。幸运的是,许多 FTP 守护程序,包括流行的 WU-FTPD 允许管理员指定 FTP 服务器使用的端口范围 。详细内容参看附录 1

  第二个问题是客户端有的支持被动模式,有的不支持被动模式,必须考虑如何能支持这些客户端,以及为他们提供解决办法 。例如, Solaris 提供的 FTP 命令行工具就不支持被动模式,需要第三方的 FTP 客户端,比如 ncftp 。

  随着 WWW 的广泛流行,许多人习惯用 web 浏览器作为 FTP 客户端 。大多数浏览器只在访问 ftp:// 这样的 URL 时才支持被动模式 。这到底是好还是坏取决于 服务器和防火墙的配置 。

  被动 FTP 的例子

  下面是一个被动 FTP 会话的实际例子,只是 服务器名、 IP 地址和用户名都做了改动 。在这个例子中, FTP 会话从 testbox1.slacksite.com (192.168.150.80) ,一个运行标准的 FTP 命令行客户端的 Linux 工作站,发起到 testbox2.slacksite.com (192.168.150.90) ,一个运行 ProFTPd 1.2.2RC2 的 Linux 工作站 。debugging ( -d )选项用来在 FTP 客户端显示连接的详细过程 。红色的文字是 debugging 信息,显示的是发送到 服务器的实际 FTP 命令和所产生的回应信息 。服务器的输出信息用黑色字表示,用户的输入信息用粗体字表示 。

  注意此例中的 PORT 命令与主动 FTP 例子的不同 。这里,我们看到是 服务器(192.168.150.90) 而不是客户端的一个端口被打开了 。可以跟上面的主动 FTP 例子中的 PORT 命令格式对比一下 。

  testbox1: {/home/p-t/slacker/public_html} % ftp -d testbox2

  Connected to testbox2.slacksite.com.

  220 testbox2.slacksite.com FTP server ready.

  Name (testbox2:slacker): slacker

  ---> USER slacker

  331 Password required for slacker.

  Password: TmpPass

  ---> PASS XXXX

  230 User slacker logged in.

  ---> SYST

  215 UNIX Type: L8

  Remote system type is UNIX.

  Using binary mode to transfer files.

  ftp> passive

  Passive mode on.

  ftp> ls

  ftp: setsockopt (ignored): Permission denied

  ---> PASV

  227 Entering Passive Mode (192,168,150,90,195,149).

  ---> LIST

  150 Opening ASCII mode data connection for file list

  drwx------ 3 slacker users 104 Jul 27 01:45 public_html

  226 Transfer complete.

  ftp> quit

  ---> QUIT

  221 Goodbye.

  总结

  下面的图表会帮助管理员们记住每种 FTP 方式是怎样工作的:

  主动 FTP :

  命令连接:客户端 >1024 端口 -> 服务器 21 端口

  数据连接:客户端 >1024 端口 <- 服务器 20 端口

  被动 FTP :

  命令连接:客户端 >1024 端口 -> 服务器 21 端口

  数据连接:客户端 >1024 端口 -> 服务器 >1024 端口

  下面是主动与被动 FTP 优缺点的简要总结:

  主动 FTP 对 FTP 服务器的管理有利,但对客户端的管理不利 。因为 FTP 服务器企图与客户端的高位随机端口建立连接,而这个端口很有可能被客户端的防火墙阻塞掉 。被动 FTP 对 FTP 客户端的管理有利,但对 服务器端的管理不利 。因为客户端要与 服务器端建立两个连接,其中一个连到一个高位随机端口,而这个端口很有可能被 服务器端的防火墙阻塞掉 。

  幸运的是,有折衷的办法 。既然 FTP 服务器的管理员需要他们的 服务器有最多的客户连接,那么必须得支持被动 FTP 。我们可以通过为 FTP 服务器指定一个有限的端口范围来减小 服务器高位端口的暴露 。这样,不在这个范围的任何端口会被 服务器的防火墙阻塞 。虽然这没有消除所有针对服务器的危险,但它大大减少了危险 。详细信息参看附录 1


解决办法,譬如leapftp,按F4,站点管理器,高级选项,“使用PASV模式”打勾去掉。


  参考资料

  O'Reilly 出版的《组建 Internet 防火墙》(第二版, Brent Chapman , Elizabeth Zwicky 著)是一本很不错的参考资料 。里面讲述了各种 Internet 协议如何工作,以及有关防火墙的例子 。

  最权威的 FTP 参考资料是 RFC 959 ,它是 FTP 协议的官方规范 。RFC 的资料可以从许多网站上下载,例如:ftp://nic.merit.edu/documents/rfc/rfc0959.txt 。
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

#
# 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之后,其他写权限才能生效(such as:anon_mkdir_enable)
write_enable=YES
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=NO

#1.chroot_local_user=YES
#2.chroot_list_enable=YES
#3.chroot_list_file
# case A: only 1, 则'本地用户'被'囚禁(chroot)'在自己的$HOME目录
# case B: 1 && 2 && 3, 则只有chroot_list中列出的'本地用户'不被'囚禁'
# case C: 2 && 3, 则只有chroot_list中列出的'本地用户' 被'囚禁'
#chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
#
# 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
#
#port模式/pasv模式默认都是开启的;以下两行可以省略
port_enable=YES
pasv_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=/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
#
# 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_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 whith two configuration files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YES
#pam_service_name此处必须填vsftpd.否则只能匿名用户登录
pam_service_name=vsftpd
#
#1.userlist_enable=YES && userlist_deny=YES(default):则禁止user_list中的用户登录
# 当使用user_list文件中的用户登录时,在提问密码之前,该用户就被拒绝登录。
#2.userlist_enable=YES && userlist_deny=NO:只允许user_list中的用户登录
userlist_enable=YES
#userlist_deny=NO
tcp_wrappers=YES



posted on 2012-03-20 22:19  笑剑钝  阅读(354)  评论(0编辑  收藏  举报

导航