Linux下基于vsftpd的FTP文件傳輸協(xié)議詳解(附完整流程命令)

理論概述

基本概念

FTP 是File Transfer Protocol(文件傳輸協(xié)議)的英文簡稱,而中文簡稱為“文傳協(xié)議”。用于Internet上的控制文件的雙向傳輸。同時,它也是一個應(yīng)用程序(Application)?;诓煌牟僮飨到y(tǒng)有不同的FTP應(yīng)用程序,而所有這些應(yīng)用程序都遵守同一種協(xié)議以傳輸文件。在FTP的使用當(dāng)中,用戶經(jīng)常遇到兩個概念:"下載"(Download)和"上傳"(Upload)。"下載"文件就是從遠(yuǎn)程主機(jī)拷貝文件至自己的計算機(jī)上;"上傳"文件就是將文件從自己的計算機(jī)中拷貝至遠(yuǎn)程主機(jī)上。用Internet語言來說,用戶可通過客戶機(jī)程序向(從)遠(yuǎn)程主機(jī)上傳(下載)文件。

vsftpd是“very secure FTP daemon”的縮寫,安全性是它的一個最大的特點(diǎn)。vsftpd 是一個 UNIX 類操作系統(tǒng)上運(yùn)行的服務(wù)器的名字,它可以運(yùn)行在諸如 Linux、BSD、Solaris、 HP-UNIX等系統(tǒng)上面,是一個完全免費(fèi)的、開放源代碼的ftp服務(wù)器軟件,支持很多其他的 FTP 服務(wù)器所不支持的特征。比如:非常高的安全性需求、帶寬限制、良好的可伸縮性、可創(chuàng)建虛擬用戶、支持IPv6、速率高等。
vsftpd是一款在Linux發(fā)行版中最受推崇的FTP服務(wù)器程序。特點(diǎn)是小巧輕快,安全易用。

使用協(xié)議及端口

  • TCP 21 建立命令鏈路
  • TCP 20 在主動傳輸模式下服務(wù)器使用20端口向客戶端建立數(shù)據(jù)鏈路

工作原理

  1. 客戶端向服務(wù)器發(fā)出連接請求,同時客戶端系統(tǒng)動態(tài)地打開一個大于1024的端口等候服務(wù)器連接(比如1031端口)
  2. 若FTP服務(wù)器在端口21偵聽到該請求,則會在客戶端1031端口和服務(wù)器的21端口之間建立一個FTP會話連接
  3. 當(dāng)需要傳輸數(shù)據(jù)時,F(xiàn)TP客戶端動態(tài)地打開一個大于1024的端口(比如1032端口)連接到服務(wù)器的20端口,并在這兩個端口之間進(jìn)行數(shù)據(jù)的傳輸。當(dāng)數(shù)據(jù)傳輸完畢后,這兩個端口會自動關(guān)閉
  4. 當(dāng)FTP客戶端斷開與FTP服務(wù)器的連接時,客戶端上動態(tài)分配的端口將自動釋放

工作模式

  • 主動傳輸模式(Active FTP):
    在主動傳輸模式下,F(xiàn)TP客戶端隨機(jī)開啟一個大于1024的端口N(1025)向服務(wù)器的21號端口發(fā)起連接,然后開放N+1號端口(1026)進(jìn)行監(jiān)聽,并向服務(wù)器發(fā)送PORT 1026命令。服務(wù)器接收到命令后,會用其本地的FTP數(shù)據(jù)端口(通常20)來連接客戶端指定的端口1026,進(jìn)行數(shù)據(jù)傳輸。

  • 被動傳輸模式(Passive FTP):
    在被動傳輸模式下,F(xiàn)TP客戶端隨機(jī)開啟一個大于1024的端口N(1025)向服務(wù)器的21號端口發(fā)起連接,同時會開啟N+1號端口(1026),然后向服務(wù)器發(fā)送PASV 命令。通知服務(wù)器自己處于被動模式。服務(wù)器收到命令后,會開放一個大于1024端口(1521)進(jìn)行監(jiān)聽,然后用PORT 命令通知客戶端,自己的數(shù)據(jù)端口是1521.客戶端收到命令后會通過1026號端口連接服務(wù)器的1521,然后在兩個端口之間進(jìn)行數(shù)據(jù)傳輸。

傳輸模式

  • 二進(jìn)制模式:圖片和執(zhí)行文件壓縮文件
  • 文本模式:CGI腳本和普通HTML文件

