Linux環境下vsftpd參數配置

好久以前就用過vsftpd,但老是忘了參數該如何配置,今天特意有搜索了一遍,把配置方法整理以下:html

(1)檢查是否已安裝vsftpd數據庫

#rpm -qa | grep vsftpd
vsftpd-2.0.5-28.el5

(2)新建一個FTP用戶安全

#mkdir /home/ftpuser            // 首先爲ftpuser用戶建立一個用戶目錄
#adduser -d /home/ftpuser -g ftp -s /sbin/nologin ftpuser  // -s /sbin/nologin表示讓其不能登錄系統,-d指定用戶目錄爲/home/ftpuser,也就是默認訪問的目錄 #passwd ftpuser           // 接下來爲ftpuser用戶設置密碼
Changing password
for user beinan.
New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully.

(3)修改vsftpd的配置文件,使ftpuser不能訪問用戶目錄的上層路徑(默認狀況下是能夠訪問用戶目錄的上層目錄的服務器

#vi /etc/vsftpd/vsftpd.conf
// 去掉如下兩行的註釋符# chroot_local_user=YES
chroot_list_enable=YES  # 本行可選 chroot_list_file=/etc/vsftpd/chroot_list // 同時添加一個配置文件: #vi /etc/vsftpd/chroot_list // 將要設定權限的用戶加進去(只有寫進去的用戶名纔會擁有切換到上層目錄的權限,其實不須要這個功能不該該寫,留空便可): ftpuser

備註:當chroot_local_user=YES 時,chroot_list_file文件中寫入的用戶(一個一行)表示擁有切換(根路徑)上級目錄的權限,沒寫進去的用戶則不具有切換(根路徑)上層目錄的權限;反之,當chroot_local_user=NO(默認) 時,全部用戶默認均可以訪問用戶目錄的上層路徑,只有寫入到chroot_list_file文件中的用戶(一個一行)才禁止切換到上層路徑。session

心得:上面的這個兩種狀況設定老是很差記,每次理解的都不是很透徹老是忘了。能夠這麼理解:chroot_local_user是總開關,能夠是YES(禁止)本地帳戶切換上層目錄,或者NO(默認,容許)本地帳戶切換上層目錄。但凡事總有例外吧,那麼這個chroot_list_file 就是設定例外狀況的。也就是與chroot_local_user 設定的含義相反。socket

(4)設定用戶權限async

在vsftpd 2.3.5以後,vsftpd加強了安全檢查,若是用戶被限定在了其主目錄下,則該用戶的主目錄不能再具備寫權限了!若是檢查發現還有寫權限,就會報以下錯誤:ide

C:\Users\liuheng.klh>ftp 47.88.58.168
鏈接到 47.88.58.168220 Welcome to Victo's FTP service.
用戶(47.88.58.168:(none)): ftpuser
331 Please specify the password.
密碼:
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
500 OOPS: priv_sock_get_cmd
遠程主機關閉鏈接。

 須要作兩步設定:工具

<1> 在配置文件 /etc/vsftpd.conf 文件末尾添加一行: allow_writeable_chroot=YESui

<2> 注意新增用戶的主目錄要有寫入權限(直接更改目錄所屬關係就能夠): chown ftpuser:ftp /home/ftpuser 

(5)從新啓動vsftpd服務

#service vsftpd restart

(6)開啓防火牆

不論是IPTables仍是阿里控制檯的安全策略,都須要新增防火牆端口控制,分紅兩步驟:

<1> 設定vsftpd被動端口範圍,在vsftpd.conf配置文件中末尾新增兩行:pasv_min_port=61001  和  pasv_max_port=62000

<2> 開啓以下端口控制:20 - 2161001 - 62000

 (7)附加一下配置示例

  1 # Example config file /etc/vsftpd.conf
  2 #
  3 # The default compiled in settings are fairly paranoid. This sample file
  4 # loosens things up a bit, to make the ftp daemon more usable.
  5 # Please see vsftpd.conf.5 for all compiled in defaults.
  6 #
  7 # READ THIS: This example file is NOT an exhaustive list of vsftpd options.
  8 # Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
  9 # capabilities.
 10 #
 11 #
 12 # Run standalone?  vsftpd can run either from an inetd or as a standalone
 13 # daemon started from an initscript.
 14 listen=YES
 15 #
 16 # Run standalone with IPv6?
 17 # Like the listen parameter, except vsftpd will listen on an IPv6 socket
 18 # instead of an IPv4 one. This parameter and the listen parameter are mutually
 19 # exclusive.
 20 #listen_ipv6=YES
 21 #
 22 # Allow anonymous FTP? (Disabled by default)
 23 anonymous_enable=NO
 24 #
 25 # Uncomment this to allow local users to log in.
 26 local_enable=YES
 27 #
 28 # Uncomment this to enable any form of FTP write command.
 29 write_enable=YES
 30 #
 31 # Default umask for local users is 077. You may wish to change this to 022,
 32 # if your users expect that (022 is used by most other ftpd's)
 33 local_umask=000
 34 #
 35 # Uncomment this to allow the anonymous FTP user to upload files. This only
 36 # has an effect if the above global write enable is activated. Also, you will
 37 # obviously need to create a directory writable by the FTP user.
 38 #anon_upload_enable=YES
 39 #
 40 # Uncomment this if you want the anonymous FTP user to be able to create
 41 # new directories.
 42 #anon_mkdir_write_enable=YES
 43 #
 44 # Activate directory messages - messages given to remote users when they
 45 # go into a certain directory.
 46 dirmessage_enable=YES
 47 #
 48 # If enabled, vsftpd will display directory listings with the time
 49 # in  your  local  time  zone.  The default is to display GMT. The
 50 # times returned by the MDTM FTP command are also affected by this
 51 # option.
 52 use_localtime=YES
 53 #
 54 # Activate logging of uploads/downloads.
 55 xferlog_enable=YES
 56 #
 57 # Make sure PORT transfer connections originate from port 20 (ftp-data).
 58 connect_from_port_20=YES
 59 #
 60 # If you want, you can arrange for uploaded anonymous files to be owned by
 61 # a different user. Note! Using "root" for uploaded files is not
 62 # recommended!
 63 #chown_uploads=YES
 64 #chown_username=whoever
 65 #
 66 # You may override where the log file goes if you like. The default is shown
 67 # below.
 68 xferlog_file=/var/log/vsftpd.log
 69 #
 70 # If you want, you can have your log file in standard ftpd xferlog format.
 71 # Note that the default log file location is /var/log/xferlog in this case.
 72 #xferlog_std_format=YES
 73 #
 74 # You may change the default value for timing out an idle session.
 75 #idle_session_timeout=600
 76 #
 77 # You may change the default value for timing out a data connection.
 78 #data_connection_timeout=120
 79 #
 80 # It is recommended that you define on your system a unique user which the
 81 # ftp server can use as a totally isolated and unprivileged user.
 82 #nopriv_user=ftpsecure
 83 #
 84 # Enable this and the server will recognise asynchronous ABOR requests. Not
 85 # recommended for security (the code is non-trivial). Not enabling it,
 86 # however, may confuse older FTP clients.
 87 #async_abor_enable=YES
 88 #
 89 # By default the server will pretend to allow ASCII mode but in fact ignore
 90 # the request. Turn on the below options to have the server actually do ASCII
 91 # mangling on files when in ASCII mode.
 92 # Beware that on some FTP servers, ASCII support allows a denial of service
 93 # attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
 94 # predicted this attack and has always been safe, reporting the size of the
 95 # raw file.
 96 # ASCII mangling is a horrible feature of the protocol.
 97 #ascii_upload_enable=YES
 98 #ascii_download_enable=YES
 99 #
100 # You may fully customise the login banner string:
101 ftpd_banner=Welcome to Victo's FTP service.
102 #
103 # You may specify a file of disallowed anonymous e-mail addresses. Apparently
104 # useful for combatting certain DoS attacks.
105 #deny_email_enable=YES
106 # (default follows)
107 #banned_email_file=/etc/vsftpd.banned_emails
108 #
109 # You may restrict local users to their home directories.  See the FAQ for
110 # the possible risks in this before using chroot_local_user or
111 # chroot_list_enable below.
112 chroot_local_user=YES
113 #
114 # You may specify an explicit list of local users to chroot() to their home
115 # directory. If chroot_local_user is YES, then this list becomes a list of
116 # users to NOT chroot().
117 # (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
118 # the user does not have write access to the top level directory within the
119 # chroot)
120 chroot_local_user=YES
121 chroot_list_enable=YES
122 # (default follows)
123 chroot_list_file=/etc/vsftpd.chroot_list
124 #
125 # You may activate the "-R" option to the builtin ls. This is disabled by
126 # default to avoid remote users being able to cause excessive I/O on large
127 # sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
128 # the presence of the "-R" option, so there is a strong case for enabling it.
129 #ls_recurse_enable=YES
130 #
131 # Customization
132 #
133 # Some of vsftpd's settings don't fit the filesystem layout by
134 # default.
135 #
136 # This option should be the name of a directory which is empty.  Also, the
137 # directory should not be writable by the ftp user. This directory is used
138 # as a secure chroot() jail at times vsftpd does not require filesystem
139 # access.
140 secure_chroot_dir=/var/run/vsftpd/empty
141 #
142 # This string is the name of the PAM service vsftpd will use.
143 pam_service_name=vsftpd
144 #
145 # This option specifies the location of the RSA certificate to use for SSL
146 # encrypted connections.
147 rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
148 # This option specifies the location of the RSA key to use for SSL
149 # encrypted connections.
150 rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
151 
152 # Add by KLH, 20181024 14:48
153 pasv_min_port=61001
154 pasv_max_port=62000
155 
156 allow_writeable_chroot=YES
View Code

 

 

下面將經常使用的vsftpd.conf文件配置參數進行說明:

1. 匿名服務器的鏈接(獨立的服務器)

在/etc/vsftpd/vsftpd.conf配置文件中添加以下幾項:

Anonymous_enable=yes    (容許匿名登錄) 
Dirmessage_enable=yes    (切換目錄時,顯示目錄下.message的內容) 
Local_umask=022             (FTP上本地的文件權限,默認是077)
Connect_form_port_20=yes     (啓用FTP數據端口的數據鏈接)* 
Xferlog_enable=yes          (激活上傳和下傳的日誌) 
Xferlog_std_format=yes    (使用標準的日誌格式))
Ftpd_banner=XXXXX        (歡迎信息) 
Pam_service_name=vsftpd (驗證方式)* 
Listen=yes         (獨立的VSFTPD服務器)* 

