CentOS 7.x 使用vsftpd來搭建ftp

一、環境說明

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 
[root@localhost ~]# uname -r
3.10.0-514.el7.x86_64
[root@localhost ~]# uname -i
x86_64
[root@localhost ~]# uname -p
x86_64
[root@localhost ~]# ip addr show|sed -rn '/inet[[:space:]]+127/! s/^[[:space:]]+inet[[:space:]]+(.*)\/[[:digit:]]+[[:space:]]+.*$/\1/p'
192.168.56.5

二、vsftpd軟件安裝和說明

2.1、ftp協議和實現的應用程序軟件vsftpd簡單概要

vsftpd是一款實現應用層ftp協議的應用程序,在CentOS默認的base倉庫有提供。我們簡單來看看軟件的描述信息:

[root@localhost ~]# yum info vsftpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.cn99.com
 * updates: mirrors.shu.edu.cn
Available Packages
Name        : vsftpd
Arch        : x86_64
Version     : 3.0.2
Release     : 25.el7
Size        : 171 k
Repo        : base/7/x86_64
Summary     : Very Secure Ftp Daemon
URL         : https://security.appspot.com/vsftpd.html
License     : GPLv2 with exceptions
Description : vsftpd is a Very Secure FTP daemon. It was written completely from
            : scratch.

由於ftp是一種明文的協議,它是基於C/S架構的,其私密信息傳輸和認證信息都可以很輕易的被抓取,所以ftp使用的場景要考慮安全設定。除此之外,ftp早期設計的時候只是爲了解決某種特定場景的問題,並沒有過多的考慮安全問題, 後來的很多服務端的應用程序實現也沒有過多的考慮安全設定,比如實現ftp協議的服務端的應用程序有wuftpd, proftpd, pureftpd, vsftpd,在其中,vsftpd相對其同類產品,安全性相對良好,也是現在紅帽發行版系統的默認yum倉庫裏的軟件。

2.2、安裝vsftpd以及安裝文件說明

安裝vsftpd,直接使用yum安裝就行。

[root@localhost ~]# yum -y install vsftpd
...... #安裝過程省略。

安裝完後的文件列表:

[root@localhost ~]# rpm -ql vsftpd
/etc/logrotate.d/vsftpd  #通過rsyslog管理的日誌輪詢腳本
/etc/pam.d/vsftpd	#基於pam認證框架的配置文件
/etc/vsftpd	#主配置文件根目錄
/etc/vsftpd/ftpusers	#基於黑白名單控制的默認配置文件之一
/etc/vsftpd/user_list	#基於黑白名單控制的默認配置文件之一
/etc/vsftpd/vsftpd.conf  #主配置文件
/etc/vsftpd/vsftpd_conf_migrate.sh #自帶的一個工具腳本,有時候可以用來移除舊版的配置文件和符號鏈接
/usr/lib/systemd/system-generators/vsftpd-generator#與unit文件相關的一個bash腳本
/usr/lib/systemd/system/vsftpd.service #vsftpd程序的unit文件
/usr/lib/systemd/system/vsftpd.target
/usr/lib/systemd/system/[email protected]
/usr/sbin/vsftpd  #主程序文件
/usr/share/doc/vsftpd-3.0.2  #下面的目錄都是一些文檔之類的
/usr/share/doc/vsftpd-3.0.2/AUDIT
/usr/share/doc/vsftpd-3.0.2/BENCHMARKS
/usr/share/doc/vsftpd-3.0.2/BUGS
/usr/share/doc/vsftpd-3.0.2/COPYING
/usr/share/doc/vsftpd-3.0.2/Changelog
/usr/share/doc/vsftpd-3.0.2/EXAMPLE
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/README
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/README.configuration
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.xinetd
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD/README
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD/README.configuration
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD/vsftpd.conf
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG/README
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG/README.configuration
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG/hosts.allow
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/README
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_HOSTS
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_HOSTS/README
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/README
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/README.configuration
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/logins.txt
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/vsftpd.pam
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS_2
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS_2/README
/usr/share/doc/vsftpd-3.0.2/FAQ
/usr/share/doc/vsftpd-3.0.2/INSTALL
/usr/share/doc/vsftpd-3.0.2/LICENSE
/usr/share/doc/vsftpd-3.0.2/README
/usr/share/doc/vsftpd-3.0.2/README.security
/usr/share/doc/vsftpd-3.0.2/REWARD
/usr/share/doc/vsftpd-3.0.2/SECURITY
/usr/share/doc/vsftpd-3.0.2/SECURITY/DESIGN
/usr/share/doc/vsftpd-3.0.2/SECURITY/IMPLEMENTATION
/usr/share/doc/vsftpd-3.0.2/SECURITY/OVERVIEW
/usr/share/doc/vsftpd-3.0.2/SECURITY/TRUST
/usr/share/doc/vsftpd-3.0.2/SIZE
/usr/share/doc/vsftpd-3.0.2/SPEED
/usr/share/doc/vsftpd-3.0.2/TODO
/usr/share/doc/vsftpd-3.0.2/TUNING
/usr/share/doc/vsftpd-3.0.2/vsftpd.xinetd
/usr/share/man/man5/vsftpd.conf.5.gz
/usr/share/man/man8/vsftpd.8.gz
/var/ftp   #默認的匿名用戶登錄路徑,也是系統用戶ftp的家目錄
/var/ftp/pub 

2.3、vsftpd的配置指令分類和使用簡單說明

