### 安裝
1. 安裝Vsftpd服務相關部件
[root@butbueatiful ~]# yum install -y vsftpd*
2. 確認安裝PAM服務相關部件
[root@butbueatiful ~]# yum install -y pam*
開發包,其實不裝也沒有關係,主要的目的是確認PAM
3. 安裝DB4部件包
這裏要特別安裝一個`db4`的包,用來支持文件數據庫
[root@butbueatiful ~]# yum install -y db4*
### 系統賬戶
1. 創建Vsftpd服務的宿主用戶
[root@butbueatiful ~]# useradd ftp -d /var/ftp -s /sbin/nologin # ubuntu: -d /srv/ftp
默認的Vsftpd的服務宿主用戶是root,可是這不符合安全性的須要。
這裏創建名字爲ftp的用戶,用他來做爲支持Vsftpd的服務宿主用戶。
因爲該用戶僅用來支持Vsftpd服務用,所以沒有許可他登錄系統的必要,
並設定他爲不能登錄系統的用戶。
2. 創建Vsftpd虛擬宿主用戶
[root@butbueatiful ~]# useradd ftpvirtuser -d /var/ftpvirtuser -s /sbin/nologin # ubuntu: -d /srv/ftpvirtuser
本篇主要是介紹 vsftp 的虛擬用戶,虛擬用戶並非系統用戶,也就是說這些FTP的用戶在系統中是不存在的。
他們的整體權限實際上是集中寄託在一個在系統中的某一個用戶身上的,所謂 vsftpd 的虛擬宿主用戶,
就是這樣一個支持着全部虛擬用戶的宿主用戶。因爲他支撐了 FTP
的全部虛擬的用戶,ftp的每一個虛擬用戶在該目錄下(也就是上面的`/var/ftpvirtuser`)用相關文件名對應相關文件的主目錄
那麼他自己的權限將會影響着這些虛擬的用戶,所以,處於安全性的考慮,也要非分注意對該用戶的權限的控制,
該用戶也絕對沒有登錄系統的必要,這裏也設定他爲不能登錄系統的用戶。
使用 -d 參數指定用戶的主目錄,用戶主目錄並非必須存在的。若是沒有存在指定的目錄的話,那麼它將不會被創建。
### 調整 vsftpd 的配置文件
1. 編輯配置文件前先備份
[root@butbueatiful ~]# cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak
2. 編輯主配置文件vsftpd.conf
[root@butbueatiful ~]# vim /etc/vsftpd/vsftpd.conf
關於這個配置文件的更多更能能夠參考 "鳥哥的私房菜-服務器架設"
```
# 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.
# 設定該vsftpd服務工做在 StandAlone 模式下。
# 順便展開說明一下,所謂 StandAlone 模式就是該服務擁有本身的守護進程支持,
# 在ps -A命令下咱們將可用看到vsftpd的守護進程名。
# 若是不想工做在StandAlone模式下,則能夠選擇SuperDaemon模式,
# 在該模式下 vsftpd 將沒有本身的守護進程,而是由超級守護進程 xinetd 全權代理,
# 與此同時,vsftp服務的許多功能將得不到實現。
listen=YES
# 只監聽來訪問192.168.146.128(適用本機多網卡、多IP的狀況)的FTP服務請求
listen_address=192.168.146.128
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
# 設定不容許匿名訪問
anonymous_enable=NO
# Uncomment this to allow local users to log in.
# 設定本地用戶能夠訪問。
# 注意:主要是爲虛擬宿主用戶,若是該項目設定爲NO那麼全部虛擬用戶將沒法訪問
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
# 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=NO
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
# 禁止匿名用戶創建目錄
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.
# 設定開啓 wu-ftp 服務器格式記錄功能
#xferlog_enable=YES
#xferlog_file=/var/log/xferlog
# You may override where the log file goes if you like. The default is shown below.
# 設定 vsftpd 的服務日誌保存路徑。
# 注意,該文件默認不存在。必需要手動touch出來,而且因爲這裏更改了vsftpd的服務宿主用戶爲手動創建的vsftpd。
# 必須注意給與該用戶對日誌的寫入權限,不然服務將啓動失敗。
dual_log_enable=YES
vsftpd_log_file=/var/log/vsftpd.log
# Make sure PORT transfer connections originate from port 20 (ftp-data).
# 設定端口20進行數據鏈接
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=NO
#chown_username=whoever
# 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.
# 設定空閒鏈接超時時間,這裏使用默認。
# 將具體數值留給每一個具體用戶具體指定,固然若是不指定的話,仍是使用這裏的默認值600,單位秒。
#idle_session_timeout=600
# You may change the default value for timing out a data connection.
# 設定單次最大連續傳輸時間,這裏使用默認。
# 將具體數值留給每一個具體用戶具體指定,固然若是不指定的話,仍是使用這裏的默認值120,單位秒。
#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.
# 設定支撐vsftpd服務的宿主用戶爲手動創建的vsftpd用戶。
# 注意,一旦作出更改宿主用戶後,必須注意一塊兒與該服務相關的讀寫文件的讀寫賦權問題。
# 好比日誌文件就必須給與該用戶寫入權限等。
nopriv_user=ftp
# 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模式的上傳和下載功能。
ascii_upload_enable=YES
ascii_download_enable=YES
# You may fully customise the login banner string:
# 設定ftp的登錄標語。
ftpd_banner=Welcome to login the FTP server ^_^
#
# 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 的使用者帳號(若是 chroot_local_user=NO 目的相反)
chroot_list_enable=YES
# 不被 chroot 的使用者帳號的列表文件
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.
# 禁止用戶登錄FTP後使用"ls -R"的命令。該命令會對服務器性能形成巨大開銷。
# 若是該項被容許,那麼當多用戶同時使用該命令時將會對該服務器形成威脅。
ls_recurse_enable=NO
# 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 !!
# 設定PAM服務下vsftpd的驗證配置文件名。
# 所以,PAM驗證將參考/etc/pam.d/下的vsftpd文件配置。
#listen_ipv6=YES
pam_service_name=vsftpd
pam_service_name=/etc/pam.d/vsftpd
# 設定userlist_file中的用戶將不得使用FTP。
userlist_enable=YES
userlist_deny=YES
userlist_file=/etc/vsftpd/user_list
# 設定支持TCP Wrappers。#KC: The following entries are added for supporting virtual ftp users.
tcp_wrappers=YES
# 如下這些是關於vsftpd虛擬用戶支持的重要配置項目。
# 默認 vsftpd.conf 中不包含這些設定項目,須要本身手動添加配置。
# 設定啓用虛擬用戶功能。
guest_enable=YES
# 指定虛擬用戶的宿主用戶。
guest_username=ftpvirtuser
# 設定虛擬用戶的權限符合他們的宿主用戶。
virtual_use_local_privs=YES
# 設定虛擬用戶我的vsftp的配置文件存放路徑。
# 也就是說,這個被指定的目錄裏,將存放每一個Vsftp虛擬用戶個性的配置文件,
# 一個須要注意的地方就是這些配置文件名必須和虛擬用戶名相同。
user_config_dir=/etc/vsftpd/virtuser_conf
```
保存退出
3. 創建Vsftpd的日誌文件,並更該屬主爲Vsftpd的服務宿主用戶
[root@butbueatiful ~]# touch /var/log/vsftpd.log
[root@butbueatiful ~]# chown ftp.ftp /var/log/vsftpd.log
4. 創建虛擬用戶配置文件存放路徑
[root@butbueatiful ~]# mkdir /etc/vsftpd/virtuser_conf/
### 製做虛擬用戶數據庫文件
1. 先創建虛擬用戶名單文件
[root@butbueatiful ~]# touch /etc/vsftpd/virtusers
創建了一個虛擬用戶名單文件,這個文件就是來記錄vsftpd虛擬用戶的用戶名和口令的數據文件,
我這裏給它命名爲virtusers。爲了不文件的混亂,我把這個名單文件就放置在/etc/vsftpd/下。
2. 編輯虛擬用戶名單文件
[root@butbueatiful ~]# vim /etc/vsftpd/virtusers
```
teacher
t
student
s
```
編輯這個虛擬用戶名單文件,在其中加入用戶的用戶名和口令信息。格式很簡單:一行用戶名,一行口令。
3. 生成虛擬用戶數據文件
[root@butbueatiful ~]# db_load -T -t hash -f /etc/vsftpd/virtusers /etc/vsftpd/virtusers.db
這裏我順便把這個命令簡單說明一下
查看 db4 的`db_load`命令使用方法:
[root@butbueatiful vsftpd]# db_load
解釋在本篇中,`db_load`命令幾個相關選項很參數-T
選項 -T 容許應用程序可以將文本文件轉譯載入進數據庫。
因爲咱們以後是將虛擬用戶的信息以文件方式存儲在文件裏的,
爲了讓 vsftpd 這個應用程序可以經過文原本載入用戶數據,必需要使用這個選項。
若是指定了選項 -T,那麼必定要追跟子選項 -t
子選項 -t,追加在在 -T 選項後,用來指定轉譯載入的數據庫類型。
擴展介紹下,-t 能夠指定的數據類型有 Btree、Hash、Queue和Recon 數據庫。
這裏,接下來咱們須要指定的是 Hash 型。
4. 查看生成的虛擬用戶數據文件
[root@butbueatiful ~]# ll /etc/vsftpd/virtusers.db
-rw-r--r-- 1 root root 12288 Apr 12 15:33 /etc/vsftpd/virtusers.db
須要特別注意的是,之後再要添加虛擬用戶的時候,只須要按照 "一行用戶名,一行口令"
的格式將新用戶名和口令添加進虛擬用戶名單文件。可是光這樣作還不夠,配置不會生效的!
還要再執行一遍`db_load -T -t hash -f 虛擬用戶名單文件 虛擬用戶數據庫文件.db`的命令使其生效才能夠!
### 設定PAM驗證文件 並指定虛擬用戶數據庫文件進行讀取
1. 察看原來的Vsftp的PAM驗證配置文件
[root@butbueatiful ~]# 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 system-auth
account include system-auth
session include system-auth
session required pam_loginuid.so
2. 在編輯前作好備份
[root@butbueatiful ~]# cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak
[root@butbueatiful ~]# vim /etc/pam.d/vsftpd
#%PAM-1.0
auth sufficient /lib/security/pam_userdb.so db=/etc/vsftpd/virtusers
account sufficient /lib/security/pam_userdb.so db=/etc/vsftpd/virtusers
以上兩條是手動添加的,內容是對虛擬用戶的安全和賬戶權限進行驗證。
`auth`是指對用戶的用戶名口令進行驗證。
`accout`是指對用戶的賬戶有哪些權限哪些限制進行驗證。
`sufficient`表示充分條件,也就是說,一旦在這裏經過了驗證,那麼也就不用通過下面剩下的驗證步驟了。
相反,若是沒有經過的話,也不會被系統當即擋之門外,由於sufficient的失敗不決定整個驗證的失敗,
意味着用戶還必須將經歷剩下來的驗證審覈。
再後面的`/lib/security/pam_userdb.so`表示該條審覈將調用`pam_userdb.so`這個庫函數進行。
最後的`db=/etc/vsftpd/virtusers`則指定了驗證庫函數將到這個指定的數據庫中調用數據進行驗證。
#KC: The entries for Vsftpd-PAM are added above.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 system-auth
account include system-auth
session include system-auth
session required pam_loginuid.so
### 虛擬用戶的配置
1. 規劃好虛擬用戶的主路徑
[root@butbueatiful ~]# mkdir /var/ftpvirtuser
2. 建立測試用戶的FTP用戶目錄
[root@butbueatiful ~]# mkdir -p /var/ftpvirtuser/teacher/{share,private}
[root@butbueatiful ~]# ln -s /var/ftpvirtuser/teacher/share /var/ftpvirtuser/student
3. 更改虛擬用戶的主目錄的屬主爲虛擬宿主用戶
[root@butbueatiful ~]# chown -R ftpvirtuser.ftpvirtuser /var/ftpvirtuser
說明: 爲了節省空間我將 teacher 用戶配置的權限較大,而後將 teacher 裏的 share 目錄連接爲 /var/ftpvirtuser/student
4. 檢查權限
[root@butbueatiful ~]# ll /var/ftpvirtuser/
total 4
lrwxrwxrwx 1 ftpvirtuser ftpvirtuser 14 Apr 12 17:16 student -> teacher/share/
drwxr-xr-x 4 ftpvirtuser ftpvirtuser 4096 Apr 12 17:12 teacher
5. 建立虛擬用戶配置文件模版
[root@butbueatiful ~]# cp /etc/vsftpd/vsftpd.conf.backup /etc/vsftpd/virtuser_conf/virtuser_conf.tmp
6. 定製虛擬用戶模版配置文件
[root@butbueatiful ~]# vim /etc/vsftpd/virtuser_conf/virtuser_conf.tmp
```
# 指定虛擬用戶的具體主路徑
local_root=/opt/ftp/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
# 設定單個客戶端的最大線程數,這個配置主要來照顧Flashget、迅雷等多線程下載軟件
max_per_ip=5
# 設定該用戶的最大傳輸速率,單位b/s。
local_max_rate=50000
```
這裏將原 vsftpd.conf 配置文件通過簡化後保存做爲虛擬用戶配置文件的模版。
這裏將並不須要指定太多的配置內容,主要的框架和限制交由 vsftpd 的主配置文件 vsftpd.conf 來定義,
即虛擬用戶配置文件當中沒有提到的配置項目將參考主配置文件中的設定。
而在這裏做爲虛擬用戶的配置文件模版只須要留一些和用戶流量控制,訪問方式控制的配置項目就能夠了。
這裏的關鍵項是`local_root`這個配置,用來指定這個虛擬用戶的 ftp 主路徑。
### 給測試用戶定製
1.從虛擬用戶模版配置文件複製
[root@butbueatiful ~]# cp /etc/vsftpd/virtuser_conf/virtuser_conf.tmp /etc/vsftpd/virtuser_conf/teacher
2.針對具體用戶進行定製
[root@butbueatiful ~]# vim /etc/vsftpd/virtuser_conf/teacher
```
local_root=/var/ftpvirtuser/teacher
write_enable=YES
anon_umask=022
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
```
[root@butbueatiful ~]# vim /etc/vsftpd/virtuser_conf/student
```
local_root=/var/ftpvirtuser/student
anonymous_enable=NO
write_enable=NO
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=25000
```
### 重啓服務
[root@butbueatiful ~]# service vsftpd restart
### 測試(以teacher用戶爲例)
1. 在虛擬用戶目錄中預先放入文件
[root@butbueatiful ~]# touch /var/ftpvirtuser/teacher/share/a.c
2. 從本機做爲客戶端登錄ftP
[root@localhost ftpvirtuser]# ftp 192.168.146.128
Connected to 192.168.146.128 (192.168.146.128).
220 Welcome to login the FTP server ^_^
Name (192.168.146.128:root): teacher
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
3. 測試列單操做
ftp> ls
227 Entering Passive Mode (192,168,146,128,79,174).
150 Here comes the directory listing.
drwxr-xr-x 2 0 0 4096 Apr 12 10:01 private
drwxr-xr-x 2 0 0 4096 Apr 12 10:18 share
226 Directory send OK.
4. 測試上傳操做
ftp> put test.c
local: test.c remote: test.c
227 Entering Passive Mode (192,168,146,128,98,47).
150 Ok to send data.
226 Transfer complete.
2304 bytes sent in 0.0217 secs (106.18 Kbytes/sec)
ftp> ls
227 Entering Passive Mode (192,168,146,128,194,191).
150 Here comes the directory listing.
drwxr-xr-x 2 504 504 4096 Apr 12 10:01 private
drwxr-xr-x 2 504 504 4096 Apr 12 10:18 share
-rw-r--r-- 1 504 504 2304 Apr 13 01:26 test.c
226 Directory send OK.
5. 測試創建目錄操做
ftp> mkdir test
257 "/test" created
ftp> ls
227 Entering Passive Mode (192,168,146,128,195,168).
150 Here comes the directory listing.
drwxr-xr-x 2 504 504 4096 Apr 12 10:01 private
drwxr-xr-x 2 504 504 4096 Apr 12 10:18 share
drwxr-xr-x 2 504 504 4096 Apr 13 01:27 test
-rw-r--r-- 1 504 504 2304 Apr 13 01:26 test.c
226 Directory send OK.
6. 測試下載操做
ftp> cd share
250 Directory successfully changed.
ftp> get b.c
local: b.c remote: b.c
227 Entering Passive Mode (192,168,146,128,129,34).
150 Opening BINARY mode data connection for b.c (97 bytes).
226 Transfer complete.
97 bytes received in 0.0022 secs (44.17 Kbytes/sec)
7. 測試切換到系統的其它目錄
說明: 這個我用 lftp 登陸由於 ftp 的這個提示信息好"弱智",就算你沒有切換到其它目錄它也會顯示切換目錄成功
[root@butbueatiful ~]# lftp teacher@192.168.146.128
Password:
lftp teacher@192.168.146.128:~> ls
drwxr-xr-x 2 504 504 4096 Apr 12 10:01 private
drwxr-xr-x 2 504 504 4096 Apr 12 10:18 share
drwxr-xr-x 2 504 504 4096 Apr 13 01:27 test
-rw-r--r-- 1 504 504 2304 Apr 13 01:26 test.c
lftp teacher@192.168.146.128:/> cd ..
lftp teacher@192.168.146.128:/> ls
drwxr-xr-x 2 504 504 4096 Apr 12 10:01 private
drwxr-xr-x 2 504 504 4096 Apr 12 10:18 share
drwxr-xr-x 2 504 504 4096 Apr 13 01:27 test
-rw-r--r-- 1 504 504 2304 Apr 13 01:26 test.c
8. 測試用其它 ip 來登陸 ftp 服務器
[root@butbueatiful ~]# ftp 127.0.0.1
ftp: connect: Connection refused
9. 測試以匿名用戶登陸
[root@butbueatiful ~]# ftp 192.168.146.128
Connected to 192.168.146.128 (192.168.146.128).
220 Welcome to login the FTP server ^_^
Name (192.168.146.128:root):
530 Permission denied.
Login failed.
ftp>shell