Linux_Service—vsftp_Service

来源:互联网 发布:mac numbers 自动填充 编辑:程序博客网 时间:2024/05/24 03:21

vsftp服务

VSFTP是一个基于类Unix系统上使用的FTP服务器软件,全称是“Very Secure FTP”。

OS:CentOS 6.5kernel:2.6.32-431.el6.x86_64vsftpd_version=vsftpd-2.2.2-11.el6_4.1.x86_64

FTP简介与原理

FTP简介

FTP 是File Transfer Protocol(文件传输协议)的英文简称,用于Internet上的控制文件的双向传输。

"下载"文件就是从远程主机拷贝文件至自己的计算机上;

"上传"文件就是将文件从自己的计算机中拷贝至远程主机上;

FTP工作原理

FTP支持两种模式:Standard (PORT方式,主动方式)Passive (PASV,被动方式)

FTP连接端口:

控制连接:TCP 21,用于发送FTP命令信息

数据连接:TCP20,用于上传、下载数据






主动模式:

FTP 客户端首先和服务器的TCP 21端口建立连接,用来发送命令,客户端需要接收数据的时候在这个通道上发送PORT命令。PORT命令包含了客户端用什么端口接收数据。在传送数据的时候,服务器端通过自己的TCP 20端口连接至客户端的指定端口发送数据。FTP server必须和客户端建立一个新的连接用来传送数据。


被动模式:
FTP 客户端首先和服务器的TCP 21端口建立连接,建立连接后发送Pasv命令。服务器收到Pasv命令后,打开一个临时端口(端口号大于1023小于65535)并且通知客户端在这个端口上传送数据的请求,客户端连接FTP服务器此端口,然后FTP服务器将通过这个端口传送数据。
很多防火墙在设置的时候都是不允许接受外部发起的连接的,所以许多位于防火墙后或内网的FTP服务器不支持PASV模式,因为客户端无法穿过防火墙打开FTP服务器的高端端口;而许多内网的客户端不能用PORT模式登陆FTP服务器,因为从服务器的TCP 20无法和内部网络的客户端建立一个新的连接,造成无法工作。

FTP相关文件

安装vsftpd:

[root@TestServer Packages]# rpm -ivh vsftpd-2.2.2-11.el6_4.1.x86_64.rpmPreparing...                ########################################### [100%]   1:vsftpd                 ########################################### [100%]

启动ftp/查询ftp状态

[root@TestServer /]# /etc/init.d/vsftpd start为 vsftpd 启动 vsftpd:                                    [确定][root@TestServer /]# netstat -tul | grep ftptcp        0      0 *:ftp                       *:*                         LISTEN   

配置文件:

主配置文件:/etc/vsftpd/vsftpd.conf

用户控制列表文件:/etc/vsftpd/user_list

FTP相关用户:

匿名用户:anonymous或ftp

本地用户:使用Linux系统中的用户名和密码

虚拟用户:管理员自定义的模拟用户

配置文件详解

[root@TestServer ~]# vim /etc/vsftpd/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).

#
# 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/*允许本地用户上传文件的umask值*/
#
# 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 /*匿名用户是否可以上传文件*/

#no_anon_password=YES/*匿名用户登陆ftp时,询问口令*/

#anon_world_readable_only=YES/*匿名用户对文件是否有下载权限*/

#anon_other_write_enable=YES/*匿名用户是否可以对文件和文件夹进行重命名和删除;同时如果为yes,那么也将允许虚拟用户上传*/

#anon_root=/var/ftp/*匿名用户主目录*/

#anon_max_rate=0/*匿名用户的最大传输限制*/

#anon_umask=022/*匿名用户上传文件的umask值*/

#
# 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 /*用户进入目录时,显示.message文件中的信息*/
#
# 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*/
#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 /*使用标准的ftp日志格式*/
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600 /*600秒没有任何操作就进行端口连接*/
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120 /*资料传输时,超过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 /*开启用户目录限制(启用此行后,ftp用户只能在指定的目录下进行操作,不能访问上一级目录,为了保证系统安全性,本地用户访问,此项必须开)*/
#chroot_list_enable=YES /*当chroot_local_user=yes时,chroot_file下的用户可以切换到其他目录,非chroot_file下的用户不能切换到其他目录*/
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list /*指定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/*设置外挂模块提供的认证服务所使用的配置文件名,即/etc/pam.d/vsftpd文件*/
userlist_enable=YES/*用户登陆限制*/
tcp_wrappers=YES/*是否使用tcp_wrappers作为主机访问控制方式*/

