1. 程式人生 > >linux 搭建FTP伺服器詳細配置

linux 搭建FTP伺服器詳細配置

#rpm -qa | grep 'vsftpd' 檢視ftp模組是否已安裝,如沒有則使用yum install vsftpd進行安裝;同時安裝ftp介面進行ftp連線,yum install ftp
Vsftpd的配置檔案:

/etc/vsftpd/vsftpd.conf

主配置檔案

/usr/sbin/vsftpd

Vsftpd的主程式

/etc/rc.d/init.d/vsftpd

啟動指令碼

/etc/pam.d/vsftpd

PAM認證檔案(此檔案中file=/etc/vsftpd/ftpusers欄位,指明阻止訪問的使用者來自/etc/vsftpd/ftpusers檔案中的使用者)

/etc/vsftpd/ftpusers

禁止使用vsftpd的使用者列表檔案。記錄不允許訪問FTP伺服器的使用者名稱單,管理員可以把一些對系統安全有威脅的使用者賬號記錄在此檔案中,以免使用者從FTP登入後獲得大於上傳下載操作的權利,而對系統造成損壞。(注意:linux-4中此檔案在/etc/目錄下)

/etc/vsftpd/user_list

禁止或允許使用vsftpd的使用者列表檔案。這個檔案中指定的使用者預設情況(即在/etc/vsftpd/vsftpd.conf中設定userlist_deny=YES)下也不能訪問FTP伺服器,在設定了userlist_deny=NO

時,僅允許user_list中指定的使用者訪問FTP伺服器。(注意:linux-4中此檔案在/etc/目錄下)

/var/ftp

匿名使用者主目錄;本地使用者主目錄為:/home/使用者主目錄,即登入後進入自己家目錄

/var/ftp/pub

匿名使用者的下載目錄,此目錄需賦權根chmod 1777 pub1為特殊許可權,使上載後無法刪除)

/etc/logrotate.d/vsftpd.log

Vsftpd的日誌檔案

vsftpd的主配置檔案/etc/vsftpd/vsftpd.conf說明(修改前先備份):

