个人工具

“Vsftpd”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
文件结构
(已恢复1.202.40.80对话)的编辑至131.202.160.92的最后一个修订版本)
 
(未显示12个用户的48个中间版本)
第1行: 第1行:
 
vsftpd,ftp服务端
 
vsftpd,ftp服务端
 +
==stand alone和super daemon==
 +
stand alone指的是一直运行vsftpd,占用资源,提供ftp服务。super daemon指的是有需要时由xinetd启动vsftpd服务。如果服务器不是那种长期开ftp,提供大量的上传下载服务的话,会选择后者。
 +
 
==安装==
 
==安装==
 
安装
 
安装
<pre>sudo apt-get install vsftpd</pre>
+
<pre>$ sudo apt-get install vsftpd</pre>
 
查看是否打开21端口
 
查看是否打开21端口
<pre>qii@ubuntu:~$ sudo netstat -tulnp| grep 21
+
<pre>$ sudo netstat -npltu | grep 21
 
tcp        0      0 0.0.0.0:21              0.0.0.0:*              LISTEN      15601/vsftpd     
 
tcp        0      0 0.0.0.0:21              0.0.0.0:*              LISTEN      15601/vsftpd     
 
</pre>
 
</pre>
第30行: 第33行:
 
需要修改vsftpd.conf
 
需要修改vsftpd.conf
 
<pre>listen=NO</pre>
 
<pre>listen=NO</pre>
 +
这里若不改成NO,会出现下列错误
 +
<pre>500 OOPS: could not bind listening IPv4 socket</pre>
 +
安装xinetd
 +
<pre>sudo apt-get install xinetd
 +
</pre>
 +
<pre>sudo vi /etc/xinetd.conf</pre>
 +
<pre>service ftp
 +
{
 +
        socket_type            = stream
 +
        wait                    = no
 +
        user                    = root
 +
        server                  = /usr/sbin/vsftpd
 +
        log_on_success          += DURATION USERID
 +
        log_on_failure          += USERID
 +
        nice                    = 10
 +
        disable                = no
 +
}</pre>
 +
停止vsftpd,启动xinetd
 +
<pre>sudo service vsftpd stop</pre>
 +
<pre>sudo service xinetd start</pre>
 +
查看端口
 +
<pre>$ sudo netstat -npltu | grep 21
 +
tcp        0      0 0.0.0.0:21              0.0.0.0:*              LISTEN      16787/xinetd  </pre>
  
 
==/etc/vsftpd.conf==
 
==/etc/vsftpd.conf==
<pre># Example config file /etc/vsftpd.conf
+
<pre>listen=<YES/NO> :设置为YES时vsftpd以独立运行方式启动,设置为NO时以xinetd方式启动(xinetd是管理守护进程的,将服务集中管理,可以减少大量服务的资源消耗)
 
