CentOS 6.2配置vsftpd虛擬用戶

 


  1. -------------------------------------------------------------------------------------------
    環境:CentOS 6.2 32bit

  2. 配置好網絡yum源(在此省略)

  3. -------------------------------------------------------------------------------------------
    一、##########安裝vsftpd相關組件##########

  4. [root@www ~]# yum install vsftpd*

  5. [root@www ~]# yum install pam*

  6. [root@www ~]# yum install db4*

  7. ---------------------------------------------------------------------------------------------

  8. 二、#######建立控制服務用戶和虛擬宿主用戶##########

  9. 1.建立Vsftpd服務的宿主用戶:


  10. [root@www ~]# useradd vsftpd -s /sbin/nologin


  11. 默認的Vsftpd的服務宿主用戶是root,但是這不符合安全性的需要。這裏建立名字爲vsftpd的用戶,用他來作爲支持Vsftpd的服務宿主用戶。

  12. 由於該用戶僅用來支持Vsftpd服務用,因此沒有許可他登陸系統的必要,並設定他爲不能登陸系統的用戶。

  13. 2.建立Vsftpd虛擬宿主用戶:


  14. [root@www ~]# useradd virtualftp -s /sbin/nologin


  15. 本篇主要是介紹Vsftp的虛擬用戶,虛擬用戶並不是系統用戶,也就是說這些FTP的用戶在系統中是不存在的。他們的總體權

  16. 限其實是集中寄託在一個在系統中的某一個用戶身上的,所謂Vsftpd的虛擬宿主用戶,就是這樣一個支持着所有虛擬用戶的

  17. 宿主用戶。由於他支撐了FTP的所有虛擬的用戶,那麼他本身的權限將會影響着這些虛擬的用戶,因此,處於安全性的考慮,

  18. 也要非分注意對該用戶的權限的控制,該用戶也絕對沒有登陸系統的必要,這裏也設定他爲不能登陸系統的用戶。(這裏插

  19. 一句:原本在建立上面兩個用戶的時候,想連用戶主路徑也不打算給的。本來想加上 -d /home/nowhere 的,

  20. 據man useradd手冊上講述:-d, --home HOME_DIR
               The new user will be created using HOME_DIR as the value for the user′s login directory. The default is to append

  21. the LOGIN name to BASE_DIR and use that as the login directory name. The directory HOME_DIR does not have to exist but will

  22. not be created if it is missing

  23. 使用-d參數指定用戶的主目錄,用戶主目錄並不是必須存在的。如果沒有存在指定的目錄的話,那麼它將不會被建立”。

  24. 三.調整Vsftpd的配置文件:


  25. 1.編輯配置文件前先備份
    [root@www ~]# cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.backup


  26. 2.編輯主配置文件Vsftpd.conf
    [root@www ~]# vim /etc/vsftpd/vsftpd.conf

  27. 這裏我將原配置文件的修改完全記錄,凡是修改的地方我都會保留註釋原來的配置。其中加入我對每條配

  28. 置項的認識,對於一些比較關鍵的配置項這裏我做了我的觀點,並且原本英語的說明我也不刪除,供參考對比用。
    ------------------------------------------------------------------------------
    # Allow anonymous FTP? (Beware - allowed by default if you comment this out).
    #anonymous_enable=YES

  29. anonymous_enable=NO
    設定不允許匿名訪問

  30. # Uncomment this to allow local users to log in.
    local_enable=YES
    設定本地用戶可以訪問。注意:主要是爲虛擬宿主用戶,如果該項目設定爲NO那麼所有虛擬用戶將無法訪問。

  31. # Uncomment this to enable any form of FTP write command.

  32. write_enable=YES
    設定可以進行寫操作。

  33. # 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
    anon_upload_enable=NO
    禁止匿名用戶上傳。
    #
    # 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=NO
    禁止匿名用戶建立目錄。
    #
    # Activate directory messages - messages given to remote users when they
    # go into a certain directory.
    dirmessage_enable=YES
    設定開啓目錄標語功能。
    #
    # Activate logging of uploads/downloads.
    xferlog_enable=YES
    設定開啓日誌記錄功能。
    #
    # Make sure PORT transfer connections originate from port 20 (ftp-data).
    connect_from_port_20=YES
    設定端口20進行數據連接。
    #
    # 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_uploads=NO
    設定禁止上傳文件更改宿主。
    #chown_username=whoever
    #
    # You may override where the log file goes if you like. The default is shown
    # below.
    xferlog_file=/var/log/vsftpd.log
    設定Vsftpd的服務日誌保存路徑。注意,該文件默認不存在。必須要手動touch出來,並且由於這裏更改了

  34. Vsftpd的服務宿主用戶爲手動建立的Vsftpd。必須注意給與該用戶對日誌的寫入權限,否則服務將啓動失敗。
    #
    # If you want, you can have your log file in standard ftpd xferlog format
    xferlog_std_format=YES
    設定日誌使用標準的記錄格式。
    #
    # You may change the default value for timing out an idle session.
    #idle_session_timeout=600
    設定空閒連接超時時間,這裏使用默認。將具體數值留給每個具體用戶具體指定,當然如果不指定的話,

  35. 還是使用這裏的默認值600,單位秒。
    #
    # You may change the default value for timing out a data connection.
    #data_connection_timeout=120
    設定單次最大連續傳輸時間,這裏使用默認。將具體數值留給每個具體用戶具體指定,當然如果不指定的話,

  36. 還是使用這裏的默認值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
    nopriv_user=vsftpd

  37. 設定支撐vsftpd服務的宿主用戶爲手動建立的Vsftpd用戶。注意,一旦做出更改宿主用戶後,必須注意

  38. 一起與該服務相關的讀寫文件的讀寫賦權問題。比如日誌文件就必須給與該用戶寫入權限等。

  39. (這裏就是我們前面建立的控制vsftp服務的用戶)
    #
    # 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
    設定支持ASCII模式的上傳和下載功能。
    #
    # You may fully customise the login banner string:
    ftpd_banner=This Vsftp server supports virtual users ^_^
    設定Vsftpd的登陸標語。
    #
    # 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_list_enable=YES

  40.  

  41. chroot_list_enable=YES   //開啓chroot_list_file設置的文件中的用戶名單

  42. chroot_local_user=YES  //禁止用戶登出自己的FTP主目錄
    chroot_list_file=/etc/vsftpd/chroot_list //此文件要手動建立,當上面兩項全部YES時,

  43. 文件中的用戶(一行一個)可以跳出自己的FTP主目錄,如果chroot_local_user=YES,

  44. chroot_list_enable=NO,則列表文件不生效,此時所有虛擬用戶將全部鎖定在自己FTP主目錄中;

  45. 如果chroot_local_user=NO並且chroot_list_enable=YES時,則chroot_list中的用戶

  46. 不可以跳出自己的FTP主目錄
    #
    # 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
    ls_recurse_enable=NO
    禁止用戶登陸FTP後使用"ls -R"的命令。該命令會對服務器性能造成巨大開銷。如果該項被允許,

  47. 那麼擋多用戶同時使用該命令時將會對該服務器造成威脅。
    # 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服務工作在StandAlone模式下。順便展開說明一下,所謂StandAlone模式就是該

  48. 服務擁有自己的守護進程支持,在ps -A命令下我們將可用看到vsftpd的守護進程名。如果不想

  49. 工作在StandAlone模式下,則可以選擇SuperDaemon模式,在該模式下 vsftpd將沒有自己的守護

  50. 進程,而是由超級守護進程Xinetd全權代理,與此同時,Vsftp服務的許多功能將得不到實現。
    #
    # This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6
    # sockets, you must run two copies of vsftpd whith two configuration files.
    # Make sure, that one of the listen options is commented !!
    #listen_ipv6=YESpam_service_name=vsftpd
    設定PAM服務下Vsftpd的驗證配置文件名。因此,PAM驗證將參考/etc/pam.d/下的vsftpd文件配置。

  51. userlist_enable=YES
    設定userlist_file中的用戶將不得使用FTP。

  52. tcp_wrappers=YES
    設定支持TCP Wrappers。#KC: The following entries are added for supporting virtual ftp users.
    以下這些是關於Vsftpd虛擬用戶支持的重要配置項目。默認Vsftpd.conf中不包含這些設定項目,

  53. 需要自己手動添加配置。

  54. guest_enable=YES
    設定啓用虛擬用戶功能。

  55. guest_username=virtualftp
    指定虛擬用戶的宿主用戶。(前面創建的用於虛擬用戶控制的宿主用戶)

  56. virtual_use_local_privs=YES
    設定虛擬用戶的權限符合他們的宿主用戶。

  57. user_config_dir=/etc/vsftpd/vconf
    設定虛擬用戶個人Vsftp的配置文件存放路徑。也就是說,這個被指定的目錄裏,將存放每個Vsftp虛擬

  58. 用戶個性的配置文件,一個需要注意的地方就是這些配置文件名必須和虛擬用戶名相同。
    -------------------------------------------------------------------------
    保存退出。

  59.  

  60. 3.建立Vsftpd的日誌文件,並更該屬主爲Vsftpd的服務宿主用戶:

  61. [root@www ~]# touch /var/log/vsftpd.log

  62. [root@www ~]# chown vsftpd.vsftpd /var/log/vsftpd.log

  63. ------------------------------------------------------------------------------
    4.建立虛擬用戶配置文件存放路徑:
    [root@www ~]# mkdir /etc/vsftpd/vconf/


  64. ##########建立虛擬用戶#############

  65. [root@www ~]# cat /etc/vsftpd/virtusers  (奇數行放用戶名,偶數行密碼,一行用戶名一行密碼)
    test
    123.com
    mac
    123456
     
    ----------------------------------------------------------------------------------------------

  66. 3.生成虛擬用戶數據文件:
    [root@www ~]# db_load -T -t hash -f /etc/vsftpd/virtusers /etc/vsftpd/virtusers.db

  67. ----------------------------------------------------------------------------------------------

  68. 4.察看生成的虛擬用戶數據文件
    [root@www ~]# ll /etc/vsftpd/virtusers.db
    -rw-r--r-- 1 root root 12288 Sep 16 03:51 /etc/vsftpd/virtusers.db

  69. 需要特別注意的是,以後再要添加虛擬用戶的時候,只需要按照“一行用戶名,一行口令”的格式將新用戶名和

  70. 口令添加進虛擬用戶名單文件。但是光這樣做還不夠,不會生效的哦!還要再執行一遍

  71. “ db_load -T -t hash -f 虛擬用戶名單文件 虛擬用戶數據庫文件.db ”的命令使其生效纔可以!

  72. ---------------------------------------------------------------------------------------------------------------------------

  73. 四.設定PAM驗證文件,並指定虛擬用戶數據庫文件進行讀取

  74. 在編輯前做好備份:
    [root@www ~]# cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.backup //.編輯Vsftpd的PAM驗證配置文件

  75. ----------------------------------------------------------------
    #%PAM-1.0

  76. [root@www ~]#  cat /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     /lib/security/pam_userdb.so     db=/etc/vsftpd/virtusers
    account    required     /lib/security/pam_userdb.so     db=/etc/vsftpd/virtusers

  77. (注意:如果是64位系統,這裏的庫目錄是/lib64)-------------->這裏要注意
    -------------------------------------------------------------------------------------------------------

  78. 五.虛擬用戶的配置
    1.規劃好虛擬用戶的主路徑:(以後文件就保存在此目錄下面)
    [root@www ~]#  mkdir /opt/vsftp/
     
    2.建立測試用戶的FTP用戶目錄:
    [root@www ~]# mkdir /opt/vsftp/test /opt/vsftp/mac

  79. -----------------------------------------------------------------------------

  80. 3.建立虛擬用戶配置文件模版:[root@www ~]# cp /etc/vsftpd/vsftpd.conf.backup /etc/vsftpd/vconf/vconf.tmp
     
    4.定製虛擬用戶模版配置文件:
    [root@www ~]# vim /etc/vsftpd/vconf/vconf.tmp
    --------------------------------
    local_root=/opt/vsftp/virtuser  #virtuser 表示你要更換的虛擬用戶名字,這裏定義成一個模板
    指定虛擬用戶的具體主路徑。
    anonymous_enable=NO
    設定不允許匿名用戶訪問。
    write_enable=YES
    設定允許寫操作。
    local_umask=022
    設定上傳文件權限掩碼。
    anon_upload_enable=NO
    設定不允許匿名用戶上傳。
    anon_mkdir_write_enable=NO
    設定不允許匿名用戶建立目錄。
    idle_session_timeout=600
    設定空閒連接超時時間。
    data_connection_timeout=120
    設定單次連續傳輸最大時間。
    max_clients=10
    設定併發客戶端訪問個數。
    max_per_ip=5
    設定單個客戶端的最大線程數,這個配置主要來照顧Flashget、迅雷等多線程下載軟件。
    local_max_rate=102400
    設定該用戶的最大傳輸速率,單位b/s。
    -----------------------------------------------------------------
    這裏將原vsftpd.conf配置文件經過簡化後保存作爲虛擬用戶配置文件的模版。這裏將並

  81. 不需要指定太多的配置內容,主要的框架和限制交由 Vsftpd的主配置文件vsftpd.conf

  82. 來定義,即虛擬用戶配置文件當中沒有提到的配置項目將參考主配置文件中的設定。而在

  83. 這裏作爲虛擬用戶的配置文件模版只需要留一些和用戶流量控制,訪問方式控制的配置項

  84. 目就可以了。這裏的關鍵項是local_root這個配置,用來指定這個虛擬用戶的FTP主路徑。
    5.更改虛擬用戶的主目錄的屬主爲虛擬宿主用戶:

  85. [root@www ~]# chown -R virtualftp.virtualftp /opt/vsftp/

  86. 六.給測試用戶定製:
    1.從虛擬用戶模版配置文件複製:
    [root@www ~]# cp /etc/vsftpd/vconf/vconf.tmp /etc/vsftpd/vconf/test
     
    2.針對具體用戶進行定製:
    [root@www ~]# vim /etc/vsftpd/vconf/test
    ---------------------------------
    local_root=/opt/vsftp/test
    anonymous_enable=NO
    write_enable=YES
    local_umask=022
    anon_upload_enable=NO
    anon_mkdir_write_enable=NO
    idle_session_timeout=300
    data_connection_timeout=90
    max_clients=1
    max_per_ip=1
    local_max_rate=204800
    ------------------------------------------------------------------------


  87. 七.啓動服務:
    [root@www ~]# service vsftpd restart


  88. 八.如果有問題 請檢查配置文件中YES、NO後面是否有空格
     
       2、關閉防火牆

  89.    3、關閉selinux

  90.  

  91. 測試過程是用firefox的一個FTP插件,非常好用,登錄不上可以彈出相應的錯誤提示及代碼,排錯很方便
    -----------------------------經過網上資料和自己實踐整理-----------------------------

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章