=====================================================================

常用全局配置(非配置文件选项,需要时需要手工添加):

listen_address=192.168.1.1/*监听特定的IP地址连接*/

listen_port=21/*设置监听ftp服务的端口号*/

download_enable=yes/*是否允许下载文件*/

max_clients=0/*限制并发客户端连接数*/

max_per_ip=0/*限制同一ip的并发连接数*/

被动模式

pasv_enable=YES/*开启被动模式*/

pasv_min_port=24500/*被动模式最小端口范围*/

pasv_max_port=24600/*被动模式最大端口*/

pasv_address=对外提供服务的IP地址/*此项用于服务端监听来自IP的端口访问请求*/

常用安全配置

accept_timeout=60/*被动模式,连接超时时间*/

connect_timeout=60/*主动模式,连接超时时间*/

匿名用户访问

1.修改/etc/vsftpd/vsftp.conf配置文件

# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES /*允许匿名用户登陆*/

===================================================================

# 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
anon_upload_enable=YES /*允许匿名用户上传*/

anon_umask=022/*匿名用户上传文件的umask过滤值*/

anon_other_write_enable=yes/*匿名用户可以有删除的权限*/

===================================================================

# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
anon_mkdir_write_enable=YES /*允许匿名用户建立目录*/

2.修改完成后,保存修改并推出;

3.将/var/ftp/pub目录的所有者改为ftp,否则就算配置文件允许上传,服务权限不满足,照样会报错;

4.[root@TestServer /]# /etc/init.d/vsftpd restart或service vsftpd restart/*重启ftp服务,使配置文件生效*/

5.然后就可以使用ip地址匿名访问ftp服务器了(匿名用户(ftp/Anonymous)的默认登陆位置在/var/ftp下)

注意事项:

默认上传目录:/var/ftp/pub/

如果允许上传,需要服务权限和系统目录同时允许

vsftpd服务的伪用户是ftp

本地用户访问

本地用于基本设置

# 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 /*本地用户上传umask权限*/

创建一个非root用户就可以登陆ftp操作了(为了保证系统的安全性,建议ftp用户不要给系统登陆权限)

chroot_local_user=yes/*开启用户目录限制(启用此行后,ftp用户只能在指定的目录下进行操作,不能访问上一级目录,为了保证系统安全性,本地用户访问,此项必须开)*/

local_root=/var/ftp/*设置本地用户的ftp根目录(需注意目录权限)*/

local_max_rate=0/*限制最大传输速率(Byte/m)*/


# 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_local_user=yes,本地用户不能切换到根目录下的上级目录*/
chroot_list_enable=YES/*当chroot_local_user=yes时,chroot_file下的用户可以切换到其他目录,非chroot_file下的用户不能切换到其他目录*/
# (default follows)
chroot_list_file=/etc/vsftpd/chroot_list/*写入/etc/vsftpd/chroot_list文件中的用户可以访问任何目录,其他用户限制在用户主目录中(默认情况下chroot_list文件不存在,需要手工建立)*/

用户访问控制

用户控制列表文件

/etc/vsftpd/ftpusers/*ftp黑名单列表,凡是写入此文件的用户禁止访问ftp服务(黑名单的优先级比白名单更高)*/

/etc/vsftpd/user_list/*默认情况下是黑名单,但是可通过修改配置文件,达到白名单效果*/

访问控制

userlist_enable=YES/*开启用户访问控制*/

userlist_deny=YES/*当userlist_deny=yes,user_liset是一个黑名单;当userliset_deny=no,user_list是一个白名单*/

Userlist_file=/etc/vsftpd/user_list/*写入/etc/vsftpd/user_list文件中的用户不能访问ftp服务器,没有写入的用户可以访问(默认就是这样)*/

虚拟用户访问

配置虚拟用户访问

1.添加虚拟用户口令文件(文件名和位置自拟)

[root@TestServer vsftpd]# touch vuser.txt/*创建一个虚拟用户文件*/
test/*用户名和密码一行一个,如:用户名为test,密码为test123*/

test123

2.生成虚拟用户口令认证文件