+
listen_port=<port> :设置控制连接的监听端口号,默认为21
# Run standalone?  vsftpd can run either from an inetd or as a standalone
+
listen_address=<ip address> :将在绑定到指定IP地址运行,适合多网卡
# daemon started from an initscript.
+
connect_from_port_20=<YES/NO> :若为YES,则强迫FTP-DATA的数据传送使用port 20,默认YES
listen=YES
+
pasv_enable=<YES/NO> :是否使用被动模式的数据连接,如果客户机在防火墙后,请开启为YES
#
+
pasv_min_port=<n>
# Run standalone with IPv6?
+
pasv_max_port=<m> :设置被动模式后的数据连接端口范围在n和m之间,建议为50000-60000端口
# Like the listen parameter, except vsftpd will listen on an IPv6 socket
+
message_file=<filename> :设置使用者进入某个目录时显示的文件内容,默认为 .message
# instead of an IPv4 one. This parameter and the listen parameter are mutually
+
dirmessage_enable=<YES/NO> :设置使用者进入某个目录时是否显示由message_file指定的文件内容
# exclusive.
+
ftpd_banner=<message> :设置用户连接服务器后的显示信息,就是欢迎信息
#listen_ipv6=YES
+
banner_file=<filename> :设置用户连接服务器后的显示信息存放在指定的filename文件中
#
+
connect_timeout=<n> :如果客户机连接服务器超过N秒,则强制断线,默认60
# Allow anonymous FTP? (Disabled by default)
+
accept_timeout=<n> :当使用者以被动模式进行数据传输时,服务器发出passive port指令等待客户机超过N秒,则强制断线,默认60
anonymous_enable=NO
+
accept_connection_timeout=<n> :设置空闲的数据连接在N秒后中断,默认120
#
+
data_connection_timeout=<n> : 设置空闲的用户会话在N秒后中断,默认300
# Uncomment this to allow local users to log in.
+
max_clients=<n> : 在独立启动时限制服务器的连接数,0表示无限制
local_enable=NO
+
max_per_ip=<n> :在独立启动时限制客户机每IP的连接数,0表示无限制(不知道是否跟多线程下载有没干系)
#
+
local_enable=<YES/NO> :设置是否支持本地用户帐号访问
# Uncomment this to enable any form of FTP write command.
+
guest_enable=<YES/NO> :设置是否支持虚拟用户帐号访问
#write_enable=YES
+
write_enable=<YES/NO> :是否开放本地用户的写权限
#
+
local_umask=<nnn> :设置本地用户上传的文件的生成掩码,默认为077
# Default umask for local users is 077. You may wish to change this to 022,
+
local_max_rate<n> :设置本地用户最大的传输速率,单位为bytes/sec,值为0表示不限制
# if your users expect that (022 is used by most other ftpd's)
+
local_root=<file> :设置本地用户登陆后的目录,默认为本地用户的主目录
#local_umask=022
+
chroot_local_user=<YES/NO> :当为YES时,所有本地用户可以执行chroot
#
+
chroot_list_enable=<YES/NO>
# Uncomment this to allow the anonymous FTP user to upload files. This only
+
chroot_list_file=<filename> :当chroot_local_user=NO 且 chroot_list_enable=YES时,只有filename文件指定的用户可以执行chroot
# has an effect if the above global write enable is activated. Also, you will
+
anonymous_enable=<YES/NO> :设置是否支持匿名用户访问
# obviously need to create a directory writable by the FTP user.
+
anon_max_rate=<n> :设置匿名用户的最大传输速率,单位为B/s,值为0表示不限制
#anon_upload_enable=YES
+
anon_world_readable_only=<YES/NO> 是否开放匿名用户的浏览权限
#
+
anon_upload_enable=<YES/NO> 设置是否允许匿名用户上传
# Uncomment this if you want the anonymous FTP user to be able to create
+
anon_mkdir_write_enable=<YES/NO> :设置是否允许匿名用户创建目录
# new directories.
+
anon_other_write_enable=<YES/NO> :设置是否允许匿名用户其他的写权限(注意,这个在安全上比较重要,一般不建议开,不过关闭会不支持续传)
#anon_mkdir_write_enable=YES
+
anon_umask=<nnn> :设置匿名用户上传的文件的生成掩码,默认为077
#
+
# Activate directory messages - messages given to remote users when they
+
# go into a certain directory.
+
dirmessage_enable=YES
+
#
+
# If enabled, vsftpd will display directory listings with the time
+
# in  your  local  time  zone.  The default is to display GMT. The
+
# times returned by the MDTM FTP command are also affected by this
+
# option.
+
use_localtime=YES
+
#
+
# Activate logging of uploads/downloads.
+
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
+
#
+
# You may override where the log file goes if you like. The default is shown
+
# below.
+
#xferlog_file=/var/log/vsftpd.log
+
#
+
# If you want, you can have your log file in standard ftpd xferlog format.
+
# Note that the default log file location is /var/log/xferlog in this case.
+
#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 restrict local users to their home directories.  See the FAQ for
+
# the possible risks in this before using chroot_local_user or
+
# chroot_list_enable below.
+
#chroot_local_user=YES
+
#
+
# 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
+
#
+
# Debian customization
+
#
+
# Some of vsftpd's settings don't fit the Debian filesystem layout by
+
# default.  These settings are more Debian-friendly.
+
#
+
# This option should be the name of a directory which is empty.  Also, the
+
# directory should not be writable by the ftp user. This directory is used
+
# as a secure chroot() jail at times vsftpd does not require filesystem
+
# access.
+
secure_chroot_dir=/var/run/vsftpd/empty
+
#
+
# This string is the name of the PAM service vsftpd will use.
+
pam_service_name=vsftpd
+
#
+
# This option specifies the location of the RSA certificate to use for SSL
+
# encrypted connections.
+
rsa_cert_file=/etc/ssl/private/vsftpd.pem</pre>
+
  
 
==匿名用户==
 
==匿名用户==
第176行: 第100行:
 
<pre># Allow anonymous FTP? (Disabled by default)
 
<pre># Allow anonymous FTP? (Disabled by default)
 
anonymous_enable=YES</pre>
 
anonymous_enable=YES</pre>
 +
===上传文件===
 
允许匿名上传
 
允许匿名上传
<pre>anon_upload_enable=YES</pre>
+
<pre>write_enable=YES
 +
anon_mkdir_write_enable=YES
 +