[root@localhost vsftpd]# man vsftpd.conf | grep '^[^[:space:]]\+'
VSFTPD.CONF(5)                                                        File Formats Manual                                                        VSFTPD.CONF(5)
NAME
DESCRIPTION
FORMAT
BOOLEAN OPTIONS
NUMERIC OPTIONS
STRING OPTIONS
AUTHOR

vsftpd的配置文件主要分爲三大選項組成,分別爲布爾值類型選項、數值類型選項以及字符串值類型選項。
其中布爾值類型選項的值可以使用YES或NO以及1或0,TRUE或FALSE,手冊中建議使用YES或者NO,所以
我們最好統一一種風格,比較就使用推薦的YES或者NO來表示啓用或者不啓用某種選項。
數值類型的選項的值進可以使用數值或者八進制的數值,八進制以0開頭。字符串類型的選項的值可以使用字符串
或數值字符串。(以實際使用測試爲準)
vsftpd的選項設置格式爲:option(directive)=value,其中的指令部分前面不能有多餘的其他符號,特別是容易忽
視的空白字符。man手冊中說明來指令和等號之間以及值和等號之間也不能有任意的空白,不過實際測試效果有
些場景的選項可以有些不能,所以建議還是按照規範來定義。就按照shell的變量賦值風格來,不要按照python的
變量賦值風格來。註釋部分以符號#開頭,表示此行都是註釋。符合shell腳本風格。
原文如下:
Below is a list of boolean options. The value for a boolean option may be set to YES or NO.
Below  is  a  list of numeric options. A numeric option must be set to a non negative integer. Octal numbers are
 supported, for convenience of the umask options. To specify an octal number, use 0 as the first digit of the
  number.
Below is a list of string options.

The format of vsftpd.conf is very simple. Each line is either a comment or a directive. Comment lines start with a 
# and are ignored.A  directive  line has the format:
option=value
It is important to note that it is an error to put any space between the option, = and value.
Each setting has a compiled in default which may be modified in the configuration file.

三、ftp的主動模式和被動模式

ftp是C/S架構的,有服務端和客戶端的概念。ftp設計的連接認證以及數據傳輸比較特殊。它把指令傳輸和數據傳輸分爲兩個不同的連接通道。於是就有兩所謂的兩個連接方式,分別爲:
命令連接:傳輸命令
數據連接:傳輸數據
而其中的數據連接由根據命令連接後,數據連接建立的方式不同分爲主動連接和被動連接,也叫主動模式和被動模式。主動模式也叫PORT模式,被動模式可以叫PASV模式。數據連接分類:
主動模式(PORT模式):ftp服務端主動使用默認的20號端口(在vsftpd軟件中可以自定義)去連接客戶端當時發起命令連接的隨機端口向後的第一個可用端口(每次加1,不可用再往後尋找,直到找到可用的爲止)
被動模式(PASV模式):打開一個隨機端口,等待客戶端去連接。這個隨機端口的範圍在vsftpd實現中可以自定義。
請看下面的圖解:
在這裏插入圖片描述

主動模式和被動模式一直都是ftp使用的一個難點之一,不是因爲連接本身不好理解,而是實際生產使用配合上防火牆,會出現各種各樣的問題。我們來考慮這樣一種場景。假設我們的ftp服務端的監聽套接字的端口爲21(就是默認的端口),客戶端使用隨機端口11502向服務端發起連接請求,經過tcp的三次握手之後建立可靠的連接虛擬通道。如果此時發起一個命令叫做get FILENAME(下載一個文件),get是命令,會通過命令連接通道傳輸這個指令數據。而文件的內容通過磁盤獲得數據塊後要在內核中構建響應報文後,數據的傳輸要額外建立通道傳輸。假設使用主動模式來傳輸數據,此時ftp服務端會使用默認的數據連接端口20(只有ftp使用主動模式傳輸數據的時候,20端口的套接字纔有信息連接,一旦數據傳輸完成,20端口並不會像默認的21號端口那樣長期監聽處於監聽狀態)去向之前ftp發起命令通道的端口11502向後加1,通過探測發現是否可用,如果不可用再往後加1,直到找到可用的(假設超超時時間允許),假設剛好11503端口可用,ftp服務端會以20向ftp客戶端主動的11503端口主動發起連接請求,我們都直到,一般客戶端去連接服務端,服務端充當的角色是提供服務的,所以會開放連接套接字給指定的主機或者所有主機。客戶端本身只是會去使用固定隨機端口去連接服務端,客戶端本身的角色是享用外界提供的服務,它本身可能不提供服務,這個時候如果你ftp服務端去主動連接客戶端,一般情況下,客戶端那邊的防火牆是關閉的,不允許外界去連接它,只允許它去連接別人,通俗來講就是,客戶端本身出去的報文受限制的概率較低,而進入客戶端的報文受限制的概念很高,這也是很多應用場景ftp的主動模式不可用的主要原因之一。不過這些都不成問題,防火牆,比如linux的iptables實現,它本身只支持連接狀態追蹤功能的,所以這種場景剛好適用,前提是要會配置。說完ftp的主動模式,我們來說說的被動模式,被動模式就是在服務端收到ftp的指令的時候,並且確認來了雙方傳輸數據使用被動模式,ftp 服務端會隨機打開一個端口(當然,由於安全原因這種隨機的可能性,我們會實現配置它固定到一個端口訪問,服務端防火牆允許這個端口範圍的報文即可),ftp服務端會以一種特殊的方式走ftp的命令連接通道告訴ftp客戶端這個連接端口,ftp客戶端受到信息後計算出ftp服務端的這個隨機端口,然後ftp客戶端也會使用一個隨機端口去連接服務端的這個隨機端口,此次連接通過後,就可以開始傳輸數據了。