功能:只能鏈接FTP服務器,不能上傳和下傳

注:其中全部和日誌歡迎信息相關連的都是可選項,打了星號的不管什麼賬戶都要添加,是屬於FTP的基本選項

2. 開啓匿名FTP服務器上傳權限

在配置文件中添加如下的信息便可:

Anon_upload_enable=yes     (開放上傳權限)
Anon_mkdir_write_enable=yes (可建立目錄的同時能夠在此目錄中上傳文件)
Write_enable=yes             (開放本地用戶寫的權限)
Anon_other_write_enable=yes  (匿名賬號能夠有刪除的權限)

3. 開啓匿名服務器下傳的權限

在配置文件中添加以下信息便可:

Anon_world_readable_only=no 

注:要注意文件夾的屬性,匿名賬戶是其它(other)用戶要開啓它的讀寫執行的權限 :(R)讀—–下傳 (W)寫—-上傳 (X)執行——若是不開FTP的目錄都進不去

4.普通用戶FTP服務器的鏈接(獨立服務器)

在配置文件中添加以下信息便可:

Local_enble=yes (本地賬戶可以登錄)
Write_enable=no (本地賬戶登錄後無權刪除和修改文件)

注:能夠用本地賬戶登錄vsftpd服務器,有下載上傳的權限 注:在禁止匿名登錄的信息後匿名服務器照樣能夠登錄但不能夠上傳下傳