anon_upload_enable=YES</pre>
 +
注意2点:
 +
 
 +
1.匿名用户就是'''ftp''',想要匿名用户写入,必须文件夹的权限为ftp可写。
 +
 
 +
2.匿名用户的根目录不允许'''写''',所以根目录的权限绝对不能是ftp可写和'''其他用户'''可写,如果根目录所有者为ftp的话,所有者的权限也不能写。
 +
 
 +
所以解决方法是建个单独的public文件夹用于上传文件,设置其为ftp可写或”其他用户可写“
 +
 
 +
还可建个download文件夹只用于下载,设置其他用户没有写权限便可。
 +
 
 +
===重命名、删除文件===
 +
开放重命名,删除文件等权限,不开的话没法续传。
 +
<pre>anon_other_write_enable=YES</pre>
 +
 
 +
==仅能上传,无法下载==
 +
<pre>write_enable=YES
 +
anon_mkdir_write_enable=YES
 +
anon_upload_enable=YES
 +
chown_uploads=YES
 +
chown_username=root</pre>
 +
上传的文件所有者被改为root,匿名用户的ftp用户就无法读取,下载了。
 +
 
 
==认证FTP配置==
 
==认证FTP配置==
 
设定vsftp认证系统用户,并允许他们上传文件,编辑 /etc/vsftpd.conf:
 
设定vsftp认证系统用户,并允许他们上传文件,编辑 /etc/vsftpd.conf:
第187行: 第136行:
  
 
==Chroot==
 
==Chroot==
限制登录用户访问其他目录,改之前登录显示的路径比如是 /home/qii ,改之后则是 /
+
===限制所有===
 +
限制登录用户访问其他目录,改之前登录显示的路径比如是 ~ ,改之后则是 /
 +
<pre>效果是像这样的。
 +
注:我的本地用户(local user为yeshuai,home directory为/home/yeshuai)
 +
 
 +
 
 +
root@ubuntu:~# ftp localhost
 +
Connected to localhost.
 +
220 (vsFTPd 2.3.2)
 +
Name (localhost:yeshuai): yeshuai
 +
 
 +
331 Please specify the password.
 +
Password:
 +
230 Login successful.
 +
Remote system type is UNIX.
 +
Using binary mode to transfer files.
 +
ftp>
 +
ftp> pwd
 +
'''257 "/"'''
 +
 
 +
看上边,正常情况下,输入pwd时,应该是显示/home/yeshuai.
 +
由于我做了chroot.所以,/home/yeshuai变成 /
 +
 
 +
</pre>
 +
 
 
<pre>chroot_local_user=YES</pre>
 
<pre>chroot_local_user=YES</pre>
 +
 +
===开放所有,限制特定===
 
可指定一组用户限制
 
可指定一组用户限制
<pre>chroot_list_enable=YES
+
<pre>chroot_local_user=NO
 +
chroot_list_enable=YES
 
chroot_list_file=/etc/vsftpd.chroot_list</pre>
 
chroot_list_file=/etc/vsftpd.chroot_list</pre>
 
随后创建列表
 
随后创建列表
第197行: 第173行:
 
重启vsftp
 
重启vsftp
 
<pre>sudo service vsftpd restart</pre>
 
<pre>sudo service vsftpd restart</pre>
==/etc/ftpusers文件==
+
===限制所有,开放特定===
 +
上面的规则是限制  /etc/vsftpd.chroot_list 中的用户,反过来限制一切,只解禁  /etc/vsftpd.chroot_list 的用户。这样:
 +
<pre>chroot_local_user=YES
 +
chroot_list_enable=YES
 +
chroot_list_file=/etc/vsftpd.chroot_list</pre>
 +
 
 +
==账号登录==
 +
===/etc/ftpusers文件===
 
该文件内的用户一律禁止ftp连接,默认列表包括了root, daemon, nobody等。需要禁止某个用户,添加进来便是。
 
该文件内的用户一律禁止ftp连接,默认列表包括了root, daemon, nobody等。需要禁止某个用户,添加进来便是。
 +
 +
这个文件是由PAM模块的 /etc/pam.d/vsftpd 指定的
 +
<pre>qii@ubuntu:~$ sudo cat /etc/pam.d/vsftpd
 +
# Standard behaviour for ftpd(8).
 +
auth    required        pam_listfile.so item=user sense=deny file=/etc/ftpusers onerr=succeed
 +
 +
# Note: vsftpd handles anonymous logins on its own. Do not enable pam_ftp.so.
 +
 +
# Standard pam includes
 +
@include common-account
 +
@include common-session
 +
@include common-auth
 +
auth    required        pam_shells.so
 +
</pre>
 +