四、實現基本的用戶訪問設置

ftp的認證用戶可以分爲:匿名用戶(無密碼),系統用戶以及虛擬用戶。所以連接訪問方式也分爲這三種。基本的訪問用戶設置就是匿名用戶和系統用戶。一般生產環境爲了使用比較特殊的場景,會使用虛擬用戶(相對安全一點)。

4.1、匿名用戶配置和連接訪問測試

匿名用戶可以使用 anonymous 或 ftp,前者好理解,單詞本身就是匿名的之意。後者也可以是因爲默認的vsftpd安裝完成,會創建一個叫做ftp的系統用戶。匿名用戶訪問的初始根目錄就是ftp系統用戶的家目錄,默認是/var/ftp。來看看與匿名用戶設置相關的幾個常用選項:

[root@localhost vsftpd]# man vsftpd.conf | grep -E 'anonymous_|anon_'
       allow_anon_ssl
       anon_mkdir_write_enable
       anon_other_write_enable
       anon_upload_enable
       anon_world_readable_only
       anonymous_enable
       force_anon_data_ssl
       force_anon_logins_ssl
       no_anon_password
       anon_max_rate
       anon_umask
       anon_root
常用的有:
anonymous_enable=YES|NO  #是否啓用匿名用戶登錄,默認值爲YES
anon_upload_enable=YES|NO #是否允許匿名用戶可以上傳文件(這只是控制ftp實現層面,具體是否可寫,還要取決於文件系統的權限),默認值爲NO
anon_world_readable_only=YES|NO #如果啓用,匿名用戶將只能下載它可見的文件,默認值爲YES
anon_mkdir_write_enable=YES|NO #是否允許匿名創建目錄(這只是控制ftp實現層面,具體是否可以創建目錄,取決於匿名用戶ftp是否對父目錄具有寫權限),默認值NO
anon_other_write_enable=YES|NO #是否允許匿名用戶執行寫操作,包括刪除和重命名文件。默認值爲NO
anon_umask=077 #設置匿名用戶創建文件時候的umask值,默認值爲077

修改前備份一下:
[root@localhost ~]# cp /etc/vsftpd/vsftpd.conf{,.bak}

ftp協議加入防火牆允許(如果測試沒有開啓,就忽略下面這部分內容)
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: dhcpv6-client ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 
	
[root@localhost ~]# firewall-cmd --add-service=ftp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: dhcpv6-client ftp ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 

使用案例一:按照默認值,上面的參數常用匿名參數列表的值爲

anonymous_enable=YES
anon_upload_enable=NO
anon_world_readable_only=YES
anon_mkdir_write_enable=NO
anon_other_write_enable=NO
anon_umask=077

啓動服務:
systemctl start vsftpd.service
查看端口以及進程:
ss -tnl
ps aux|grep vsftpd

測試:
linux瀏覽器或者windows命令行可以測試。或者其他linux主機使用lftp或ftp連接測試。
lftp或ftp可能要使用yum來安裝。

[root@localhost ~]# ftp
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): ftp
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (192,168,56,5,198,216).
150 Here comes the directory listing.
drwxr-xr-x    2 0        0               6 Oct 30 19:45 pub
226 Directory send OK.
查看幫助:
ftp> help
Commands may be abbreviated.  Commands are:

!		debug		mdir		sendport	site
$		dir		mget		put		size
account		disconnect	mkdir		pwd		status
append		exit		mls		quit		struct
ascii		form		mode		quote		system
bell		get		modtime		recv		sunique
binary		glob		mput		reget		tenex
bye		hash		newer		rstatus		tick
case		help		nmap		rhelp		trace
cd		idle		nlist		rename		type
cdup		image		ntrans		reset		user
chmod		lcd		open		restart		umask
close		ls		prompt		rmdir		verbose
cr		macdef		passive		runique		?
delete		mdelete		proxy		send


是否可創建目錄?
ftp> mkdir test1
550 Permission denied.

是否可刪除文件或目錄?
ftp> rmdir pub
550 Permission denied.

是否可上傳文件或下載文件?
ftp> lcd /etc/
Local directory now /etc
ftp> put fstab
local: fstab remote: fstab
227 Entering Passive Mode (192,168,56,5,131,10).
550 Permission denied.
ftp> get pub
local: pub remote: pub
227 Entering Passive Mode (192,168,56,5,183,213).
550 Failed to open file.

使用案例2:給匿名用戶完全控制的權限,配置選項(正常生產環境要禁用匿名用戶登錄)
anonymous_enable=YES
anon_upload_enable=YES
anon_world_readable_only=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
anon_umask=077
測試略,要隨時注意文件系統目錄權限。因爲匿名用戶不常用,所以就不舉例。

4.2、系統用戶配置和連接訪問測試

用戶通過vsftpd服務訪問到的默認路徑,是用戶自己的家目錄;默認可以自己有權限訪問的所有路徑間切換;如果要禁錮用戶於其家目錄中,就不能讓系統用戶對其家目錄擁有寫的權限(也就是系統用戶設置爲ftp用戶登錄後,就不能讓它再登錄系統,否則就不安全了)。

幾個常用選項和配置說明:

local_enable=YES|NO #是否允許系統用戶可以登錄(/etc/passwd中出現的用戶),默認值爲NO。如果要讓匿名用戶
可以登錄以及虛擬用戶可以登錄,都需要啓用這個選項,因爲匿名用戶要使用系統用戶ftp,匿名用戶也要使用自
己定義的一個系統用戶來管理所有匿名用戶。

