1、理论比较重要,安装很简单。先理论ftp简介
1>ftp服务两个链接,控制连接21,数据连接(根据主动被动,从服务器角度出发。)分两种情况如下所列。
FTP: tcp, 两个连接
命令连接,控制连接:21/tcp
数据连接:
主动模式:20/tcp,服务器接受命令后用20端口去连接客户端的随机端口
被动模式:端口随机,服务器给出自己的服务端口信息,由客户端来连接服务端端口
数据传输模式(自动模式):
二进制:
文本:
ftp server --> ftp client
2>服务端和客户端的实现软件很多,如下所列。这里主要介绍:服务端——vsftp服务、客户端——命令行ftp。
服务器端程序:
wu-ftpd:
vsftpd: Very Secure ftp Daemon
proftpd:
pureftpd
Filezilla
Serv-U
客户端程序:
CLI:
ftp
lftp
GUI:
gftpd
FlashFXP
Cuteftp
Filezilla
3>其他信息
vsftpd:
/etc/vsftpd: 配置文件目录
/etc/init.d/vsftpd: 服务脚本
/usr/sbin/vsftpd: 主程序
基于PAM实现用户认证:
/etc/pam.d/*
/lib/security/*
/lib64/security/*
支持虚拟用户
vsftpd: (ftp, ftp)
/var/ftp:ftp服务根目录
ftp: 系统用户
匿名用户 --> 系统用户: anonymous_enable
系统用户: local_enable
虚拟用户 --> 系统用户
/var/ftp: ftp用户的家目录
匿名用户访问目录
2、了解完以上信息,直接安装测试。上边不明白的地方看操作即可。
前提:这里不涉及到iptables和selinux,这两个功能会对vsftpd产生很大影响。先关闭
关闭selinux:setenforce 0
查看是否关闭:getenforce 显示Permissive即为关闭
停止iptables:service iptables stop
yum list installed | grep -i vsftpd #查看vsftpd是否安装
yum install vsftpd -y #安装vsftpd。如果安装过可以先卸载yum remove vsftpd
rpm -ql vsftpd #查看安装生成的文件和目录。
service vsftpd start #启动vsftpd
chkconfig vsftpd on #加入启动列表 chkconfig --list | grep -i vsftpd #查看是否加入完毕
ftp 127.0.0.1 #登入ftp服务端,端口默认21,如果改变了端口:ftp 127.0.0.1 xxxx
用户:anonymous 密码:随意 或者用户:ftp 密码:随意
注:默认没改变任何配置文件的情况下。匿名用户会被映射为ftp用户,家目录/var/ftp。系统用户会被映射到各自的家目录下。
通俗的讲,通过ftp登录到服务端的用户最终都会被映射为操作系统上的用户,映射为那个用户就会被放到那个用户的家目录下,anonymous被映射为ftp用户。
如果你是系统用户 那么你在你的目录下将会有所有权限,并且还可以cd到操作系统的“/”目录下(这是很危险的,后边配置文件中应该予以进制)。
如果系统用户登录不上去,请确保你的账号没问题,并关闭selinux。
如果你是anonymous用户那你的家目录/var/ftp,在这个目录里边或者pub目录里边你只有读的权限,没有任何写的权限。
一般我们会简单的配置:系统用户对其家目录有全部的权限,不可以cd到其他目录。匿名用户也必须在自己家目录(/var/ftp),并且匿名用户对/var/ftp/pub目录只有读权限,这也是默认配置。所以我们只需要自己在/var/ftp/下目录下新建一个目录并且给予ftp用户写的权限就可以了。
配置文件详解:
# 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 #允许匿名用户登录,默认也是允许,设置为NO不允许匿名登录。建议打开,这样方便共享文件
#
# Uncomment this to allow local users to log in.
local_enable=YES #允许系统用户登录,配合下方的write_enable选项使用
#
# 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=YES #允许匿名用户在其家目录/var/ftp及其子目录下有上传的权限,注意还需要这个目录有写权限(chmod o+w /var/ftp/upload 或者使用setfacl -m u:username:rwx /var/ftp/upload)。需要明白ftp服务配置了用户有(上传、创建目录、删除目录的权限)。还必须使这个用户对这个目录有写权限。两者取交集,否则配置的 权限不生效.
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
anon_mkdir_write_enable=YES #允许匿名用户对/var/ftp/机器子目录有创建目录权限,注意该用户对于该目录也要有写权限(chmod o+w /var/ftp/upload).这样你就可以创建目录了,但注意还不能删除目录和删除文件。
#anon_other_write_enable=YES#这里才是允许匿名用户对目录(/var/ftp/)及其子目录下的文件或目录具有删除的权限,一般不建议打开。注意用户必须对此目录有写权限(chmod o+w /var/ftp/upload)
#说的简单点就是,你在ftp例配置了哪些用户具有哪些权限后,还必须保证你配置的用户对该目录也得有写权限。才会生效。否则不生效。
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES #这里是默认登录时给予提示信息,不太重要。你可以在某个目录下 创建一个文件:.message 随便写内容。当用户ftp到系统cd到这个目录即可显示内容。
#
# The target log file can be vsftpd_log_file or xferlog_file.
# This depends on setting xferlog_std_format parameter
xferlog_enable=YES #开启日志记录功能,记录用户的上传下载信息,配合下边xfer_file使用,否则不生效
#
# 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 #这个是上边改变属主的主是谁。可随便定义个系统用户root除外。
#
# 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 #即当数据传输结束后,用户连接FTP服务器的时间不应超过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 #所有用户都必须在自己家目录下,不可cd到其他目录。必须,为了安全
#chroot_list_enable=YES#此选项和下一个选项共同控制哪些用户不允许cd到系统“/”目录下。
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list #此文件需要自己建立,写下不允许cd到系统/目录下的用户名即可。不要开启好了。
#
# 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#定义服务为独立守护进程,如果用瞬时守护进程需要配置在xinited服务中提供脚本(格式有规定)。独立守护进程适合于多用户压力打的情景,瞬时守护进程适合访问不高的服务。
#
# 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
# 在/etc目录中的hosts.allow和hosts.deny两个文件用于设置tcp_wrappers的访问控制
# 前者设置允许访问记录,后者设置拒绝访问记录。
# 如想限制某些主机对FTP服务器192.168.57.2的匿名访问,编缉/etc/hosts.allow文件,如在下面增加两行命令:
# vsftpd:192.168.57.1:DENY 和vsftpd:192.168.57.9:DENY
# 表明限制IP为192.168.57.1/192.168.57.9主机访问IP为192.168.57.2的FTP服务器
# 此时FTP服务器虽可以PING通,但无法连接
#用户访问控制的进一步细化。可参看配置。具体我也没亲自试过,配置完成后请测试下,以免出现错误。
Userlist_enable=YES | Ftpusers中用户禁止访问 |
Userlist_enable=NO | Ftpusers中用户禁止访问 |
Userlist_deny=YES | Ftpusers中用户禁止访问(登录时可以看到密码输入提示,但仍无法访问) |
Userlist_deny=NO | ftpusers中用户禁止访问 |
Userlist_enable=YES 并且 | Ftpusers中用户禁止访问 |
Userlist_enable=YES 并且 | Ftpusers中用户禁止访问 |
其他参考信息:
控制用户访问
max_client设置项 用于设置FTP服务器所允许的最大客户端连接数,值为0时表示不限制。例如max_client=100表示FTP服务器的所有客户端最大连接数不超过100个。
max_per_ip设置项 用于设置对于同一IP地址允许的最大客户端连接数,值为0时表示不限制。例如max_per_ip=5表示同一IP地址的FTP客户机与FTP服务器建立的最大连接数不超过5个。
local_max_rate设置项 用于设置本地用户的最大传输速率,单位为B/s,值为0时表示不限制。例如local_max_rate=500000表示FTP服务器的本地用户最大传输速率设置为500KB/s.
anon_max_rate设置项 用于设置匿名用户的最大传输速率,单位为B/s,值为0表示不限制。例如ano_max_rate=200000,表示FTP服务器的匿名用户最大传输速率设置为200KB/s.
修改默认端口
默认FTP服务器端口号是21,出于安全目的,有时需修改默认端口号,修改/etc/vsftpd/vsftpd.conf,添加语句(例):
listen_port=4449
语句指定了修改后FTP服务器的端口号,应尽量大于4000。修改后访问
#ftp 192.168.57.2 4449
注意这里需加上正确的端口号了,否则不能正常连接。
设置用户组
有关FTP用户和用户组的重要性,我们在之前介绍vsftpd的时候便已经提到过。这里主要是简单的说明用户组的技术实现,至于具体如何应用,还是具体需求具体对待。
1
2
3
4
5
6
7
8
9
10
11
|
#mkdir -p /home/try 递归创建新目录
#groupadd try 新建组
#useradd -g try -d /home/try try1 新建用户try1并指定家目录和属组
#useradd -g try -d /home/try try2 新建用户try2并指定家目录和属组
#useradd -g try -d /home/try try3 新建用户try3并指定家目录和属组
#passwd try1 为新用户设密码
#passwd try2 为新用户设密码
#passwd try3 为新用户设密码
#chown try1 /home/try 设置目录属主为用户try1
#chown .try /home/try 设置目录属组为组try
#chmod 750 /home/try 设置目录访问权限try1为读,写,执行;try2,try3为读,执行
|
由于本地用户登录FTP服务器后进入自己主目录,而try1,try2 try3对主目录/home/try分配的权限不同,所以通过FTP访问的权限也不同,try1访问权限为:上传,下载,建目录;try2,try3访问权限为下载,浏览,不能建目录和上传。实现了群组中用户不同访问级别,加强了对FTP服务器的分级安全管理。
安全保障:ftp是明文传输的,因此如果数据比较隐私,就必须加密。
ftp安全的两种实现方式:ftps 基于ssl/tls、sftp基于ssh的安全传输方式
以ftps为例:
1>
##################################################################################################
虚拟用户认证:基于mysql的认证配置过程如下,有需要的可参考。