===userlist_file 文件===
 +
vsftpd自订的列表,跟/etc/ftpusers类似,具体文件名和路径是由用户自己指定的。这边设成 /etc/vsftpd.user_list
 +
 +
添加配置文件字段:
 +
<pre>userlist_enable=YES
 +
userlist_deny=YES
 +
userlist_file=/etc/vsftpd.user_list</pre>
 +
===限制一切,开放特定===
 +
上述2个方法都是限制列表文件中的用户,如果要反过来,限制一切用户登录,只允许列表文件中的用户,用 userlist_file,这样:
 +
<pre>userlist_enable=YES
 +
userlist_deny=NO</pre>
 +
 +
===root登录===
 +
之所以限制root这类账号登录就是托上面几个文件,要是你非要用系统账号登录,如上所述,将列表文件中的特定用户名删除便是。
 +
==主机访问==
 +
===tcp_wrappers配置===
 +
stand alone模式下提供基于主机的访问控制的配置
 +
 +
tcp_wrappers使用/etc/hosts.allow和/etc/hosts.deny两个配置文件实现访问控制,hosts.allow是一个许可表,hosts.deny是一个拒绝表
 +
在hosts.allow中也可以使用DENY,所以通常可以只使用hosts.allow来实现访问控制。
 +
对vsftpd而言,书写hosts.allow有三种语法形式
 +
<pre>
 +
1: vsftpd:主机表 (设置允许访问的主机表)
 +
2: vsftpd:主机表:DENY (设置拒绝访问的主机表)
 +
3: vsftpd:主机表:setenv VSFTPD_LOAD_CONF 配置文件名 (对指定的主机使用另外的配置)
 +
</pre>
 +
setenv VSFTPD_LOAD_CONF的值为指定的配置文件名,意图是让vsftpd守护进程读取新的配置项来覆盖主配置文件中的项,实现特定待遇
 +
这有一个小例子来说明tcp_wrappers
 +
 +
<pre>功能:
 +
》1.拒绝192.168.2.0/24访问
 +
》2.对192.168.1.0/24内的所有主机不作连接限制和最大传输速率限制
 +
》3.对其他主机的访问限制为:每ip连接数为1,最大传输速率为10kb/s (在主配置文件中设置就好了)
 +
</pre>
 +
首先保证设定
 +
<pre>tcp_wrappers=YES</pre>
 +
然后编辑
 +
<pre>sudo vi /etc/hosts.allow </pre>
 +
<pre>
 +
vsftpd:192.168.2.0/24: DENY (阻止192.168.2.0子网的访问)
 +
vsftpd:192.168.1.0/24 (允许192.168.1.0子网的访问)
 +
vsftpd:192.168.1.0/24: setenv VSFTPD_LOAD_CONF /etc/xxx.conf (对192.168.1.0/24指定专有配置文件xxx.conf,xxx可以自己指定文件名,需要建立)
 +
</pre>
 +
然后建立xxx.conf文件,并编辑 (建立文件可以用sudo touch /etc/xxx.conf建立)
 +
 +
===super daemon限制主机访问===
 +
====只允许指定主机访问====
 +
在配置文件/etc/xinetd.d/vsftpd的{}中添加如下的配置语句:
 +
<pre>only_from <主机表> </pre>
 +
如 only_from 192.168.1.0  表示只允许192.168.1.0网段内的主机访问。
 +
====指定不能访问的主机====
 +
<pre>no_access <主机表> </pre>
 +
如:no_access 192.168.1.0  表示只有192.168.1.0网段内的主机不能访问。
 +
====主机表====
 +
关于主机表的书写形式,见下表:
 +
访问控制表时主机表的书写语法 <pre>
 +
选项值 含义
 +
Hostname 可解析的主机名
 +
IP Address 十进制表示的IP地址
 +
Net_name 在/etc/networks中定义的网络名
 +
x.x.x.0 x.x.0.0 x.0.0.0 0.0.0.0 0作为通配符看待。如:191.72.61.0匹配从191.72.61.0到191.72.61.255的所有IP地址。0.0.0.0表示匹配所有的IP地址
 +
x.x.x.{a,b,.} x.x.{a,b,.} x.{a,b,.} 指定主机表。如:191.72.61.{1,3,123}表示包含地址191.72.61.1、191.72.61.2和191.72.61.123
 +
IPAddress/netmask 定义要匹配的网络或子网。如:172.19.16/20匹配从172.19.16.0到172.19.31.255 </pre>
 +
 
==限制==
 
==限制==
 
===限制连接数===
 
===限制连接数===
第204行: 第265行:
 
<pre>max_clients=数字
 