5. 限定用戶進入其主目錄之外的目錄

設置全部的本地用戶都執行:

chroot chroot_local_user=yes (本地全部賬戶都只能在自家目錄) 

設置指定用戶執行

chroot chroot_list_enable=yes (chroot_list_file文件中的名單能夠調用) 
chroot_list_file= /etc/vsftpd/chroot_list  (該文件中指定須要限定的用戶名,該文件能夠放在任意其餘路徑中)

注:chroot_list 默認是不存在的,須要本身手動添加,在該文件中加入想要控制權限的用戶名便可。

特別說明:當chroot_local_user=yes時,chroot_list_file中指定的用戶將不會限定在用戶目錄,其餘用戶都被限定了;只有當chroot_local_user=no時,chroot_list_file中指定的用戶纔是禁止訪問用戶目錄之外的路徑。

6. 限制本地用戶訪問FTP

Userlist_enable=yes(用userlistlai 來限制用戶訪問)
Userlist_deny=no    (名單中的人不容許訪問)
Userlist_file=/指定文件存放的路徑/ (文件放置的路徑) 

注:開啓userlist_enable=yes匿名賬號不能登錄

7. 一些安全選項

Idle_session_timeout=600    (單位:秒,用戶會話空閒後10分鐘) 
Data_connection_timeout=120 (單位:秒,將數據鏈接空閒2分鐘斷)
Accept_timeout=60 (單位:秒,將客戶端空閒1分鐘後斷)
Connect_timeout=60 (單位:秒,中斷1分鐘後又從新鏈接)
Local_max_rate=50000 (單位:bit,本地用戶傳輸率50K)
Anon_max_rate=30000 (單位:bit,匿名用戶傳輸率30K)
Pasv_min_port=50000 Pasv_max_port=60000 (將客戶端的數據鏈接端口改在50000—60000之間) Max_clients=200 (FTP的最大鏈接數) Max_per_ip=4 (每IP的最大鏈接數) Listen_port=5555 (從5555端口進行數據鏈接)