write_enable=YES|NO #是否允許FTP改變文件系統內容的相關命令。默認值爲NO。包含系統用戶是否可寫之意
local_umask=022 #指定系統用戶創建文件時候的umask值
輔助配置文件/etc/vsftpd/ftpusers,列在此文件中的用戶 均禁止使用ftp服務;
chroot_local_user=YES #禁錮所有本地用戶 於其家目錄;需要事先去除用戶對家目錄的寫權限;默認值爲NO
chroot_list_enable=YES 
chroot_list_file=/etc/vsftpd/chroot_list #禁錮列表中文件存在的用戶於其家目錄中;需要事先去除用戶對家目錄的寫權限;
控制可登錄vsftpd服務的用戶列表:
userlist_enable=YES #啓用/etc/vsftpd/user_list文件來控制可登錄用戶;
userlist_deny=YES|NO #YES:意味着此爲黑名單;NO:白名單;
userlist_file=/etc/vsftpd/user_list #黑白名單的用戶列表文件,每行一個用戶名。要在userlist_enable啓用的時候有效,而且至於是黑白名單是由userlist_deny選項來控制。

示例:

(1) 創建一個系統用戶,來實驗禁錮於在家目錄和沒有禁錮在家目錄的比較
[root@localhost ~]# useradd yanhui
[root@localhost ~]# echo 'yanhui.com' | passwd yanhui --stdin
Changing password for user yanhui.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# finger yanhui
Login: yanhui         			Name: 
Directory: /home/yanhui             	Shell: /bin/bash
Never logged in.
No mail.
No Plan.
默認沒有禁錮於家目錄:
[root@localhost ~]# ftp
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): yanhui
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (192,168,56,5,159,114).
150 Here comes the directory listing.
226 Directory send OK.
ftp> pwd
257 "/home/yanhui"
ftp> cd ../
250 Directory successfully changed.
ftp> pwd
257 "/home"
ftp> cd /var/tmp/
250 Directory successfully changed.
ftp> lcd /etc
Local directory now /etc
ftp> put fstab .
local: fstab remote: .
227 Entering Passive Mode (192,168,56,5,237,217).
553 Could not create file.
ftp> ls 
227 Entering Passive Mode (192,168,56,5,247,185).
150 Here comes the directory listing.
drwx------    4 0        0              59 Dec 02 21:27 dracut.PFkSf1
drwx------    3 0        0              17 Dec 16 09:00 systemd-private-60cceef7dd504b6da580f743fda92924-vmtoolsd.service-dEUclD
drwx------    3 0        0              17 Dec 02 21:27 systemd-private-7e00d9f20e814ed0b1fb4c07e8329e57-systemd-hostnamed.service-rkPmzP
drwx------    3 0        0              17 Dec 02 21:26 systemd-private-7e00d9f20e814ed0b1fb4c07e8329e57-vmtoolsd.service-shJVQq
226 Directory send OK.

可以看到,上面可以直接切換系統目錄,可以想象,這種做法危害有多大。

設置選項後chroot_local_user=YES,默認我們先看看系統用戶是否還能切換,沒有去掉用戶對家目錄的寫權限
登錄會有以下的提示:
[root@localhost ~]# ftp 192.168.56.5
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): yanhui
331 Please specify the password.
Password:
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
Login failed.
421 Service not available, remote server has closed connection
ftp> ls
Not connected.
原因是:從2.3.5之後,vsftpd增強了安全檢查,如果用戶被限定在了其主目錄下,則該用戶的主目錄不能再具有
寫權限了!如果檢查發現還有寫權限,就會報該錯誤。所以,如果我們想讓系統用戶鎖定在其主目錄下, 要去掉
其對目錄的寫權限。
[root@localhost ~]# ls -l /home/
total 0
drwx------ 2 yanhui yanhui 62 Dec 16 20:08 yanhui
[root@localhost ~]# chmod u-w /home/yanhui/
[root@localhost ~]# ls -l /home/
total 0
dr-x------ 2 yanhui yanhui 62 Dec 16 20:08 yanhui
重新測試登錄:
[root@localhost ~]# ftp 192.168.56.5
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): yanhui
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
257 "/"
ftp> ls
227 Entering Passive Mode (192,168,56,5,58,219).
150 Here comes the directory listing.
226 Directory send OK.
ftp> cd /etc/
550 Failed to change directory.
ftp> pwd
257 "/"
這次切換目錄就報錯了。


(2) 創建系統用戶,來實驗黑白名單
創建2個系統用戶,分別爲user1和user2來模擬黑白名單。
[root@localhost ~]# for i in {1..2};do
> useradd user${i}
> done
[root@localhost ~]# id user1
uid=1001(user1) gid=1001(user1) groups=1001(user1)
[root@localhost ~]# id user2
uid=1002(user2) gid=1002(user2) groups=1002(user2)
[root@localhost ~]# for i in {1..2};do
> echo "user${i}"|passwd user${i} --stdin
> done
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
實現黑名單,之前有創建一個系統用戶yanhui,這裏有創建兩個用戶,分別爲user1和user2.只有yanhui可以登錄,user1和user2都不能登錄。配置:
userlist_enable=YES
userlist_deny=NO
userlist_file=/etc/vsftpd/user_list

