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.www58058.com/5053

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

相關推薦

  • 硬鏈接與軟鏈接

    硬連接:          其本質就是:一個文件,多個名字。注意:目錄是沒有硬鏈接的。 用ln filename linkname創建硬鏈接,注意源文件和鏈接文件位置順序,硬鏈接不可以跨分區建。 在創建硬鏈接時會增加硬連接數,可以通過ls –l查看,如下圖所示的,可看到硬鏈接數。…

    2017-07-20
  • 實時文件查找工具–find

    find [option] …[查找路徑] [查找條件] [處理動作] 起始路徑:指定具體的目標路徑,默認為當前目錄 查找條件:指定查找標準,可以根據文件名,權限,文件大小等標準進行。默認為指定路徑下的所有文件 處理動作:對符合條件的文件做什么操作 1 查找條件: (1)根據文件名查找: ?-name “文件名稱” 支持使用glob -iname…

    Linux干貨 2017-07-02
  • 批量創建用戶(8月1日作業)

    1、創建用戶gentoo,附加組為bin和root,默認shell為/bin/csh,注釋信息為"Gentoo Distribution" 2、創建下面的用戶、組和組成員關系 名字為admins的組;  用戶natasha,使用admins作為附屬組;  用戶harry,也使用admins作為附屬組 &nbs…

    Linux干貨 2016-08-04
  • 第七周作業

    1、創建一個10G分區,并格式為ext4文件系統;    (1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;        (2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳; [ro…

    2017-04-05
  • iptables

    規則格式:iptables   [-t table]   COMMAND   chain   [-m matchname [per-match-options]]   -j targetname [per-target-options]     -t table:&nb…

    Linux干貨 2017-06-16
  • 網絡21期第十周博客作業

    網絡21期第十周博客作業 1、請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情) Centos6啟動流程:       POST加電自檢 —> BOOT Sequence —> 加載內核啟動系統  &…

    Linux干貨 2016-09-15
欧美性久久久久