理論概述
基本概念
FTP 是File Transfer Protocol(文件傳輸協議)的英文簡稱,而中文簡稱為“文傳協議”。用于Internet上的控制文件的雙向傳輸。同時,它也是一個應用程序(Application)。基于不同的操作系統有不同的FTP應用程序,而所有這些應用程序都遵守同一種協議以傳輸文件。在FTP的使用當中,用戶經常遇到兩個概念:"下載"(Download)和"上傳"(Upload)。"下載"文件就是從遠程主機拷貝文件至自己的計算機上;"上傳"文件就是將文件從自己的計算機中拷貝至遠程主機上。用Internet語言來說,用戶可通過客戶機程序向(從)遠程主機上傳(下載)文件。
vsftpd是“very secure FTP daemon”的縮寫,安全性是它的一個最大的特點。vsftpd 是一個 UNIX 類操作系統上運行的服務器的名字,它可以運行在諸如 Linux、BSD、Solaris、 HP-UNIX等系統上面,是一個完全免費的、開放源代碼的ftp服務器軟件,支持很多其他的 FTP 服務器所不支持的特征。比如:非常高的安全性需求、帶寬限制、良好的可伸縮性、可創建虛擬用戶、支持IPv6、速率高等。
vsftpd是一款在Linux發行版中最受推崇的FTP服務器程序。特點是小巧輕快,安全易用。
使用協議及端口
- TCP 21 建立命令鏈路
- TCP 20 在主動傳輸模式下服務器使用20端口向客戶端建立數據鏈路
工作原理
- 客戶端向服務器發出連接請求,同時客戶端系統動態地打開一個大于1024的端口等候服務器連接(比如1031端口)
- 若FTP服務器在端口21偵聽到該請求,則會在客戶端1031端口和服務器的21端口之間建立一個FTP會話連接
- 當需要傳輸數據時,FTP客戶端動態地打開一個大于1024的端口(比如1032端口)連接到服務器的20端口,并在這兩個端口之間進行數據的傳輸。當數據傳輸完畢后,這兩個端口會自動關閉
- 當FTP客戶端斷開與FTP服務器的連接時,客戶端上動態分配的端口將自動釋放
工作模式
主動傳輸模式(Active FTP):
在主動傳輸模式下,FTP客戶端隨機開啟一個大于1024的端口N(1025)向服務器的21號端口發起連接,然后開放N+1號端口(1026)進行監聽,并向服務器發送PORT 1026命令。服務器接收到命令后,會用其本地的FTP數據端口(通常20)來連接客戶端指定的端口1026,進行數據傳輸。被動傳輸模式(Passive FTP):
在被動傳輸模式下,FTP客戶端隨機開啟一個大于1024的端口N(1025)向服務器的21號端口發起連接,同時會開啟N+1號端口(1026),然后向服務器發送PASV 命令。通知服務器自己處于被動模式。服務器收到命令后,會開放一個大于1024端口(1521)進行監聽,然后用PORT 命令通知客戶端,自己的數據端口是1521.客戶端收到命令后會通過1026號端口連接服務器的1521,然后在兩個端口之間進行數據傳輸。
傳輸模式
- 二進制模式:圖片和執行文件壓縮文件
- 文本模式:CGI腳本和普通HTML文件
目前服務器上面和FTP客戶端軟件能夠自動識別文件類型并選擇相應的傳輸方式
賬戶類型
- 匿名用戶:ftp 或 anonymous
- 本地用戶:Linux服務器本機的系統用戶賬號
- 虛擬用戶:賬號信息存放在獨立的文件或數據庫內
默認安裝vsftpd特點
- 匿名用戶與本地用戶都可以登錄
- 匿名用戶登錄到/var/ftp,只能下載不能上傳
- 本地用戶登錄到本地用戶的家目錄,可以上傳和下載
訪問ftp服務器方法
- 常見的網頁瀏覽器(Firefox、IE等)
- 常見的下載工具(wget、curl等)
- 專用的FTP管理工具(Filezilla、CuteFTP、WinSCP等)
ftp和lftp命令
get //下載
put //上傳
mget //下載多個,支持通配符
mput //上傳多個,支持通配符
!cmd //執行外面命令
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,統一資源定位器(網址)
Unified Resource Locator
協議://用戶名:密碼@服務器地址:端口/目錄路徑/文件名
vsftpd相關文件說明
主配置文件
/etc/vsftpd/vsftpd.conf
anonymous_enable=YES //開啟匿名共享
local_enable=YES //開啟本地賬戶共享
write_enable=YES //本地賬戶是否可寫
anon_upload_enable=YES //匿名用戶是否可以上傳文件
anon_mkdir_write_enable=YES //匿名可創建目錄
anon_other_write_enable=YES //匿名刪除,重命名
chroot_local_user //禁錮本地賬戶
anon_root=/abc //定義匿名用戶根目錄為/abc目錄
anon_umask=022 //定義匿名用戶上傳的掩碼值
listen_address=192.168.4.5 //定義監聽地址為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地址
默認的vsftpd服務:
允許匿名FTP訪問,只能下載不能上傳 【/var/ftp/】
允許本地用戶FTP訪問,既可以下載,也可以上傳 【~用戶名】
并發連接和速度:
max_clients //最大并發連接數
max_per_ip //同一IP地址的最大并發連接數
anon_max_rate //匿名用戶訪問速度(字節/秒)
local_max_rate //本地用戶訪問速度(字節/秒)
實踐操作
[root@server99 yum.repos.d]# yum -y install vsftpd //使用Yum進行安裝
[root@server99 yum.repos.d]# service vsftpd start
為 vsftpd 啟動 vsftpd: [確定]
[root@server99 yum.repos.d]# chkconfig vsftpd on //設置為開機自啟
[root@server99 yum.repos.d]# chkconfig vsftpd --list //驗證查看開機狀態
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 //安裝vsftpd后,生成的系統用戶
ftp-test-01:x:500:500::/home/ftp-test-01:/bin/bash
//在ftp的缺省目錄中添加標識文件,方便驗證
[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 //使用yum安裝ftp訪問客戶端,還可以使用瀏覽器等方式訪問,這里我們姑且使用ftp即可。
//匿名用戶
[root@pc-97 ~]# ftp 192.168.4.99 //訪問ftp服務器,保證客戶端PC和FTP服務器可以連通
Connected to 192.168.4.99 (192.168.4.99).
220 (vsFTPd 2.2.2)
Name (192.168.4.99:root): ftp //匿名用戶,也就是安裝vsftpd生成的系統用戶,用作匿名用戶進行訪問
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,4,99,85,130).
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> 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 //測試下載文件,使用該方法會默認將文件下載的當前的本地目錄下
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 //該命令可從當前本地目錄進行文件的上傳
local: ftp-put.txt remote: ftp-put.txt
227 Entering Passive Mode (192,168,4,99,241,45).
550 Permission denied. //顯示權限不足,已經被拒絕,由于缺省不允許進行文件上傳操作
//本地用戶
[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 //在服務器新建的本地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,4,99,46,149).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 21 Dec 16 23:45 local.txt //服務器端創建的本地用戶的驗證文件
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. //可以看到本地用戶在缺省狀態下,并沒有禁錮ftp目錄,可以已知查看到根文件系統
//驗證本地用戶缺省下載
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 //下載成功
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.