# 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 (是否允許匿名登入FTP伺服器,預設設定為YES允許,即使用者可使用使用者名稱ftp或anonymous進行ftp登入,口令為使用者的E-mail地址。如不允許匿名訪問去掉前面#並設定為NO)
# Uncomment this to allow local users to log in.
local_enable=YES (是否允許本地使用者(即linux系統中的使用者帳號)登入FTP伺服器,預設設定為YES允許,本地使用者登入後會進入使用者主目錄,而匿名使用者登入後進入匿名使用者的下載目錄/var/ftp/pub;若只允許匿名使用者訪問,前面加上#,可阻止本地使用者訪問FTP伺服器。)
# Uncomment this to enable any form of FTP write command.
write_enable=YES  (是否允許本地使用者對FTP伺服器檔案具有寫許可權,預設設定為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 (或其它值,設定本地使用者的檔案掩碼為預設022,也可根據個人喜好將其設定為其他值,預設值為077)
# 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 (是否允許匿名使用者上傳檔案,須將write_enable=YES,預設設定為YES允許)
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES  (是否允許匿名使用者建立新資料夾,預設設定為YES允許)
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
#dirmessage_enable=YES (是否啟用目錄歡迎資訊功能,當用戶用CMD     模式首次訪問伺服器上某個目錄時,FTP伺服器將顯示歡迎資訊,預設情況下,歡迎資訊是通過該目錄下的.message檔案獲得的,此檔案儲存自定義的歡迎資訊,由使用者自己建立)
# Activate logging of uploads/downloads.
xferlog_enable=YES (預設值為NO如果啟用此選項,系統將會維護記錄伺服器上傳和下載情況的日誌檔案,預設情況該日誌檔案為/var/log/vsftpd.log,也可以通過下面的xferlog_file選項對其進行設定。)
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES (設定FTP伺服器將啟用FTP資料埠的連線請求,ftp-data資料傳輸,21為連線控制埠)
# 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!-注意,不推薦使用root使用者上傳檔案
#chown_uploads=YES(設定是否允許改變上傳檔案的屬主,與下面一個設定項配合使用)
#chown_username=whoeve(設定想要改變的上傳檔案的屬主,如果需要,則輸入一個系統使用者名稱,例如可以把上傳的檔案都改成root屬主。whoever:任何人)
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/vsftpd.log(設定系統維護記錄FTP伺服器上傳和下載情況的日誌檔案,/var/log/vsftpd.log是預設的,也可以另設其它)
# If you want, you can have your log file in standard ftpd xferlog format
#xferlog_std_format=YES(如果啟用此選項,傳輸日誌檔案將以標準xferlog的格式書寫,該格式的日誌檔案預設為/var/log/xferlog,也可以通過xferlog_file選項對其進行設定,預設值為NO)
#dual_log_enable(如果新增並啟用此選項,將生成兩個相似的日誌檔案,預設在/var/log/xferlog和/var/log/vsftpd.log目錄下。前者是wu_ftpd型別的傳輸日誌,可以利用標準日誌工具對其進行分析;後者是vsftpd型別的日誌)
#syslog_enable(如果新增並啟用此選項,則原本應該輸出到/var/log/vsftpd.log中的日誌,將輸出到系統日誌中)
# You may change the default value for timing out an idle session.
#idle_session_timeout=600 (設定資料傳輸中斷間隔時間,此語句表示空閒的使用者會話中斷時間為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 (執行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 client會下達“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 (大多數FTP伺服器都選擇用ASCII方式傳輸資料,將#去掉就能實現用ASCII方式上傳和下載檔案)
#ascii_download_enable=YES (將#去掉就能實現用ASCII方式下載檔案)
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service. (將#去掉可設定登入FTP伺服器時顯示的歡迎資訊,可以修改=後的歡迎資訊內容。另外如在需要設定更改目錄歡迎資訊的目錄下建立名為.message的檔案,並寫入歡迎資訊儲存後,在進入到此目錄會顯示自定義歡迎資訊)
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES(可將某些特殊的email address抵擋住。如果以anonymous登入伺服器時,會要求輸入密碼,也就是您的email address,如果很討厭某些email address,就可以使用此設定來取消他的登入許可權,但必須與下面的設定項配合)
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails(當上面的deny_email_enable=YES時,可以利用這個設定項來規定那個email address不可登入vsftpd伺服器,此檔案需使用者自己建立,一行一個email address即可!)
# 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(設定為NO時,使用者登入FTP伺服器後具有訪問自己目錄以外的其他檔案的許可權,設定為YES時,使用者被鎖定在自己的home目錄中,vsftpd將在下面chroot_list_file選項值的位置尋找chroot_list檔案,此檔案需使用者建立,再將需鎖定在自己home目錄的使用者列入其中,每行一個使用者)
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list(此檔案需自己建立,被列入此檔案的使用者,在登入後將不能切換到自己目錄以外的其他目錄,由FTP伺服器自動地chrooted到使用者自己的home目錄下,使得chroot_list檔案中的使用者不能隨意轉到其他使用者的FTP home目錄下,從而有利於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(是否允許遞迴查詢,大型站點的FTP伺服器啟用此項可以方便遠端使用者查詢
# 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(如果設定為YES,則vsftpd將以獨立模式執行,由vsftpd自己監聽和處理連線請求)
# 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=YES(設定是否支援IPV6)
#pam_service_name=vsftpd(設定PAM外掛模組提供的認證服務所使用的配置檔名,即/etc/pam.d/vsftpd檔案,此檔案中file=/etc/vsftpd/ftpusers欄位,說明了PAM模組能抵擋的帳號內容來自檔案/etc/vsftpd/ftpusers中)
#userlist_enable=YES/NO(此選項預設值為NO ,此時ftpusers檔案中的使用者禁止登入FTP伺服器;若此項設為YES,則user_list檔案中的使用者允許登入   FTP伺服器,而如果同時設定了userlist_deny=YES,則user_list檔案中的使用者將不允許登入FTP伺服器,甚至連輸入密碼提示資訊都沒有,直接被FTP伺服器拒絕)
#userlist_deny=YES/NO(此項預設為YES,設定是否阻扯user_list檔案中的使用者登入FTP伺服器)
tcp_wrappers=YES (表明伺服器使用tcp_wrappers作為主機訪問控制方式,tcp_wrappers可以實現linux系統中網路服務的基於主機地址的訪問控制,在/etc目錄中的hosts.allow和hosts.deny兩個檔案用於設定tcp_wrappers的訪問控制,前者設定允許訪問記錄,後者設定拒絕訪問記錄。例如想限制某些主機對FTP伺服器192.168.57.2的匿名訪問,編緝/etc/hosts.allow檔案,如在下面增加兩行命令:vsftpd:192.168.57.1:DENYvsftpd:192.168.57.9:DENY表明限制IP為192.168.57.1/192.168.57.9主機訪問IP為192.168.57.2的FTP伺服器,此時FTP伺服器雖可以PING通,但無法連線)
在FTP伺服器的管理中無論對本地使用者還是匿名使用者,對於FTP伺服器資源的使用都需要進行控控制,避免由於負擔過大造成FTP伺服器執行異常,可以新增以下配置項對FTP客戶機使用FTP伺服器資源進行控制:
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.
ano n_max_rate設定項用於設定匿名使用者的最大傳輸速率,單位為B/s,值為0表示不限制。例如ano_max_rate=200000,表示FTP伺服器的匿名使用者最大傳輸速率設定為200KB/s.
vsftpd.user_list檔案需要與vsftpd.conf檔案中的配置項結合來實現對於vsftpd.user_list檔案中指定使用者賬號的訪問控制:
(1)設定禁止登入的使用者賬號
當vsftpd.conf配置檔案中包括以下設定時,vsftpd.user_list檔案中的使用者賬號被禁止進行FTP登入:
userlist_enable=YES
userlist_deny=YES
userlist_enable設定項設定使用vsftpd.user_list檔案,userlist_deny設定為YES表示vsftpd.user_list檔案用於設定禁止的使用者賬號。
(2)設定只允許登入的使用者賬號
當vsftpd.conf配置檔案中包括以下設定時,只有vsftpd.user_list檔案中的使用者賬號能夠進行FTP登入:
userlist_enable=YES
userlist_deny=NO
userlist_enable設定項設定使用vsftpd.user_list檔案,userlist _deny設定為NO表示vsftpd.usre_list檔案用於設定只允許登入的使用者賬號,檔案中未包括的使用者賬號被禁止FTP登入。
userlist_denyuserlist_enable選項限制使用者登入FTP伺服器(使用userlist_deny選項和user_list檔案一起能有效阻止root,apache,www等系統使用者登入FTP伺服器,從而保證FTP伺服器的分級安全性)

Userlist_enable=YES

Ftpusers中使用者允許訪問

User_list中使用者允許訪問

Userlist_enable=NO

Ftpusers中使用者禁止訪問

User_list中使用者允許訪問

Userlist_deny=YES

Ftpusers中使用者禁止訪問(登入時可以看到密碼輸入提示,但仍無法訪問)

user_list 中使用者禁止訪問

Userlist_deny=NO

ftpusers中使用者禁止訪問

user_list中使用者允許訪問

Userlist_enable=YES 並且

Userlist_deny=YES

Ftpusers中使用者禁止訪問

User_list中使用者禁止訪問(登入時不會出現密碼提示,直接被伺服器拒絕)

Userlist_enable=YES 並且

Userlist_deny=NO

Ftpusers中使用者禁止訪問

User_list中使用者允許訪問
配置FTP伺服器的虛擬使用者
在vsftpd伺服器中支援匿名使用者,本地使用者,和虛擬使用者3類使用者賬號,用途及區別如下:
匿名使用者是名為anonymous或ftp的FTP使用者,匿名FTP使用者登入後將FTP伺服器中的/var/ftp作為FTP根目錄。匿名使用者通常用於提供公共檔案的下載,如架設公共軟體下載的FTP伺服器,所有人都可以使用匿名使用者進行軟體下載。
本地使用者賬號是FTP伺服器中的系統使用者賬號,使用FTP本地使用者賬號登入FTP伺服器後,登入目錄為本地使用者的宿主目錄。本地FTP使用者賬號通常和Web伺服器一起提供虛擬主機服務,作為網頁虛擬主機更新網頁的途徑。
虛擬使用者賬號是為了保證FTP伺服器的安全性,由vsftpd伺服器提供的非系統使用者賬號。虛擬使用者FTP登入後將把指定的目錄作為FTP根目錄。虛擬使用者與本地使用者具有類似的功能,由於虛擬使用者相對安全,因此正逐步替代本地使用者賬號。
由於虛擬使用者賬號具有較高的安全性,可以替代本地使用者賬號使用,下面是vsftpd虛擬使用者賬號設定的幾個步驟:
以設定miket 和 john 兩個虛擬使用者帳號為例來配置vsftpd伺服器
(1)建立虛擬使用者口令庫檔案
建立虛擬使用者的口令檔案,檔案中奇數行設定虛擬使用者的使用者名稱,偶數行設定使用者的口令。例如,使用vi編輯器建立名為logins.txt的使用者口令庫檔案,在檔案中設定使用者mike的口令為pwabcd,使用者john 的口令是pw1234:
#vi logins.txt
mike      (奇數行設定虛擬使用者名稱)
pw