<pre>max_clients=数字
 
max_per_ip=数字</pre>
 
max_per_ip=数字</pre>
 +
错误提示分别是
 +
<pre>qii@ubuntu:~$ ftp localhost
 +
Connected to localhost.
 +
421 There are too many connected users, please try later.
 +
</pre>
 +
<pre>qii@ubuntu:~$ ftp localhost
 +
Connected to localhost.
 +
421 There are too many connections from your internet address.
 +
</pre>
 +
 
===限制下载速度===
 
===限制下载速度===
 
单位是字节,所以需要换算。比如我想让匿名用户和vsFTP上的用户都以80KB下载,所以这个数字应该是1024x80=81920  
 
单位是字节,所以需要换算。比如我想让匿名用户和vsFTP上的用户都以80KB下载,所以这个数字应该是1024x80=81920  
 
<pre>anon_max_rate=数字 #匿名用户下载速度
 
<pre>anon_max_rate=数字 #匿名用户下载速度
 
local_max_rate=数字 #普通用户下载速度</pre>
 
local_max_rate=数字 #普通用户下载速度</pre>
 +
===super daemon限制连接数===
 +
instances 是服务器最多的连接数,per_source 是单个IP地址最多的连接数。
 +
<pre>service ftp
 +
{
 +
        socket_type            = stream
 +
        wait                    = no
 +
        user                    = root
 +
        server                  = /usr/sbin/vsftpd
 +
        server_args            = /etc/vsftpd.conf
 +
        log_on_success          += DURATION USERID
 +
        log_on_failure          += USERID
 +
        nice                    = 10
 +
        disable                = no
 +
        per_source              = 10 
 +
        instances              = 10 
 +
}</pre>
 +
错误提示都是
 +
<pre>qii@ubuntu:~$ ftp localhost
 +
Connected to localhost.
 +
421 Service not available, remote server has closed connection
 +
</pre>
 +
定制错误提示,添加
 +
<pre>banner_fail      = /etc/vsftpd.busy_banner</pre>
 +
编辑提示内容
 +
<pre>sudo vi /etc/vsftpd.busy_banner</pre>
 +
比如
 +
<pre>qii@ubuntu:~$ ftp localhost
 +
Connected to localhost.
 +
服务器大姨妈中
 +
</pre>
 +
 
==欢迎信息==
 
==欢迎信息==
 
<pre>dirmessage_enable=YES</pre>
 
<pre>dirmessage_enable=YES</pre>
第229行: 第331行:
 
若是匿名用户,就放到
 
若是匿名用户,就放到
 
<pre>/var/ftp</pre>
 
<pre>/var/ftp</pre>
 +
 
==虚拟路径==
 
==虚拟路径==
 
比如我的ftp的默认目录是/srv/ftp,我想把/mnt/LinG/WinSoft文件夹,映射到/srv/ftp目录中,我就如下操作
 
比如我的ftp的默认目录是/srv/ftp,我想把/mnt/LinG/WinSoft文件夹,映射到/srv/ftp目录中,我就如下操作
第237行: 第340行:
 
执行mount命令
 
执行mount命令
 
<pre>sudo mount --bind /mnt/LinG/WinSoft /srv/ftp/WinSoft</pre>
 
<pre>sudo mount --bind /mnt/LinG/WinSoft /srv/ftp/WinSoft</pre>
 +
==虚拟用户==
 +
*[[Vsftpd虚拟用户设置]]
 +
*[[Vsftpd和mysql配置]]
 +
 +
==实例==
 +
*[[Vsftpd实例]]
 +