[root@localhost ~]# vim /etc/vsftpd/user_list 
[root@localhost ~]# cp /etc/vsftpd/user_list{,.bak}
[root@localhost ~]# >/etc/vsftpd/user_list
[root@localhost ~]# echo 'yanhui' >/etc/vsftpd/user_list
[root@localhost ~]# cat /etc/vsftpd/user_list
yanhui
測試登錄yanhui用戶:
[root@localhost ~]# ftp 
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): yanhui
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (192,168,56,5,47,98).
150 Here comes the directory listing.
226 Directory send OK.
ftp> pwd
257 "/"
測試登錄user1和user2用戶:
[root@localhost ~]# ftp
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): user1
530 Permission denied.
Login failed.
ftp> open 192.168.56.5 21
Already connected to 192.168.56.5, use close first.
ftp> close
221 Goodbye.
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): user2
530 Permission denied.
Login failed.
直接在輸入用戶後就拒絕登錄。 


實現白名單,出了yanhui用戶之外,其他用戶都能登錄。
userlist_enable=YES
userlist_deny=YES
userlist_file=/etc/vsftpd/user_list
改了一下userlist_deny爲YES,其他配置都不用動。重啓vsftpd.service,然後驗證。
驗證yanhui用戶登錄:
[root@localhost ~]# ftp
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): yanhui
530 Permission denied.
Login failed.
ftp> close
221 Goodbye.

驗證user1和user2用戶登錄:
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): user1
331 Please specify the password.
Password:
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
Login failed.
421 Service not available, remote server has closed connection
ftp> close
Not connected.
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): user2
331 Please specify the password.
Password:
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
Login failed.
421 Service not available, remote server has closed connection
上述失敗是由於之前遇到的安全爲題引起,可以把系統用戶家目錄的寫權限去掉或者
[root@localhost ~]# chmod u-w /home/user{1,2}
[root@localhost ~]# ls -ld /home/user{1,2}
dr-x------ 2 user1 user1 62 Dec 16 20:46 /home/user1
dr-x------ 2 user2 user2 62 Dec 16 20:46 /home/user2
再次測試:
[root@localhost ~]# ftp
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): user1
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> close
221 Goodbye.
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): user2
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> close
221 Goodbye.

4.3、一些與連接數、傳輸日誌、守護進程類型等相關的說明

(1) 與傳輸日誌相關的選項
vsftpd_log_file=/var/log/vsftpd.log
dual_log_enable=NO
syslog_enable=NO
xferlog_enable=YES
xferlog_file=/var/log/xferlog
xferlog_std_format=YES

(2) 與長傳下載速率以及併發連接數相關的選項
anon_max_rate=0
local_max_rate=0
max_clients=2000
max_per_ip=50

上述選項的相關主機可以參考:man 5 vsftpd.conf ,如果沒有,請安裝man-pages軟件包,我們這裏實驗不會用到,我會在其他翻譯博文中給出所有選項的含義的註解。


(3) 守護進程類型說明
standalone:獨立守護進程;由服務進程自行監聽套按字,並接收用戶訪問請求; 
transient:瞬時守護進程;由受託管方代爲監聽套按字,服務進程沒有訪問請求時不啓動;當託管方收到訪問請求時,才啓動服務進程;
其中瞬時守護進程(非獨立守護進程)在CentOS6和CentOS7託管的對象不同,分別如下:
CentOS 6:xinetd獨立守護進程, /etc/xinetd.d/,
CentOS 7:由systemd代爲監聽;

五、基於pam_mysql來實現虛擬用戶控制

5.1、幾個相關的選項說明

pam_service_name=ftp
#設置vsfptd的pam的配置文件,這個配置文件配置了具體授權和認證相關。默認值爲ftp,這個配置文件默認要放#在/etc/pam.d/目錄下,而且命名儘量和應用程序vsftpd相關。

guest_enable=YES|NO
#是否啓用虛擬用戶登錄。默認值爲NO,表示不啓用,如果設置爲啓用後,所有的虛擬用戶映射
#guest_username選項對應的系統用戶。即所有虛擬用戶都通過選項guest_username指定的系統用戶來管理。

guest_username=ftp
#設置用於被虛擬用戶映射的系統用戶,默認值爲ftp。

user_config_dir=
默認值爲none。定義用於管理不同的虛擬用戶的目錄,這個目錄可以單獨爲每個用戶設置不同的子配置文件,
裏面可以直接存放於虛擬用戶命名一樣的配置文件,然後單獨爲每個虛擬用戶自定義不同的權限。

virtual_use_local_privs=YES|NO
默認爲NO。如果設置爲YES,虛擬用戶權限檢查和本地系統用戶權限檢查一樣。默認爲NO,表示虛擬用戶權限
檢查和匿名用戶權限檢查一樣,這種權限檢查趨勢更加的嚴謹,特別是在寫權限控制方面。

local_root=
默認值爲none。這個選項用於控制,vsftpd的用戶在文件系統上登錄後切換到的路徑。在虛擬用戶時,可以通過
此選項來配置不同虛擬用戶切換的路徑不一樣。

說明:虛擬用戶的寫權限,通過匿名一樣的指令進行定義;

5.2、yum安裝mariadb以及mariadb的幾個選項設置

(1) 安裝mariadb-server

[root@localhost ~]# yum install mariadb-server
...... #安裝過程略

(2) 設置幾個參數

[root@localhost ~]# cat /etc/my.cnf.d/server.cnf
在上面配置文件的[mysqld]分組後面寫入一下配置:
skip-name-resolve=ON
lower_case_table_names=1
innodb_file_per_table=ON
含義分別爲:
跳過IP地址反向解析;
忽略字符大小敏感;
每個表一個數據文件;