目前服務(wù)器上面和FTP客戶端軟件能夠自動識別文件類型并選擇相應(yīng)的傳輸方式

賬戶類型

  • 匿名用戶:ftp 或 anonymous
  • 本地用戶:Linux服務(wù)器本機(jī)的系統(tǒng)用戶賬號
  • 虛擬用戶:賬號信息存放在獨(dú)立的文件或數(shù)據(jù)庫內(nèi)

默認(rèn)安裝vsftpd特點(diǎn)

  • 匿名用戶與本地用戶都可以登錄
  • 匿名用戶登錄到/var/ftp,只能下載不能上傳
  • 本地用戶登錄到本地用戶的家目錄,可以上傳和下載

訪問ftp服務(wù)器方法

  • 常見的網(wǎng)頁瀏覽器(Firefox、IE等)
  • 常見的下載工具(wget、curl等)
  • 專用的FTP管理工具(Filezilla、CuteFTP、WinSCP等)
    ftp和lftp命令
get  //下載
put  //上傳
mget    //下載多個,支持通配符
mput    //上傳多個,支持通配符
!cmd    //執(zhí)行外面命令
lcd     //切換外面路徑

訪問FTP地址表示方法

  • 匿名訪問
ftp://192.168.4.5
ftp://192.168.4.5/path/to/file
  • 用戶驗證訪問
ftp://user:pass@192.168.4.5
ftp://user:pass@192.168.4.5/path/to/file

URL,統(tǒng)一資源定位器(網(wǎng)址)
Unified Resource Locator
協(xié)議://用戶名:密碼@服務(wù)器地址:端口/目錄路徑/文件名

vsftpd相關(guān)文件說明

主配置文件

/etc/vsftpd/vsftpd.conf

anonymous_enable=YES         //開啟匿名共享
local_enable=YES             //開啟本地賬戶共享
write_enable=YES             //本地賬戶是否可寫
anon_upload_enable=YES       //匿名用戶是否可以上傳文件
anon_mkdir_write_enable=YES  //匿名可創(chuàng)建目錄
anon_other_write_enable=YES  //匿名刪除,重命名
chroot_local_user           //禁錮本地賬戶
anon_root=/abc              //定義匿名用戶根目錄為/abc目錄
anon_umask=022              //定義匿名用戶上傳的掩碼值
listen_address=192.168.4.5  //定義監(jiān)聽地址為192.168.4.5

其他配置文件

  • 黑名單:/etc/vsftpd/ftpusers
  • 黑/白名單:/etc/vsftpd/user_list
    在字段userlist_enable=YES或NO //是否開啟user_list文件
    在字段userlist_deny=NO時user_list是白名單

其他常用選項

  • listen_port=端口號
  • listen_address=IP地址

默認(rèn)的vsftpd服務(wù):

允許匿名FTP訪問,只能下載不能上傳 【/var/ftp/】
允許本地用戶FTP訪問,既可以下載,也可以上傳 【~用戶名】

并發(fā)連接和速度:

max_clients     //最大并發(fā)連接數(shù)
max_per_ip      //同一IP地址的最大并發(fā)連接數(shù)
anon_max_rate   //匿名用戶訪問速度(字節(jié)/秒)
local_max_rate  //本地用戶訪問速度(字節(jié)/秒)

實踐操作

[root@server99 yum.repos.d]# yum -y install vsftpd ?。褂肶um進(jìn)行安裝
[root@server99 yum.repos.d]# service vsftpd start
為 vsftpd 啟動 vsftpd:                                    [確定]
[root@server99 yum.repos.d]# chkconfig vsftpd on  ?。O(shè)置為開機(jī)自啟
[root@server99 yum.repos.d]# chkconfig vsftpd --list //驗證查看開機(jī)狀態(tài)
vsftpd          0:off   1:off   2:on    3:on    4:on    5:on    6:off
//添加本地用戶,來驗證ftp
[root@server99 yum.repos.d]# useradd ftp-test-01
[root@server99 yum.repos.d]# echo 123456 | passwd --stdin ftp-test-01
Changing password for user ftp-test-01.
passwd: all authentication tokens updated successfully.
//查看用戶
[root@server99 yum.repos.d]# grep ftp /etc/passwd
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin ?。惭bvsftpd后,生成的系統(tǒng)用戶
ftp-test-01:x:500:500::/home/ftp-test-01:/bin/bash
//在ftp的缺省目錄中添加標(biāo)識文件,方便驗證
[root@server99 ~]# cd /var/ftp/         //匿名用戶缺省登錄訪問目錄
[root@server99 ftp]# ls
pub
[root@server99 ftp]# echo "anon user test file" > anon.txt
[root@server99 ftp]# ls
anon.txt  pub
[root@server99 ftp]# cd /home/ftp-test-01/      //普通(本地)用戶缺省登錄訪問目錄
[root@server99 ftp-test-01]# ls
[root@server99 ftp-test-01]# echo "local user test file" > local.txt
[root@server99 ftp-test-01]# ls
local.txt