yum install db4_utils -y/*如果口令认证命令db_load不存在,则需要按照db4_utils包*/
[root@TestServer vsftpd]# db_load -T -t hash -f /etc/vsftpd/vuser.txt /etc/vsftpd/vuser.db  /*把文本文档转变为认证的数据库*/[root@TestServer vsftpd]# lschroot_list  user_list        vuser.txt  vsftpd_conf_migrate.sh  /*此时目录下会生成一个virtual_user.db的access数据库文件*/ftpusers     vuser.db  vsftpd.conf

3.编辑vsftpd的PAM认证文件

vi /etc/pam.d/vsftpd/*修改/etc/pam.d/vsftpd文件*/

#%PAM-1.0#session    optional     pam_keyinit.so    force revoke#auth       required    pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed#auth       required    pam_shells.so#auth       include     password-auth#account    include     password-auth#session    required     pam_loginuid.so#session    include     password-auth
/*将以上内容进行注释,可以禁止本地用户登陆,因为本地用户登录时的验证依赖这个文件*/
auth required /lib64/security/pam_userdb.so db=/etc/vsftpd/vuser/*如果ftp登陆出现登陆失败的情况,可以把lib改为lib64,既可解决*/
account required /lib64/security/pam_userdb.so db=/etc/vsftpd/vuser

/*添加以上两行内容,进行虚拟用户pam验证*/

4.建立本地映射用户并设置宿主目录权限

useradd vftpuser -d /home/virtualftproot -s /sbin/nologin/*建立本地映射用户(此用户不需要登陆,只是映射用户,由于此用户不需要登陆,所以无需给此用户设置密码;用户名必须和下一步配置文件中一致)*/

chmod 755 virtualftproot/*设置虚拟ftp默认目录权限*/

5.修改配置文件

vi /etc/vsftpd/vsftpd.conf/*修改vsftpd配置文件*/

guest_enable=YES/*开启虚拟用户*/
guest_username=vuser /*ftp虚拟用户对应的系统用户(第四步创建的本地映射用户名)*/
pam_service_name=vsftpd/*pam认证文件(生成pam数据库的文件名),此行默认存在*/

6.重启vsftpd服务,并测试

[root@TestServer /]# service vsftpd restart关闭 vsftpd:                                              [确定]为 vsftpd 启动 vsftpd:                                    [确定]
/*此时虚拟用户可以登录,查看,下载,不能上传,默认上传位置是宿主用户的家目录,权限使用的是匿名用户权限进行管理*/

7.调整虚拟用户权限

anon_upload_enable=YES/*匿名用户可以上传*/

anon_mkdir_write_enable=YES/*匿名用户可以建立文件夹*/

anon_other_write_enable=yes/*匿名用户可以上传其他类型的文件*/

虚拟用户单独定义访问控制权限

可以给没个虚拟用户单独建立目录,并建立自己的配置文件。这样方便单独配置权限,并可以单独指定上传目录。

1.修改配置文件(vi /etc/vsftpd/vsftpd.conf)

user_config_dir=/etc/vsftpd/vusers_dir/*指定保存虚拟用户配置文件的目录*/

2.手工建立目录(目录名必须和user_config_dir所指定的配置文件目录名一致)

mkdir vusers_dir

3.为虚拟用户建立配置文件(这里以JinDaoer为用户建立配置文件)

[root@TestServer vusers_dir]# touch JinDaoer/*为JinDaoer建立用户配置文件*/

[root@TestServer vusers_dir]# vi JinDaoer
anon_upload_enable=yes/*允许JinDoaer用户上传文件*/
anon_mkdir_write_enable=yes/*允许JinDaoer用户创建目录*/
anon_other_write_enable=yes/*匿名用户是否可以对文件和文件夹进行重命名和删除;同时如果为yes,那么也将允许虚拟用户上传*/
local_root=/tmp/JinDaoer/*指定JinDaoer的上传根目录*/

4.为虚拟用户(JinDaoer)创建上传目录并赋予系统权限

[root@TestServer vsftpd]# mkdir /tmp/JinDaoer

[root@TestServer vsftpd]# chown vuser:vuser vusers_dir//*虚拟用户全部是通过本地映射用户访问的*/

(注意:如果用户不在vusers_dir目录下,那么此用户遵循vsftpd.conf文件的配置)

5.服务测试(权限修改)

配置效果:

禁止匿名登陆

禁止本地系统登陆

允许虚拟用户登陆

JinDaoer上传目录是/tmp/JinDaoer

/etc/JinDaoer可以查看、上传、删除、重命名、创建/删除文件夹、但不可下载文件

0 0