啓動mysql:
[root@localhost ~]# systemctl start mariadb.service
[root@localhost ~]# ps aux|grep mysql
mysql     25378  0.0  0.3 113252  1580 ?        Ss   21:13   0:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql     25576  2.2 16.4 969280 79480 ?        Sl   21:13   0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root      25612  0.0  0.1 112648   960 pts/0    R+   21:14   0:00 grep --color=auto mysql
[root@localhost ~]# ss -tnl|grep 3306
LISTEN     0      50           *:3306                     *:*                  
[root@localhost ~]# ss -tnlp|grep 3306
LISTEN     0      50           *:3306                     *:*                   users:(("mysqld",pid=25576,fd=13))

(3) 安全設置

#要在mysql啓動的情況下
[root@localhost ~]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):   #第一次默認root密碼爲空,回車即可
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y  #是否設置root登錄密碼,肯定要設置,默認root是mysql的管理員賬號
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y  #是否移除匿名用戶,這裏就是用戶名爲空的賬戶,一定要刪除。
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n  #是否禁止root遠程登錄,禁止後只能通過本地迴環或localhost登錄,根據實際需要設置,我這裏就跳過沒有禁止。
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n #是否刪除默認的test數據庫,可以刪也可以不刪,沒有多大意義。
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y  #是否重新加載權限配置內容到內存中,要加載一起,理解生效權限表的配置
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

測試root登錄:
[root@localhost ~]# mysql -h127.0.0.1 -uroot -P3306 -pyanhui.com --default-character-set=utf8
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
測試OK。

5.3、編譯安裝pam_mysql

在CentOS6,這個軟件可以通過配置epel源後安裝。CentOS7,默認沒有提供,可以去網站http://sf.net/projects/pam-mysql/下載這個源碼包編譯安裝。有興趣或者有能力的話,可以去epel的源碼包目錄下載對應的SRPM包,參考裏面的SPEC文件手動製作用於CentOS 7上的rpm安裝包。CentOS6就不演示了,可以列出
CentOS 6的rpm包的安裝目錄作爲參考:

[root@VM_0_4_centos packages]# rpm -qpl pam_mysql-0.7-0.12.rc1.el6.x86_64.rpm
/lib64/security/pam_mysql.so
/usr/share/doc/pam_mysql-0.7
/usr/share/doc/pam_mysql-0.7/COPYING
/usr/share/doc/pam_mysql-0.7/CREDITS
/usr/share/doc/pam_mysql-0.7/ChangeLog
/usr/share/doc/pam_mysql-0.7/NEWS
/usr/share/doc/pam_mysql-0.7/README

CentOS 6.x上的pam_mysql的SRPM包下載鏈接爲:
https://mirrors.tuna.tsinghua.edu.cn/epel/6/SRPMS/Packages/p/pam_mysql-0.7-0.12.rc1.el6.src.rpm
CentOS 7.x上的pam_mysql的源碼包下載鏈接爲:
 http://sf.net/projects/pam-mysql/

CentOS 7上編譯安裝:

安裝兩個依賴:
yum install mariadb-devel pam-devel gcc gcc-c++ make openssl-devel

或者直接安裝開發包組以及指定包組可能沒安裝的軟件包:
yum groupinstall "Development Tools" "Server Platform Development"
yum install mariadb-devel pam-devel
過程忽略。

[root@localhost ~]# ls -l pam_mysql-0.7RC1.tar.gz 
-rw-r--r-- 1 root root 335240 Dec 16 21:22 pam_mysql-0.7RC1.tar.gz
[root@localhost ~]# tar -xf pam_mysql-0.7RC1.tar.gz 
[root@localhost ~]# cd pam_mysql-0.7RC1
[root@localhost pam_mysql-0.7RC1]# ./configure --help  #可以查看幫助信息

最終編譯和安裝:
./configure --with-pam=/usr --with-mysql=/usr --with-pam-mods-dir=/usr/lib64/security
make
make install 

查看文件:
[root@localhost pam_mysql-0.7RC1]# ls -l /usr/lib64/security/pam_mysql.*
-rwxr-xr-x 1 root root    886 Dec 16 21:39 /usr/lib64/security/pam_mysql.la
-rwxr-xr-x 1 root root 141680 Dec 16 21:39 /usr/lib64/security/pam_mysql.so
上面的pam_mysql.so這個模塊文件很關鍵,如果沒有就不行。默認的pam軟件包的模塊安裝路徑爲/usr/lib64/security,所以我們把第三方的模塊也安裝到這個默認的路徑下。

5.4、授權數據庫用戶,創建數據庫和表

(1) 管理員賬號來創建賬號並授權:
    GRANT ALL ON vsftpd.* TO vsftpd@'127.0.0.1' IDENTIFIED BY 'vsftpd';
    FLUSH PRIVILEGES; #不刷也可以,mysql5.1的時候要刷,這裏的默認的mariadb對於GRANT語句已經不需要刷新權限表了。
  測試新創建用戶vsftpd的登錄:
   [root@localhost ~]# mysql -h127.0.0.1 -uvsftpd -P3306 -pvsftpd
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

(2) 創建用於管理虛擬用戶的庫表
CREATE DATABASE vsftpd DEFAULT CHARACTER SET utf8;
USE vsftpd;
CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `password` char(48) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;   