```
以下是客戶端上的操作步驟:
```
[root@pc-97 ~]# yum -y install ftp?。褂茫酰戆惭bftp訪問客戶端,還可以使用瀏覽器等方式訪問,這里我們姑且使用ftp即可。
//匿名用戶
[root@pc-97 ~]# ftp 192.168.4.99 ?。L問ftp服務(wù)器,保證客戶端PC和FTP服務(wù)器可以連通
Connected to 192.168.4.99 (192.168.4.99).
220 (vsFTPd 2.2.2)
Name (192.168.4.99:root): ftp   //匿名用戶,也就是安裝vsftpd生成的系統(tǒng)用戶,用作匿名用戶進(jìn)行訪問
331 Please specify the password.
Password:               //直接回車進(jìn)入,匿名用戶不需要使用密碼
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (192,168,4,99,85,130).
150 Here comes the directory listing.
-rw-r--r--    1 0        0              20 Dec 16 23:45 anon.txt  ?。@里就是我們剛才創(chuàng)建的文件
drwxr-xr-x    2 0        0            4096 Mar 06  2015 pub
226 Directory send OK.
ftp> cd ..                          //返回上一級目錄,缺省配置下,匿名用戶會被禁錮根目錄
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (192,168,4,99,91,171).
150 Here comes the directory listing.
-rw-r--r--    1 0        0              20 Dec 16 23:45 anon.txt
drwxr-xr-x    2 0        0            4096 Mar 06  2015 pub
226 Directory send OK.
ftp> get anon.txt                       //測試下載文件,使用該方法會默認(rèn)將文件下載的當(dāng)前的本地目錄下
local: anon.txt remote: anon.txt
227 Entering Passive Mode (192,168,4,99,114,170).
150 Opening BINARY mode data connection for anon.txt (20 bytes).
226 Transfer complete.
20 bytes received in 5.2e-05 secs (384.62 Kbytes/sec)
ftp> quit
221 Goodbye.
[root@pc-97 ~]# ls anon.txt                 //驗證文件下載
anon.txt
[root@pc-97 ~]# echo "ftp put test file" > ftp-put.txt  //測試文件上傳
[root@pc-97 ~]# ls ftp-put.txt 
ftp-put.txt
[root@pc-97 ~]# ftp 192.168.4.99
Connected to 192.168.4.99 (192.168.4.99).
220 (vsFTPd 2.2.2)
Name (192.168.4.99:root): ftp
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> put ftp-put.txt                    //該命令可從當(dāng)前本地目錄進(jìn)行文件的上傳
local: ftp-put.txt remote: ftp-put.txt
227 Entering Passive Mode (192,168,4,99,241,45).
550 Permission denied.                  //顯示權(quán)限不足,已經(jīng)被拒絕,由于缺省不允許進(jìn)行文件上傳操作

