FTP(File Transfer Protocol,文件传输协议),是一种应用层协议,能够实现很好的实现跨平台,可是没法实现一些其余的功能,像如文件系统挂载等功能。NFS(Network File System,网路文件系统)是工做在内核模式下的,所以不能很好的实现跨平台,通常只能是Linux主机或者Unix主机之间,但能够实现挂载使用等功能。SMB(Service Message Block,服务消息块协议),可以实现Windows和Linux主机之间的文件共享服务,可实现跨平台,在Linux上实现了CIFS(Common Internet File System)协议。mysql
是一种C/S架构,基于套接字通讯,用来在两台机器之间相互传输文件。FTP协议用到2种tcp链接:一是命令链接,用于客户端和服务端之间传递命令,监听在tcp/21端口;另外一个是数据传输链接,用来传输数据,监听的端口是随机的。linux
主动模式的原理以下图:sql
主动模式存在的问题是,在客户端通常都会有防火墙的设置,当服务端与客户端数据进行数据通讯时,客户端的防火墙会将服务端的端口挡在外面。此时,通讯就会受阻。所以,被动模式就产生了。数据库
被动模式的原理图以下:vim
被动模式也会存在防火墙的问题,客户端与服务端传输数据时,在服务端也会有防火墙,但在服务端的防火墙有链接追踪的功能,解决了防火墙的问题。所以,通常使用被动模式比较多。windows
FTP支持系统用户,匿名用户,和虚拟用户三种用户认证。centos
匿名用户:登录用户名是anonymous,没有密码安全
系统用户:是FTP服务器端的本地用户和对应的密码,默认访问的是用户家目录服务器
虚拟用户:仅用于访问服务器中特定的资源,常见的虚拟用户认证的方式有使用文件认证或使用数据库进行认证。最终也会将这些虚拟用户同一映射为一个系统用户,访问的默认目录就是这个系统用户的家目录。session
1**:提示信息
2**:成功执行的状态码
3**:须要进一步提供补充类的信息码,例如在输入用户帐号信息后出现此状态,提示继续输入密码
4**:客户端类的错误
5**:服务端错误
服务端:
Linux端:wu-ftpd,pureftp,vsftpd(Centos 6上默认提供的)
windows端:ServU,FileZilla-Server
客户端工具:
Linux操做系统:ftp,lftp,lftpget,wget,cul,gftp等
windows操做系统:FileZilla
在CentOS上默认提供的是vsftpd(Very Secure FTP),以安全著称。
用户认证配置文件:/etc/pam.d/vsftpd
服务脚本:/etc/rc.d/init.d/vsftpd
配置文件目录:/etc/vsftpd
主配置文件:vsftpd.conf
匿名用户(映射为ftp用户)共享资源位置:/var/ftp
系统用户经过ftp访问的资源的位置:用户本身的家目录
虚拟用户经过ftp访问的资源的位置:给虚拟用户指定的映射成为的系统用户的家目录
Vsftpd
[root@centos ~]# rpm -ql vsftpd
/etc/logrotate.d/vsftpd 日志轮训备份配置文件
/etc/pam.d/vsftpd 基于pam.d 用户认证配置文件
/etc/rc.d/init.d/vsftpd
/etc/vsftpd 主要配置文件目录
/etc/vsftpd/ftpusers 黑名单
/etc/vsftpd/user_list 根据主配置文件设置为黑名单仍是白名单
/etc/vsftpd/vsftpd.conf 主配置文件
/etc/vsftpd/vsftpd_conf_migrate.sh
/usr/sbin/vsftpd 主程序
匿名用户(映射为ftp用户)共享位置为:/var/ftp
系统用户经过ftp访问时资源位置:用户本身的家目录
虚拟用户ftp访问时资源位置:给虚拟用户指定的映射成为的系统用户的家目录
/etc/vsftpd/vsftpd.conf配置文件详解:
# 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 本地用户是否有写权限
#
# 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 是否开启日志功能
#
# 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 把用户上传文件属组改变为指定的文件属组。例如改变成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)0.-**
#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 所的本地用户锁定在本身的家目录
#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 with two configuration files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES 是否启用userlist文件
userlist_deny=YES|NO YES时为黑名单,NO时为白名单
tcp_wrappers=YES
匿名用户配置:
anonymous_enable=YES 是否启用匿名用户访问。默认为打开。
anon_root=/var/ftp 匿名访问FTP的路径,默认为/var/ftp
anon_upload_enable=YES 是否打开匿名用户上传。默认关闭。
anon_mkdir_write_enable=YES 是否打开匿名用户创建目录,默认关闭
anon_other_write_enable=YES 是否容许匿名用的其余操做。好比说删出。
anon_max_rate=0 匿名用户最大传输速度,0时为无限制
anon_umask=077 匿名上传权限掩码
本地用户配置:
local_enable=YES 系统用户登陆,访问到本身的家目录,很危险须要锁在本身的家目录
local_umask=022 限制本地用户上传的文件的权限
write_enable=YES 本地用户是否有写权限
chroot_local_user=YES 全部本地用户锁定在本身的家目录
chroot_list_enable=YES 锁定指定用户的在本身的家目录
chroot_list_file=/etc/vsftpd/chroot_list 锁定指定用户的在本身的家目录的具体配置文件。
local_max_rate=0 本地用户数据传输率
全局配置:
listen=YES 是否监听端口
listen_port=21 监听入站的FTP请求的端口号
download-enable=YES 若是设置为NO,将拒绝全部的下载请求
dirmessage_enable=YES 用户进入目录是否显示消息
connetct_from_port_20=YES 使用主动模式链接,启用20端口
pasv_enable=YES 是否启用被动模式链接,默认为被动
pasv_max_port=24600 被动模式链接的最大端口号
pasv_min_port=24500 被动模式的最小端口号
userlist_enable=YES 是否启用userlist文件
userlist_deny=YES|NO YES时为黑名单,NO时为白名单
xferlog_enable=YES 是否开启xferlog 日志功能
xferlog_std_format=YES 日志文件格式
xferlog_file=/var/log/xferlog 日志存在在那个地方
max_clients=0 最大链接并发数,等0时无限制,
max_per_ip: 每一个IP能够同时发起的并发请求数
write_enable=YES 本地用户是否有写权限
tcp_wrappers=YES 是否启用tcp_wrappers
guest_enable=YES 若是为YES,则全部的非匿名登陆都映射为guest_username指定的帐户
guest_username=ftp 上面选项为YES时,的帐户名
user_config_dir=/etc/vsftd/conf 指定目录,在该目录下能够为用户设置独立的配置文件和选项
dual_log_enable=NO 是否开启双日志功能
anonymous_enable=YES 是否开启匿名登陆 默认为开启
chown_uploads=YES 是否容许把用户上传的文件属组改变
chown_username=whoever 把用户上传文件属组改变为指定的文件属组。例如改变成whoever
idle_session_timeout=600 会话时长
data_connection_timeout=120 数据链接操时时长
pam_service_name=vsftpd pam 认证
主动链接:
命令:client: 5000----->server: 21/tcp
数据:server: 20 /tcp-------> client:5000+
被动链接:
命令:client: 5000------->server:21 tcp
服务器告诉客户端口本身的端口。121*256+23.客户端计算后链接这个端口进行数据传输。
数据:client: 5000+ ------>server:随机端口
# yum -y install vsftpd mysql-server mysql-devel pam_mysql
# cd /lib64/security/ 验证模块位置
Vsftpd+mysql+pam.d(单台服务器)
1、事先安装好开发环境和mysql数据库;
# yum -y install mysql-server mysql-devel vsftpd pam_mysql
# yum -y groupinstall "Development Tools" "Development Libraries"
2.安装pam_mysql-0.7RC1
# tar zxvf pam_mysql-0.7RC1.tar.gz
# cd pam_mysql-0.7RC1
# ./configure --with-mysql=/usr --with-openssl
# make
# make install
2、建立虚拟用户帐号
1.准备数据库及相关表
首先请确保mysql服务已经正常启动。然后,按须要创建存储虚拟用户的数据库便可,这里将其建立为vsftpd数据库。
mysql> create database vsftpd;
mysql> grant select on vsftpd.* to vsftpd@localhost identified by '123456';
mysql> grant select on vsftpd.* to vsftpd@127.0.0.1 identified by '123456';
mysql> flush privileges;
mysql> use vsftpd;
mysql> create table users (id int AUTO_INCREMENT NOT NULL,name char(20) binary NOT NULL,password char(48) binary NOT NULL,primary key(id));
2、添加测试的虚拟用户
根据须要添加所须要的用户,须要说明的是,这里将其密码采用明文格式存储,缘由是pam_mysql的password()函数与MySQL的password()函数可能会有所不一样。
mysql> INSERT INTO users (name,password) VALUES ('tom',‘tompassword’);
mysql> INSERT INTO users (name,password) VALUES ('jack','jackpassword');
3、配置vsftpd
1.创建pam认证所需文件
#vi /etc/pam.d/vsftpd.mysql
添加以下两行
auth required /lib64/security/pam_mysql.so user=vsftpd passwd=123456 host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=0
account required /lib64/security/pam_mysql.so user=vsftpd passwd=123456 host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=0
2.修改vsftpd的配置文件,使其适应mysql认证
创建虚拟用户映射的系统用户及对应的目录
#useradd -s /sbin/nologin -d /var/ftproot vuser
#chmod go+rx /var/ftproot
请确保/etc/vsftpd.conf中已经启用了如下选项
anonymous_enable=YES
local_enable=YES
write_enable=YES
anon_upload_enable=NO
anon_mkdir_write_enable=NO
chroot_local_user=YES
然后添加如下选项
guest_enable=YES
guest_username=vuser
并确保pam_service_name选项的值以下所示
pam_service_name=vsftpd.mysql
4、启动vsftpd服务
# service vsftpd start
# chkconfig vsftpd on
查看端口开启状况
# netstat -tnlp |grep :21
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 23286/vsftpd
基于mysql控制vsftp的用户认证机制(两台服务器)
第一mysql准备工做:假如:172.16.10.109为mysql 数据库:
1:创建vsftp库:
mysql> create database vsftpd;
2:给来来访的的vsftp电脑受权
mysql> grant select on vsftpd.* to vsftpd@172.16.10.107 identified by '123456';
MariaDB [vsftpd]> FLUSH PRIVILEGES;
3:建立用户表:
mysql> create table users (id int AUTO_INCREMENT NOT NULL,name char(20) binary NOT NULL,password char(48) binary NOT NULL,primary key(id));
4:添加测试的虚拟用户
mysql> INSERT INTO users (name,password) VALUES ('tom',password('tompassword'));
mysql> INSERT INTO users (name,password) VALUES ('jack',password('jackpassword'));
在ftp服务链接mysql数据库服务器,看链接的上不,注意防火墙和SElinux
# mysql -uvsftpd -h172.16.10.109 -p
第二配置vsftpd,服务器IP为172.16.10.107
1:创建pam认证的文件#vi /etc/pam.d/vsftpd.mysql
添加以下两行
auth required /lib64/security/pam_mysql.so user=vsftpd passwd=123456 host=172.16.10.109 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2
account required /lib64/security/pam_mysql.so user=vsftpd passwd=123456 host=172.16.10.109 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2
2.修改vsftpd的配置文件,使其适应mysql认证
创建虚拟用户映射的系统用户及对应的目录
#useradd -s /sbin/nologin -d /var/ftproot vuser
#chmod go+rx /var/ftproot
请确保/etc/vsftpd.conf中已经启用了如下选项
anonymous_enable=YES
local_enable=YES
write_enable=YES
anon_upload_enable=NO
anon_mkdir_write_enable=NO
chroot_local_user=YES
然后添加如下选项
guest_enable=YES
guest_username=vuser
并确保pam_service_name选项的值以下所示
pam_service_name=vsftpd.mysql
4、启动vsftpd服务
# service vsftpd start
# chkconfig vsftpd on
查看端口开启状况
# netstat -tnlp |grep :21
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 23286/vsftpd
使用虚拟用户登陆,验正配置结果,默认为只读访问。
5、配置虚拟用户具备不一样的访问权限
vsftpd能够在配置文件目录中为每一个用户提供单独的配置文件以定义其ftp服务访问权限,每一个虚拟用户的配置文件名同虚拟用户的用户名。配置文件目录能够是任意未使用目录,只须要在vsftpd.conf指定其路径及名称便可。
一、配置vsftpd为虚拟用户使用配置文件目录
# vim vsftpd.conf
添加以下选项
user_config_dir=/etc/vsftpd/vusers
二、建立所须要目录,并为虚拟用户提供配置文件
# mkdir /etc/vsftpd/vusers
# cd /etc/vsftpd/vusers
# touch tom jerry
三、配置虚拟用户tom的访问权限:
anon_upload_enable=YES 可上传
anon_mkdir_write_enable=YES 能够创建目录
anon_other_write_enable=YES 其余全部权限
jack只能上传
anon_upload_enable=YES
ftp链接后的操做命令:
mkdir 创建目录
rmdir 删除目录
del 删除文件
put 将本地文件上传到主机
get 下载