8. 新建FTP用戶,並指定用戶的默認FTP目錄(不經過指定用戶目錄的方式)

建立FTP用戶,限定其登錄操做系統:

#useradd -s /sbin/nologin -g ftp newUser
#passwd newUser

添加用戶配置文件目錄:

#vi /etc/vsftpd/vsftp.conf
// 指定用戶配置信息路徑:
user_config_dir=/var/ftp
// 保存退出

建立該用戶的FTP根目錄:

#mkdir -p /var/www/bbs/

指定newUser用戶的FTP根目錄:

#vi /var/ftp/boy
// 添加以下
local_root=/var/www/bbs/

#chmod 777 /var/www/bbs/

重啓vsftpd服務便可。

9. 查看誰登錄了FTP,並殺死它的進程

ps –xf |grep ftp

kill 進程號

10. 開啓虛擬帳戶

首先要知道什麼是虛擬帳戶:虛擬帳戶是FTP服務提供的專用帳戶,它不屬於操做系統的帳戶,而只可以登陸FTP服務器。全部的虛擬帳戶都對應着同一個操做系統帳戶,也就是說全部虛擬帳戶的權限都受到該操做系統帳戶的權限限制。下面來看一下增長FTP虛擬帳戶的步驟:

10.1 在操做系統中添加一個FTP虛擬帳戶對應的本地帳戶(因爲是FTP虛擬帳戶,因此對應的本地帳戶不該該指定較高權限,-s /sbin/nologin表示不容許登錄操做系統)

#useradd -d /home/vftpuser -s /sbin/nologin vftpuser

10.2 編寫一個帳戶信息文件,並生成對應的帳戶信息數據庫

#vi /etc/vsftpd/vftpuser.txt
// 添加如下帳戶信息(奇數行爲帳戶名稱,偶數行爲對應的密碼):
user1
password1
user2
password2

// 利用db_load生成帳戶信息數據庫
#db_load -T -t hash -f /etc/vsftpd/vftpuser.txt /etc/vsftpd/vftpuser.db

 注意:若是要添加更多的帳戶,在vftpuser.txt文件中添加新用戶名和密碼,而後從新調用db_load生成vftpuser.db便可。