*[http://forum.ubuntu.org.cn/viewtopic.php?f=24&t=23873 Vsftpd实例2(oneleaf的vsftpd配置)]
 +
 +
==缺陷==
 +
Vsftpd的'''虚拟用户'''无法实现磁盘限额,系统用户倒是可以用quota工具实现磁盘限额。
 +
==参考==
 +
[http://forum.ubuntu.org.cn/viewtopic.php?f=54&t=117505 vsftpd参考小手册]
 +
 
[[Category:服务器]]
 
[[Category:服务器]]

2013年3月23日 (六) 16:41的最新版本

vsftpd,ftp服务端

stand alone和super daemon

stand alone指的是一直运行vsftpd,占用资源,提供ftp服务。super daemon指的是有需要时由xinetd启动vsftpd服务。如果服务器不是那种长期开ftp,提供大量的上传下载服务的话,会选择后者。

安装

安装

$ sudo apt-get install vsftpd

查看是否打开21端口

$ sudo netstat -npltu | grep 21
tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      15601/vsftpd    

登录

ftp localhost

输入Ubuntu的用户名、密码登录

ls

会显示home目录的文件

文件结构

匿名用户根路径

/srv/ftp

配置文件

/etc/vsftpd.conf

查阅配置文件详细信息

man 5 vsftpd.conf

设定log保存位置,默认如下

xferlog_file=/var/log/vsftpd.log

运行

standalone

最普遍的方式

sudo service vsftpd start

super daemon

需要修改vsftpd.conf

listen=NO

这里若不改成NO,会出现下列错误

500 OOPS: could not bind listening IPv4 socket

安装xinetd

sudo apt-get install xinetd
sudo vi /etc/xinetd.conf
service ftp
{
        socket_type             = stream
        wait                    = no
        user                    = root
        server                  = /usr/sbin/vsftpd
        log_on_success          += DURATION USERID
        log_on_failure          += USERID
        nice                    = 10
        disable                 = no
}

停止vsftpd,启动xinetd

sudo service vsftpd stop
sudo service xinetd start

查看端口

$ sudo netstat -npltu | grep 21
tcp        0      0 0.0.0.0:21              0.0.0.0:*               LISTEN      16787/xinetd  

/etc/vsftpd.conf

listen=<YES/NO> :设置为YES时vsftpd以独立运行方式启动,设置为NO时以xinetd方式启动(xinetd是管理守护进程的,将服务集中管理,可以减少大量服务的资源消耗)
listen_port=<port> :设置控制连接的监听端口号,默认为21
listen_address=<ip address> :将在绑定到指定IP地址运行,适合多网卡
connect_from_port_20=<YES/NO> :若为YES,则强迫FTP-DATA的数据传送使用port 20,默认YES
pasv_enable=<YES/NO> :是否使用被动模式的数据连接,如果客户机在防火墙后,请开启为YES
pasv_min_port=<n>
pasv_max_port=<m> :设置被动模式后的数据连接端口范围在n和m之间,建议为50000-60000端口
message_file=<filename> :设置使用者进入某个目录时显示的文件内容,默认为 .message
dirmessage_enable=<YES/NO> :设置使用者进入某个目录时是否显示由message_file指定的文件内容
ftpd_banner=<message> :设置用户连接服务器后的显示信息,就是欢迎信息
banner_file=<filename> :设置用户连接服务器后的显示信息存放在指定的filename文件中
connect_timeout=<n> :如果客户机连接服务器超过N秒,则强制断线,默认60
accept_timeout=<n> :当使用者以被动模式进行数据传输时,服务器发出passive port指令等待客户机超过N秒,则强制断线,默认60
accept_connection_timeout=<n> :设置空闲的数据连接在N秒后中断,默认120
data_connection_timeout=<n> : 设置空闲的用户会话在N秒后中断,默认300
max_clients=<n> : 在独立启动时限制服务器的连接数,0表示无限制
max_per_ip=<n> :在独立启动时限制客户机每IP的连接数,0表示无限制(不知道是否跟多线程下载有没干系)
local_enable=<YES/NO> :设置是否支持本地用户帐号访问
guest_enable=<YES/NO> :设置是否支持虚拟用户帐号访问
write_enable=<YES/NO> :是否开放本地用户的写权限
local_umask=<nnn> :设置本地用户上传的文件的生成掩码,默认为077
local_max_rate<n> :设置本地用户最大的传输速率,单位为bytes/sec,值为0表示不限制
local_root=<file> :设置本地用户登陆后的目录,默认为本地用户的主目录
chroot_local_user=<YES/NO> :当为YES时,所有本地用户可以执行chroot
chroot_list_enable=<YES/NO> 
chroot_list_file=<filename> :当chroot_local_user=NO 且 chroot_list_enable=YES时,只有filename文件指定的用户可以执行chroot
anonymous_enable=<YES/NO> :设置是否支持匿名用户访问
anon_max_rate=<n> :设置匿名用户的最大传输速率,单位为B/s,值为0表示不限制
anon_world_readable_only=<YES/NO> 是否开放匿名用户的浏览权限
anon_upload_enable=<YES/NO> 设置是否允许匿名用户上传
anon_mkdir_write_enable=<YES/NO> :设置是否允许匿名用户创建目录
anon_other_write_enable=<YES/NO> :设置是否允许匿名用户其他的写权限(注意,这个在安全上比较重要,一般不建议开,不过关闭会不支持续传)
anon_umask=<nnn> :设置匿名用户上传的文件的生成掩码,默认为077

==匿名用户==
先备份
<pre>sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.old

打开

sudo vi /etc/vsftpd.conf

允许匿名访问

# Allow anonymous FTP? (Disabled by default)
anonymous_enable=YES

上传文件

允许匿名上传

write_enable=YES
anon_mkdir_write_enable=YES
anon_upload_enable=YES

注意2点:

1.匿名用户就是ftp,想要匿名用户写入,必须文件夹的权限为ftp可写。

2.匿名用户的根目录不允许,所以根目录的权限绝对不能是ftp可写和其他用户可写,如果根目录所有者为ftp的话,所有者的权限也不能写。

所以解决方法是建个单独的public文件夹用于上传文件,设置其为ftp可写或”其他用户可写“

还可建个download文件夹只用于下载,设置其他用户没有写权限便可。

重命名、删除文件

开放重命名,删除文件等权限,不开的话没法续传。

anon_other_write_enable=YES

仅能上传,无法下载

write_enable=YES
anon_mkdir_write_enable=YES
anon_upload_enable=YES
chown_uploads=YES
chown_username=root

上传的文件所有者被改为root,匿名用户的ftp用户就无法读取,下载了。

认证FTP配置

设定vsftp认证系统用户,并允许他们上传文件,编辑 /etc/vsftpd.conf:

local_enable=YES
write_enable=YES

重启vsftp

sudo service vsftpd restart

系统用户登录ftp便进入他们的home目录

Chroot

限制所有

限制登录用户访问其他目录,改之前登录显示的路径比如是 ~ ,改之后则是 /。

效果是像这样的。
注:我的本地用户(local user为yeshuai,home directory为/home/yeshuai)


root@ubuntu:~# ftp localhost
Connected to localhost.
220 (vsFTPd 2.3.2)
Name (localhost:yeshuai): yeshuai

331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> 
ftp> pwd
'''257 "/"'''

看上边,正常情况下,输入pwd时,应该是显示/home/yeshuai.
由于我做了chroot.所以,/home/yeshuai变成 /

chroot_local_user=YES

开放所有,限制特定

可指定一组用户限制

chroot_local_user=NO
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list

随后创建列表

sudo vi  /etc/vsftpd.chroot_list

一行一个用户名 重启vsftp

sudo service vsftpd restart

限制所有,开放特定

上面的规则是限制 /etc/vsftpd.chroot_list 中的用户,反过来限制一切,只解禁 /etc/vsftpd.chroot_list 的用户。这样:

chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list

账号登录

/etc/ftpusers文件

该文件内的用户一律禁止ftp连接,默认列表包括了root, daemon, nobody等。需要禁止某个用户,添加进来便是。

这个文件是由PAM模块的 /etc/pam.d/vsftpd 指定的

qii@ubuntu:~$ sudo cat /etc/pam.d/vsftpd
# Standard behaviour for ftpd(8).
auth    required        pam_listfile.so item=user sense=deny file=/etc/ftpusers onerr=succeed

# Note: vsftpd handles anonymous logins on its own. Do not enable pam_ftp.so.

# Standard pam includes
@include common-account
@include common-session
@include common-auth
auth    required        pam_shells.so

userlist_file 文件

vsftpd自订的列表,跟/etc/ftpusers类似,具体文件名和路径是由用户自己指定的。这边设成 /etc/vsftpd.user_list

添加配置文件字段:

userlist_enable=YES
userlist_deny=YES
userlist_file=/etc/vsftpd.user_list

限制一切,开放特定

上述2个方法都是限制列表文件中的用户,如果要反过来,限制一切用户登录,只允许列表文件中的用户,用 userlist_file,这样:

userlist_enable=YES
userlist_deny=NO

root登录

之所以限制root这类账号登录就是托上面几个文件,要是你非要用系统账号登录,如上所述,将列表文件中的特定用户名删除便是。

主机访问

tcp_wrappers配置

stand alone模式下提供基于主机的访问控制的配置

tcp_wrappers使用/etc/hosts.allow和/etc/hosts.deny两个配置文件实现访问控制,hosts.allow是一个许可表,hosts.deny是一个拒绝表 在hosts.allow中也可以使用DENY,所以通常可以只使用hosts.allow来实现访问控制。 对vsftpd而言,书写hosts.allow有三种语法形式

1: vsftpd:主机表 (设置允许访问的主机表)
2: vsftpd:主机表:DENY (设置拒绝访问的主机表)
3: vsftpd:主机表:setenv VSFTPD_LOAD_CONF 配置文件名 (对指定的主机使用另外的配置)

setenv VSFTPD_LOAD_CONF的值为指定的配置文件名,意图是让vsftpd守护进程读取新的配置项来覆盖主配置文件中的项,实现特定待遇 这有一个小例子来说明tcp_wrappers

功能:
》1.拒绝192.168.2.0/24访问
》2.对192.168.1.0/24内的所有主机不作连接限制和最大传输速率限制
》3.对其他主机的访问限制为:每ip连接数为1,最大传输速率为10kb/s (在主配置文件中设置就好了)

首先保证设定

tcp_wrappers=YES

然后编辑

sudo vi /etc/hosts.allow 
vsftpd:192.168.2.0/24: DENY (阻止192.168.2.0子网的访问)
vsftpd:192.168.1.0/24 (允许192.168.1.0子网的访问)
vsftpd:192.168.1.0/24: setenv VSFTPD_LOAD_CONF /etc/xxx.conf (对192.168.1.0/24指定专有配置文件xxx.conf,xxx可以自己指定文件名,需要建立)

然后建立xxx.conf文件,并编辑 (建立文件可以用sudo touch /etc/xxx.conf建立)

super daemon限制主机访问

只允许指定主机访问

在配置文件/etc/xinetd.d/vsftpd的{}中添加如下的配置语句:

only_from <主机表> 

如 only_from 192.168.1.0 表示只允许192.168.1.0网段内的主机访问。

指定不能访问的主机

no_access <主机表> 

如:no_access 192.168.1.0 表示只有192.168.1.0网段内的主机不能访问。

主机表

关于主机表的书写形式,见下表:

访问控制表时主机表的书写语法
选项值 含义 
Hostname 可解析的主机名 
IP Address 十进制表示的IP地址 
Net_name 在/etc/networks中定义的网络名 
x.x.x.0 x.x.0.0 x.0.0.0 0.0.0.0 0作为通配符看待。如:191.72.61.0匹配从191.72.61.0到191.72.61.255的所有IP地址。0.0.0.0表示匹配所有的IP地址 
x.x.x.{a,b,.} x.x.{a,b,.} x.{a,b,.} 指定主机表。如:191.72.61.{1,3,123}表示包含地址191.72.61.1、191.72.61.2和191.72.61.123 
IPAddress/netmask 定义要匹配的网络或子网。如:172.19.16/20匹配从172.19.16.0到172.19.31.255 

限制

限制连接数

前者为服务器最大支持连接数,后者为每个ip允许最多连接数。

max_clients=数字
max_per_ip=数字

错误提示分别是

qii@ubuntu:~$ ftp localhost 
Connected to localhost.
421 There are too many connected users, please try later.
qii@ubuntu:~$ ftp localhost 
Connected to localhost.
421 There are too many connections from your internet address.

限制下载速度

单位是字节,所以需要换算。比如我想让匿名用户和vsFTP上的用户都以80KB下载,所以这个数字应该是1024x80=81920

anon_max_rate=数字 #匿名用户下载速度
local_max_rate=数字 #普通用户下载速度

super daemon限制连接数

instances 是服务器最多的连接数,per_source 是单个IP地址最多的连接数。

service ftp
{
        socket_type             = stream
        wait                    = no
        user                    = root
        server                  = /usr/sbin/vsftpd
        server_args             = /etc/vsftpd.conf 
        log_on_success          += DURATION USERID
        log_on_failure          += USERID
        nice                    = 10
        disable                 = no
        per_source              = 10   
        instances               = 10  
}

错误提示都是

qii@ubuntu:~$ ftp localhost 
Connected to localhost.
421 Service not available, remote server has closed connection

定制错误提示,添加

banner_fail       = /etc/vsftpd.busy_banner

编辑提示内容

sudo vi /etc/vsftpd.busy_banner

比如

qii@ubuntu:~$ ftp localhost 
Connected to localhost.
服务器大姨妈中

欢迎信息

dirmessage_enable=YES

然后编辑各用户home目录下的.message

vi .message
qii@ubuntu:~$ cat .message 
欢迎来到vsftpd
qii@ubuntu:~$ ftp localhost 
Connected to localhost.
220 (vsFTPd 2.2.2)
Name (localhost:qii): qii
331 Please specify the password.
Password:
230-欢迎来到vsftpd
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> 

若是匿名用户,就放到

/var/ftp

虚拟路径

比如我的ftp的默认目录是/srv/ftp,我想把/mnt/LinG/WinSoft文件夹,映射到/srv/ftp目录中,我就如下操作 命令:

#mount --bind [原有的目录] [新目录]

先创建文件夹

sudo mkdir /srv/ftp/WinSoft

执行mount命令

sudo mount --bind /mnt/LinG/WinSoft /srv/ftp/WinSoft

虚拟用户

实例

缺陷

Vsftpd的虚拟用户无法实现磁盘限额,系统用户倒是可以用quota工具实现磁盘限额。

参考

vsftpd参考小手册