//本地用戶
[root@pc-97 ~]# ftp 192.168.4.99
Connected to 192.168.4.99 (192.168.4.99).
220 (vsFTPd 2.2.2)
Name (192.168.4.99:root): ftp-test-01       //在服務(wù)器新建的本地ftp測試用戶
331 Please specify the password.
Password:                       //輸入當(dāng)前普通用戶的密碼
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (192,168,4,99,46,149).
150 Here comes the directory listing.
-rw-r--r--    1 0        0              21 Dec 16 23:45 local.txt?。?wù)器端創(chuàng)建的本地用戶的驗證文件
226 Directory send OK.
ftp> cd ..
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (192,168,4,99,73,87).
150 Here comes the directory listing.
drwx------    4 500      500          4096 Dec 16 23:45 ftp-test-01
226 Directory send OK.
//驗證本地用戶目錄禁錮
ftp> cd ..
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (192,168,4,99,248,92).
150 Here comes the directory listing.
dr-xr-xr-x    2 0        0            4096 Apr 20  2016 bin
dr-xr-xr-x    5 0        0            1024 Mar 29  2016 boot
drwxr-xr-x    2 0        0            4096 Jan 06  2015 cgroup
drwxr-xr-x   19 0        0            3660 Dec 16 23:15 dev
drwxr-xr-x   89 0        0            4096 Dec 16 23:41 etc
drwxr-xr-x    3 0        0            4096 Dec 16 23:40 home
dr-xr-xr-x   10 0        0            4096 Mar 29  2016 lib
dr-xr-xr-x    9 0        0           12288 Apr 20  2016 lib64
drwx------    2 0        0           16384 Mar 29  2016 lost+found
drwxr-xr-x    2 0        0            4096 Jun 28  2011 media
drwxr-xr-x    2 0        0            4096 Jun 28  2011 mnt
drwxr-xr-x    3 0        0            4096 Mar 29  2016 opt
dr-xr-xr-x  104 0        0               0 Dec 17  2016 proc
dr-xr-x---   11 0        0            4096 Dec 16 23:35 root
dr-xr-xr-x    2 0        0           12288 Apr 20  2016 sbin
drwxr-xr-x    7 0        0               0 Dec 17  2016 selinux
drwxr-xr-x    2 0        0            4096 Jun 28  2011 srv
drwxr-xr-x   13 0        0               0 Dec 17  2016 sys
drwxrwxrwt    3 0        0            4096 Dec 16 23:32 tmp
drwxr-xr-x   13 0        0            4096 Mar 29  2016 usr
drwxr-xr-x   20 0        0            4096 Dec 16 23:32 var
226 Directory send OK.      //可以看到本地用戶在缺省狀態(tài)下,并沒有禁錮ftp目錄,可以已知查看到根文件系統(tǒng)
//驗證本地用戶缺省下載
ftp> ls
227 Entering Passive Mode (192,168,4,99,48,154).
150 Here comes the directory listing.
-rw-r--r--    1 0        0              21 Dec 16 23:45 local.txt
226 Directory send OK.
ftp> get local.txt
local: local.txt remote: local.txt
227 Entering Passive Mode (192,168,4,99,181,48).
150 Opening BINARY mode data connection for local.txt (21 bytes).
226 Transfer complete.
21 bytes received in 4.9e-05 secs (428.57 Kbytes/sec)
[root@pc-97 ~]# ls local.txt  ?。螺d成功 
local.txt
//驗證本地用戶的缺省的上傳功能
[root@pc-97 ~]# ls ftp-put.txt 
ftp-put.txt
[root@pc-97 ~]# ftp 192.168.4.99
Connected to 192.168.4.99 (192.168.4.99).
220 (vsFTPd 2.2.2)
Name (192.168.4.99:root): ftp-test-01
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> put ftp-put.txt
local: ftp-put.txt remote: ftp-put.txt
227 Entering Passive Mode (192,168,4,99,251,192).
150 Ok to send data.
226 Transfer complete.
18 bytes sent in 5.7e-05 secs (315.79 Kbytes/sec)
ftp> ls
227 Entering Passive Mode (192,168,4,99,88,150).
150 Here comes the directory listing.
-rw-r--r--    1 500      500            18 Dec 17 00:22 ftp-put.txt
-rw-r--r--    1 0        0              21 Dec 16 23:45 local.txt  ?。蟼鞒晒?226 Directory send OK.


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ftp 文件傳輸協(xié)議 跨平臺 上傳下載文件 vsftpd 工具:非常安全的文件傳輸協(xié)議;默認(rèn)的命令端口21號,數(shù)據(jù)...
    柒夏錦閱讀 4,236評論 1 9
  • FTP服務(wù)概述 簡單FTP構(gòu)建及訪問 VSFTP服務(wù)基礎(chǔ) 用戶禁錮、黑白名單 FTP并發(fā)及帶寬限制 一、FTP服務(wù)...
    紫_軒閱讀 8,040評論 3 25
  • 1.FTP簡介 File Transfer Protocol 早期的三個應(yīng)用級協(xié)議之一? 基于C/S結(jié)構(gòu)? 雙通道...
    尛尛大尹閱讀 2,677評論 0 0
  • 圖片更清晰,文字在最下面 FTP是TCP/IP的一種應(yīng)用,使用TCP而不是UDP,所以是可靠的,面向連接的。 FT...
    停下浮躁的心閱讀 1,815評論 0 4
  • vsftpd.conf 部分:文件格式(5)索引 返回主要內(nèi)容 名稱 vsftpd.conf - vsftpd的配...
    張龍象閱讀 2,512評論 0 1

友情鏈接更多精彩內(nèi)容