vsftpd基于mysql实现用户认证

一、前言

  ftp介绍:

    ftp全程是File Transfer Protocol(文件传输协议),方便于实文件交换;但是在文件传输以及账号密码发送时都是以明文传输,因此是一个明文协议

ftp是C/S方式:

  常见的客户端有:

    GUI方式:browers、FileZilla-client、FlashFXP、CuteFTP、GFTP

    CLI方式: lftp、wget、curl、lftpget 

  常见的服务器端:

    WU-FTPD、ProFTPD、pureftp、vsftp、Serv-U、FileZilla-server

vsftp介绍:

   vsftp全程是Very Secure FTP(非常安全的ftp),它的最大优势就是非常安全(当然这是自己说的);是基于GPL发部的在linux或Unix上的ftp服务器软件

  监听端口:监听在TCP的21号端口

  连接类型:

    命令连接:实现命令传输的连接,文件管理类命令,始终处于连接状态

    数据连接:数据的传输,按需创建,有两种工作模式,分别是主动模式和被动模式

      主动模式:客户端通过TCP的三层握手,与服务器端建立命令连接,命令连接建立后,服务器会主动通过tcp的20端口去连接客户端发送数据,默认去连接客户端建立命令连接端口+1端口,(如:客户端使用4001端口建立命令连接,则服务器端会主动去连接客户的4002端口,如果端口被占用,继续+1,依次类推)

       ftp1.png

       被动模式:客户端通过tcp的三次握手,与服务器端建立命令连接,命令连接建立后,服务器端通过命令连接告知客户端自己已打开数据传输端口,客户端收到信息后,通过请求服务器端告知的端口获得数据;

         ftp2.png

      

     

  数据传输格式有两种:

     文本数据传输:ASCII

     二进制数据:BINARY

  ftp响应码:

    1xx: 信息类

    2xx:成功类信息

    3xx: 提示需要进一步补全内容类

    4xx: 客户端错误

    5xx:服务器端错误

  用户认证:用户通过ftp协议访问自己的家目录中的数据,可以通过ftp便览整个文件系统,为了安全起见将用户禁锢在家目录中 

    匿名用户认证:映射为一个系统用户ftp,为ftp用户设置权限

    OS用户:通过nsswitch服务解决用户名称解析,通过pam对系统用户进行认证(/etc/passwd,/etc/shadow)

    虚拟用户:映射为系统某一用户,用户账号密码存储于非/etc/passwd,/etc/shadow

     

  

二、基于Centos6.6下的vsftp软件包介绍

  启动脚本:/etc/rc.d/init.d/vsftpd

  用户控制文件:/etc/vsftpd/ftpusers

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

vsftpd配置文件介绍 :

# 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     \\允许用户有写入的权限  
#
# 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  \\是否允许匿名用户上传文件,[YES | NO]
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES  \\是否允许匿名用户有创建目录的权限 [YES | NO]
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES  \\是否启动消息目录 [YES | NO]
#
# The target log file can be vsftpd_log_file or xferlog_file.
# This depends on setting xferlog_std_format parameter
xferlog_enable=YES  \\是否启用xferlog日志 [YES | NO]
#
# 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 \\是否启动上传ftp文件后,更改文件的属主 [YES | NO] 
#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  \\定义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  \\是否使用标准的xferlog模式 [YES | NO]
# 
# 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  \\运行vsftpd需要非特权系统用户,默认是nobody
#
# 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  \\设置是否允许执行特殊的ftp命令async ABOR
#
# 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码上传文件 [YES | NO]
#ascii_download_enable=YES \\设置是否使用ascii码下载文件 [YES | NO]
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service. \\设置定义登录ftp的欢迎语
#
# 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  \\将所有的本地用户禁锢在家目录
#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  \\是否允许递归 [YES | NO]
#
# 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 \\设置vsftpd是否处于监听状态
#
# 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 \\是否启用ipv6地址监听