10.3 開啓PAM帳戶認證

#vi /etc/pam.d/vsftpd
// 將裏面其餘的都註釋掉,寫入下面這兩行:
auth sufficient /lib/security/pam_userdb.so db=/etc/vsftpd/vftpuser
account sufficient /lib/security/pam_userdb.so db=/etc/vsftpd/vftpuser

 注意:上面紅色的關鍵字若是是required,則會形成本地帳戶沒法同時登錄。

10.4 開啓vsftpd服務對虛擬帳戶的支持

#vi /etc/vsftpd/vsftpd.conf
anonymous_enable=NO       // 關閉匿名用戶【該項已存在,需修改】
guest_enable=YES        // 開啓虛擬用戶  
guest_username=vftpuser    // FTP虛擬用戶對應的系統用戶
pam_service_name=vsftpd    // PAM認證文件
user_config_dir=/etc/vsftpd/vsftpd_config_dir     // 用戶配置文件的目錄

10.5 給FTP虛擬帳戶指定參數,每個帳戶都用一個同名文件來指定權限(下面以user1用戶爲例)

vi /etc/vsftpd/vsftpd_config_dir/user1
// 指定如下參數,僅針對user1用戶有效:
local_root=/data/ftproot/user1 // 該用戶的主目錄
anon_world_readable_only=NO    // 下載權限
anon_upload_enable=YES         // 上傳權限
anon_mkdir_write_enable=YES    // 建立目錄的權限
anon_other_write_enable=NO     // 刪除和重命名的權限

10.6 從新啓動vsftpd服務使配置生效

#service vsftpd restart

 

=============================================================================================

小問題聚集:

<1>vsftpd鏈接速度很是慢,須要等上十幾秒,這是爲何?

解答:緣由是vsftp在登錄時作了DNS檢查來確認域名,解決辦法有兩個:一、在resolv.conf文件中刪除localhost或者127.0.0.1這樣的地址;二、在vsftpd.conf文件中加入:reverse_lookup_enable=NO。

<2>如何指定特定用戶的操做權限?

解答:在vsftp的配置文件/etc/vsftpd.conf裏頭添加這麼一行:user_config_dir=/etc/vsftpd_user_conf,而後在/etc/vsftpd_user_conf目錄裏頭建立和用戶名同名的文件,好比ftpuser,添加以下內容:

local_root=/vsr/www/html        // 指定用戶目錄
// 指定用戶的操做權限(這裏表示可上傳下載,刪除,建立文件夾等) 
cmds_allowed=ABOR,CWD,LIST,DELE,RMD,MDTM,MKD,NLST,PASS,PASV,PORT,PWD,QUIT,REST,RETR,RNFR,RNTO,SIZE,STOR,TYPE,USER,ACCT,APPE,CDUP,HELP,MODE,NOOP,REIN,STAT,STOU,STRU,SYST

注意,用戶不能上傳和刪除極有多是文件夾操做權限的問題,經過chmod能夠解決。 

<3>如何開啓FTP操做日誌功能?

 

解答:在vsftp的配置文件/etc/vsftp.conf裏頭添加以下內容,已有的進行修改便可:

xferlog_enable=YES             // FTP服務器記錄上傳下載狀況
xferlog_std_format=YES        // 表示將記錄的上傳下載狀況寫在xferlog_file所指定的文件中,即/var/log/xferlog文件
xferlog_file=/var/log/xferlog   // 指定日誌記錄文件(該文件能夠利用標準日誌工具進行分析)
dual_log_enable=YES           // 開啓雙份日誌記錄功能,在用xferlog文件記錄服務器上傳下載狀況的同時,vsftpd_log_file所指定的文件(即/var/log/vsftpd.log),也將用來記錄服務器的傳輸狀況
vsftpd_log_file=/var/log/vsftpd.log      // vsftpd格式的日誌文件
syslog_enable=NO              // 將vsftpd日誌輸出到系統日誌中
相關文章
相關標籤/搜索