MariaDB [(none)]> CREATE DATABASE vsftpd DEFAULT CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> USE vsftpd;
Database changed
MariaDB [vsftpd]> CREATE TABLE `users` (
    ->   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    ->   `name` varchar(100) NOT NULL,
    ->   `password` char(48) NOT NULL,
    ->   PRIMARY KEY (`id`),
    ->   UNIQUE KEY `name` (`name`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
Query OK, 0 rows affected (0.10 sec)

MariaDB [vsftpd]> SHOW TABLES;
+------------------+
| Tables_in_vsftpd |
+------------------+
| users            |
+------------------+
1 row in set (0.01 sec)
 

5.5、向虛擬用戶表中插入多個普通用戶

插入虛擬用戶tom和jerry的信息
INSERT INTO users(name,password) VALUES('tom',PASSWORD('tom')),('jerry',PASSWORD('jerry'));
MariaDB [vsftpd]> INSERT INTO users(name,password) VALUES('tom',PASSWORD('tom')),('jerry',PASSWORD('jerry'));
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [vsftpd]> SELECT * FROM users;
+----+-------+-------------------------------------------+
| id | name  | password                                  |
+----+-------+-------------------------------------------+
|  1 | tom   | *71FF744436C7EA1B954F6276121DB5D2BF68FC07 |
|  2 | jerry | *09FB9E6E2AA0750E9D8A8D22B6AA8D86C85BF3D0 |
+----+-------+-------------------------------------------+
2 rows in set (0.00 sec)

5.6、配置vsftpd的配置以及pam的配置

(1) 創建用於管理的虛擬用戶vftpadmin:
useradd  -s /sbin/nologin vftpadmin
[root@localhost ~]# useradd  -s /sbin/nologin vftpadmin
[root@localhost ~]# finger vftpadmin
Login: vftpadmin      			Name: 
Directory: /home/vftpadmin          	Shell: /sbin/nologin
Never logged in.
No mail.
No Plan.

(2) 設定虛擬用戶的子配置文件目錄爲:/etc/vsftpd/vsftpd_user_config
[root@localhost ~]# mkdir -pv /etc/vsftpd/vsftpd_user_config
mkdir: created directory ‘/etc/vsftpd/vsftpd_user_config’

(3) 創建用於vsftpd的pam的配置文件:
[root@localhost ~]# cat /etc/pam.d/vsftpd.vftpadmin
auth required /usr/lib64/security/pam_mysql.so user=vsftpd passwd=vsftpd host=127.0.0.1 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2
account required /usr/lib64/security/pam_mysql.so user=vsftpd passwd=vsftpd host=127.0.0.1 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2

配置字段說明(如果要弄懂原理,要看得懂pam的配置才行)
auth 表示認證 
account 驗證賬號密碼正常使用 
required 表示認證要通過 
pam_mysql.so模塊是默認的相對路徑,是相對/lib64/security/路 徑而言,也可以寫絕對路徑;後面爲給此模塊傳遞的參數 
user=vsftpd   登錄mysql的用戶 
passwd=vsftpd 登錄mysql的的密碼,我們的vsftpd用戶的密碼vsftpd,之前有授權過
host=127.0.0.1   mysql服務器的主機名或ip地址,我們之前授權vsftpd用戶通過127.0.0.1登錄纔有權限。
db=vsftpd  指定連接msyql的數據庫名稱 
table=users 指定連接數據庫中的表名 
usercolumn=name 當做用戶名的字段 
passwdcolumn=password 當做用戶名字段的密碼 
crypt=2 密碼的加密方式爲mysql password()函數加密,crypt是加密方式,0表示不加密,1表示crypt(3)加密,2表示使用mysql password()函數加密,3表示md5加密,4表示sha1 加密 

(4) 設定工作目錄
[root@localhost ~]# mkdir -pv /data/vftpadmin/upload
mkdir: created directory ‘/data/vftpadmin’
mkdir: created directory ‘/data/vftpadmin/upload’
[root@localhost ~]# chmod a-w /data/vftpadmin  #爲了應對高版本的vsftpd的安全問題,只能單獨設置一個切換的目錄,然後上傳文件控制到子目錄中。
[root@localhost ~]# ls -l /data/vftpadmin/upload/
total 0
[root@localhost ~]# ls -ld /data/vftpadmin/upload/
drwxr-xr-x 2 root root 6 Dec 16 22:18 /data/vftpadmin/upload/
[root@localhost ~]# setfacl -m u:vftpadmin:rwx /data/vftpadmin/upload
[root@localhost ~]# getfacl /data/vftpadmin/upload
getfacl: Removing leading '/' from absolute path names
# file: data/vftpadmin/upload
# owner: root
# group: root
user::rwx
user:vftpadmin:rwx
group::r-x
mask::rwx
other::r-x

5.7、模擬測試不同虛擬用戶的不同權限

全局配置文件加入或者啓用:
pam_service_name=vsftpd.vftpadmin
user_config_dir=/etc/vsftpd/vsftpd_user_config

兩個虛擬用戶的子配置文件:
[root@localhost upload]# cat /etc/vsftpd/vsftpd_user_config/tom 
local_root=/data/vftpadmin
guest_enable=YES  #啓用虛擬用戶
write_enable=YES  #系統用戶可寫(vftpadmin)
download_enable=YES #全局允許所有下載請求
guest_username=vftpadmin #映射虛擬用戶的系統用戶爲vftpadmin
virtual_use_local_privs=NO #前面有介紹,建議使用默認值NO, 其他權限控制和匿名用戶權限一樣。如果設置
#爲YES,其他權限控制和系統用戶一樣的。
local_umask=007  #虛擬用戶創建文件的umask值
anonymous_enable=NO #匿名用戶不可登錄
anon_upload_enable=YES #匿名是否支持上傳功能,YES表示支持。由於我們的virtual_user_local_privs權限爲
#NO,所以這裏也是控制虛擬用戶是否可以上傳。
anon_world_readable_only=NO #要想虛擬用戶能下載,即使download_enable爲YES,這個也不能設置爲
#YES,否則就不可以下載了。因爲是虛擬用戶映射vftpadmin用戶,其本身不屬於虛擬用戶的文件,所以不能做
#這樣的控制。
anon_mkdir_write_enable=YES #匿名用戶是否支持創建文件功能,YES表示支持,和上面同理,虛擬用戶可以
#創建文件
anon_other_write_enable=YES #匿名用戶是否支持刪除文件功能,YES表示支持,和上面同理,虛擬用戶可以
#刪除文件

[root@localhost upload]# cat /etc/vsftpd/vsftpd_user_config/jerry 
local_root=/data/vftpadmin
guest_enable=YES
write_enable=NO
download_enable=NO
guest_username=vftpadmin
virtual_use_local_privs=NO
local_umask=007
anonymous_enable=NO
anon_upload_enable=NO
anon_world_readable_only=NO 
anon_mkdir_write_enable=NO

重啓服務:
[root@localhost vsftpd_user_config]# systemctl restart vsftpd.service

驗證測試:tom用戶
[root@localhost ~]# ftp
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): tom
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd /etc    #切換目錄失敗,符合預期值。前面有講如何限定用戶不能切換目錄
550 Failed to change directory.
ftp> pwd
257 "/"
ftp> ls
227 Entering Passive Mode (192,168,56,5,19,204).
150 Here comes the directory listing.
drwxrwxr-x    3 0        0              31 Dec 16 14:32 upload
226 Directory send OK.
ftp> cd upload
250 Directory successfully changed.
ftp> lcd /etc
Local directory now /etc
ftp> ls
227 Entering Passive Mode (192,168,56,5,90,61).
150 Here comes the directory listing.
226 Directory send OK.
ftp> put fstab   #上傳文件OK
local: fstab remote: fstab
227 Entering Passive Mode (192,168,56,5,169,151).
150 Ok to send data.
226 Transfer complete.
541 bytes sent in 6.3e-05 secs (8587.30 Kbytes/sec)
ftp> mkdir test1    #創建目錄OK
257 "/upload/test1" created
ftp> mkdir test2
257 "/upload/test2" created
ftp> rmdir test2    #刪除目錄OK
250 Remove directory operation successful.
ftp> put passwd
local: passwd remote: passwd
227 Entering Passive Mode (192,168,56,5,219,91).
150 Ok to send data.
226 Transfer complete.
1008 bytes sent in 3.5e-05 secs (28800.00 Kbytes/sec)
ftp> delete passwd   #刪除文件OK
250 Delete operation successful.
ftp> ls
227 Entering Passive Mode (192,168,56,5,63,84).
150 Here comes the directory listing.
-rw-------    1 1003     1003          541 Dec 16 14:43 fstab
drwx------    2 1003     1003            6 Dec 16 14:43 test1
226 Directory send OK.
ftp> lcd /etc/
Local directory now /etc
ftp> mput issue passwd
mput issue? y
227 Entering Passive Mode (192,168,56,5,194,181).
150 Ok to send data.
226 Transfer complete.
23 bytes sent in 3.2e-05 secs (718.75 Kbytes/sec)
mput passwd? y
227 Entering Passive Mode (192,168,56,5,239,122).
150 Ok to send data.
226 Transfer complete.
1008 bytes sent in 3.4e-05 secs (29647.06 Kbytes/sec)
ftp> lcd /var/tmp/
Local directory now /var/tmp
ftp> ls
227 Entering Passive Mode (192,168,56,5,182,33).
150 Here comes the directory listing.
-rwx------    1 1003     1003          541 Dec 16 15:19 fstab
-rw-------    1 1003     1003           23 Dec 16 15:20 issue
-rw-------    1 1003     1003         1008 Dec 16 15:20 passwd
drwx------    2 1003     1003            6 Dec 16 15:12 test1
226 Directory send OK.
ftp> mget issue fstab   #下載測試OK
mget issue? y
227 Entering Passive Mode (192,168,56,5,166,86).
150 Opening BINARY mode data connection for issue (23 bytes).
226 Transfer complete.
23 bytes received in 2e-05 secs (1150.00 Kbytes/sec)
mget fstab? y
227 Entering Passive Mode (192,168,56,5,154,7).
150 Opening BINARY mode data connection for fstab (541 bytes).
226 Transfer complete.
541 bytes received in 4.5e-05 secs (12022.22 Kbytes/sec)


驗證測試:jerry用戶
ftp> open 192.168.56.5 
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): jerry
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd upload
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (192,168,56,5,47,167).
150 Here comes the directory listing.
-rwx------    1 1003     1003          541 Dec 16 15:19 fstab
-rw-------    1 1003     1003           23 Dec 16 15:20 issue
-rw-------    1 1003     1003         1008 Dec 16 15:20 passwd
drwx------    2 1003     1003            6 Dec 16 15:12 test1
226 Directory send OK.
ftp> mkdir test2    #創建目錄失敗
550 Permission denied.
ftp> rmdir test1    #刪除目錄失敗
550 Permission denied.
ftp> lcd /etc      
Local directory now /etc
ftp> put passwd   #上傳文件失敗
local: passwd remote: passwd
227 Entering Passive Mode (192,168,56,5,206,125).
550 Permission denied.
ftp> delete fstab    #刪除文件失敗
550 Permission denied.
ftp> lcd /tmp
Local directory now /tmp
ftp> get issue    #下載文件失敗
local: issue remote: issue
227 Entering Passive Mode (192,168,56,5,178,217).
550 Permission denied.
驗證來,tom用戶擁有幾乎所有權限,jerry用戶只擁有隻讀的權限。

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