guest_enable=YES \\是否启用来宾账号(默认没有) 
guest_username=vuser \\将来宾账号映射为那一个系统用户(默认没有)
pam_service_name=vsftpd.mysql \\pam认证文件 
user_config_dir=/etc/vsftpd/vuser_config\\设置匿名用户的权限配置文件位置 (默认没有)
userlist_enable=YES  \\此选项与下面选项userlit_deny都为YES时,/etc/vsftpd/user_list为黑名单,
userlist_deny=YES     \\若userlist_enable为YES,userlist_deny=NO则此文件为白名单
tcp_wrappers=YES  \\支持tcp_wrappers访问限制(/etc/{hosts.allow,hosts.deny}

max_clients=1024  \\限制最大并发连接数(默认没有此选项)
max_per_ip=1024   \\限制每个ip同时请求的连接数(默认没有此选项)
anon_max_rate=1024 \\限制匿名用户的传输速率(默认没有此选项)
local_max_rate=1024 \\限制本地用户的传输速率(默认没有出选项)

三、vsftpd+mysql+pam实现过程:

  1、创建用于vsftpd的数据库,在创建表,用于存储虚拟用户账号与密码

   F1.png

  f2.png

    

  2、添加虚拟用户

f3.png

 3、授权admin用户对vuser表只有查询的权限

  f4.png

 

 4、创建虚拟用户的映射用户

   f5.png

   

 5、编辑/etc/vsftpd/vsftpd.conf,添加以下信息

   f6.png

    

 6、在/etc/pam.d/目中中创建vsftpd.mysql,内容如下

  (1)确保安装pam_mysql之后生成了认证时所需要的共享库

f9.png

  

 (2)在/etc/pam.d/目录中创建认证文件

f8.png

7、重新启动vsftpd服务

 f10.png  

8、使用虚拟用户登录ftp,验证是否可以成功登录

  f11.png 

 9、为不同的虚拟用户设置不同的权限;

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

   f12.png

 

  (2)默认是没有此目录的,需要手动创建

   f13.png

  

  (3)在此目录下,分别创建以tom,obama等用户为名称的文件,内容如下

    f14.png f15.png

 

  (4)重启vsftpd服务,测试权限

   f16.png

   f17.png

   f18.png

 (5)vsftpd已经实现基于mysql的认证,可以尽情使用了 

原创文章,作者:马行空,如若转载,请注明出处:http://www.178linux.com/5053

(0)
上一篇 2015-06-15 16:25
下一篇 2015-06-15 16:40

相关推荐

  • linux 学习笔记

    第二周

    Linux干货 2018-03-16
  • 网卡别名与bonding

    网络接口配置-bonding Bonding简介 Bonding 就是将多块网卡绑定同一IP地址对外提供服务,可以实现高 可用或者负载均衡。然,直接给两块网卡设置同一IP地址 是不可能的。通过bonding,虚拟一块网卡对外提供连接, 物理网卡的被修改为相同的MAC地址。 Bonding模式 Mode 0 (balance-rr) 轮转(Round-robi…

    Linux干货 2016-09-05
  • 8.9作业

    删除/etc/grub2.conf文件中所有以空白开头的行,行首的空白字符 [root@English6 ~]# sed "s@^[[:space:]]\+@@" /etc/grub.conf  # grub.conf generated by an…

    Linux干货 2016-08-11
  • LVM(逻辑卷)(logical volume manager),快照卷

    逻辑卷的创建,扩展,缩减,迁移,删除。快照卷的创建

    Linux干货 2017-12-11
  • redis-cli的一些有趣也很有用的功能

    redis-cli我们最常用的三个参数就是-h、-p、-a选项,分配用来指定连接的redis-server的host、port和登录密码。通过redis-cli –help发现,redis-cli还提供了其他很多的参数和功能。 1)-x-x选项从标准输入(stdin)读取最后一个参数。 比如从管道中读取输入: echo -en &quot…

    Linux干货 2016-04-13
  • Linux cluster集群全讲解

                     Linux cluster集群 Linux cluster(集群): cluster:计算机组合,为解决某个特定问题组合起来形成的单个系统;   Linux Cluster类型:    LB:Load Balancing,负载均衡;    HA:High Availiablity,高可用;    A=MTBF(平均无故障时长…

    2016-11-18