Linux FTP文件服務

本文摘要:(1)瞭解什麼是FTP服務(2)服務端會部署FTP服務,客戶端會使用FTP工具ftp,lftp(3)匿名用戶向服務端上傳下載文件(4)本地用戶訪問ftp服務端(5)限制本地用戶越獄

 

 ftp文件服務

vsftpd 是“very secure FTP daemon”的縮寫,是一個完全免費的、開放源代碼的ftp服務器軟件。特點是:非常高的安全性需求、帶寬限制、良好的可伸縮性等。(FTP和NFS不同的地方在於文件的管理上是不同的,NFS是以塊的形式讀取文件,FTP是以副本的形式來存取文件,即讀的是整個文件)

 

(1)工作原理

vsftpd使用ftp協議,該協議屬於應用層協議。它是典型的c/s架構,ftp服務端用來存儲文件,ftp客戶端可以通過ftp協議連接服務端實現上傳和下載資源。

ftp使用tcp的21端口進行命令傳輸,然後用tcp 的20端口進行數據傳輸(主動模式端口固定20)。默認是被動模式(端口隨機分配)。

 

(2)安裝部署(selinux 防火牆關閉

服務端192.168.179.100   客戶端 192.168.179.99

[root@localhost ~]# yum install vsftpd ftp lftp -y   --服務端安裝

vsftpd: 爲服務端軟件

ftp、lftp: 爲客戶端工具,推薦使用lftp

 

[root@localhost ~]# yum install  ftp lftp -y   --客戶端安裝

 

(3)服務端啓動服務

[root@localhost ~]# systemctl start vsftpd

[root@localhost ~]# netstat -tpln | grep vsftpd  --可以看到多了21端口

tcp6       0      0 :::21                   :::*                    LISTEN      4962/vsftpd

 

(4)客戶端測試

匿名用戶訪問:

用ftp客戶端匿名登錄需要輸入用戶名及密碼驗證,匿名用戶名爲:ftp或者anonymous,密碼爲空。用lftp客戶端匿名登錄則不需要輸入以上信息。

 


匿名用戶去訪問ftp服務器

[root@localhost ]# ftp 192.168.179.100  --客戶端訪問服務端

Connected to 192.168.179.100 (192.168.179.100).

220 (vsFTPd 3.0.2)

Name (192.168.179.100: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,179,100,113,193).

150 Here comes the directory listing.

drwxr-xr-x    2 0        0               6 Oct 30  2018 pub

226 Directory send OK.

 

匿名用戶默認登入的目錄是位於ftp服務端的pub目錄,這個目錄位於服務端的/var/ftp下面

[root@localhost ~]# cd /var/ftp/

[root@localhost ftp]# ls

pub

 

[root@localhost ftp]# cp /etc/fstab /var/ftp/pub/  --服務端pub下有了fstab文件了

ftp> pwd  --服務端查看pub下是否存在ftp文件

257 "/pub"

ftp> ls

227 Entering Passive Mode (192,168,179,100,156,253).

150 Here comes the directory listing.

-rw-r--r--    1 0        0             501 Mar 04 12:25 fstab

226 Directory send OK.

 

匿名用戶get單個下載文件

ftp> get fstab  --對該文件進行下載

local: fstab remote: fstab

227 Entering Passive Mode (192,168,179,100,111,116).

150 Opening BINARY mode data connection for fstab (501 bytes).

226 Transfer complete.

501 bytes received in 0.00225 secs (222.17 Kbytes/sec)

 

ftp> exit  --退出看看文件有沒有下載到本地

221 Goodbye.

[root@localhost ~]# ls

anaconda-ks.cfg  fstab

 

 

[root@localhost pub]# touch file1{1..9}  --在服務端再創建一些文件

[root@localhost pub]# ls

file11  file12  file13  file14  file15  file16  file17  file18  file19  fstab

ftp> ls  --客戶端要批量下載這些文件

227 Entering Passive Mode (192,168,179,100,206,50).

150 Here comes the directory listing.

-rw-r--r--    1 0        0               0 Mar 04 12:44 file11

-rw-r--r--    1 0        0               0 Mar 04 12:44 file12

-rw-r--r--    1 0        0               0 Mar 04 12:44 file13

-rw-r--r--    1 0        0               0 Mar 04 12:44 file14

-rw-r--r--    1 0        0               0 Mar 04 12:44 file15

-rw-r--r--    1 0        0               0 Mar 04 12:44 file16

-rw-r--r--    1 0        0               0 Mar 04 12:44 file17

-rw-r--r--    1 0        0               0 Mar 04 12:44 file18

-rw-r--r--    1 0        0               0 Mar 04 12:44 file19

-rw-r--r--    1 0        0             501 Mar 04 12:25 fstab

 

匿名用戶mget批量下載文件(不管是mget還是get命令下載的內容都在服務端當前目錄下)

ftp> mget file*   --ftp客戶端批量下載服務端文件,可以看出交互式的,每次下載都得詢問,,要查看ftp更多命令使用ftp>help來查看

mget file11?

227 Entering Passive Mode (192,168,179,100,63,59).

150 Opening BINARY mode data connection for file11 (0 bytes).

226 Transfer complete.

mget file12? y

 

ftp> prompt off  --關閉詢問這種交互模式

Interactive mode off.

ftp> prompt off  --再去下載就不會有詢問是否要下載文件

Interactive mode off.

ftp> mget file*

local: file11 remote: file11

227 Entering Passive Mode (192,168,179,100,30,126).

150 Opening BINARY mode data connection for file11 (0 bytes).

226 Transfer complete.

local: file12 remote: file12

227 Entering Passive Mode (192,168,179,100,175,245).

 

ftp> put fstab  --可以看到匿名用戶不能上傳,因爲沒有權限

local: fstab remote: fstab

227 Entering Passive Mode (192,168,179,100,208,220).

550 Permission denied.

ftp> mkdir abc  --匿名用戶也不能創建目錄

550 Create directory operation failed.

 

總結就是:使用匿名用戶登入,默認配置只能進行文件的讀取和下載,不能進行寫入和上傳文件

 

 

修改ftp的配置文件,開啓匿名用戶創建文件,重命名,刪除,上傳權限(注意selinux要關閉和防火牆關閉)

[root@localhost pub]# vi /etc/vsftpd/vsftpd.conf  --開啓下面三個權限差不多開啓了匿名用戶大部分功能

#開啓上傳權限 anon_upload_enable=YES

#開啓創建文件權限 anon_mkdir_write_enable=YES

#開啓重命名,刪除權限 anon_other_write_enable=YES

[root@localhost pub]# systemctl restart vsftpd  --修改配置文件之後重啓服務

 

來到客戶端

ftp> lcd /etc  --lcd是切換到本地客戶端的目錄

Local directory now /etc

ftp> put fstab  --可以看到還是上傳不了,這個是因爲上一級目錄權限的問題

local: fstab remote: fstab

421 Timeout.

Passive mode refused.

 

[root@localhost pub]# ls -ld /var/ftp/pub  --當服務端的配置都已經修改好,那麼就應該考慮目錄的權限問題了

drwxr-xr-x. 2 root root 145 Mar  4 20:44 /var/ftp/pub

[root@localhost pub]# chmod o+w /var/ftp/pub/  --修改目錄權限

[root@localhost pub]# ls -ld /var/ftp/pub

drwxr-xrwx. 2 root root 145 Mar  4 20:44 /var/ftp/pub

 

再次來到客戶端

ftp> mkdir abc  --可以創建目錄

257 "/pub/abc" created

[root@localhost pub]# ll

total 8

drwx------ 2 ftp  ftp    6 Mar  5 20:05 abc

ftp> lcd /etc

Local directory now /etc

ftp> put fstab  --可以上傳文件(如果是實用得ftp客戶端,不能直接實用絕對路徑上傳,需要先切換到本地文件路徑,直接上傳文件)

local: fstab remote: fstab

227 Entering Passive Mode (192,168,179,100,98,80).

150 Ok to send data.

226 Transfer complete.

501 bytes sent in 7.6e-05 secs (6592.11 Kbytes/sec)

 

lftp 192.168.179.100:/pub> put /etc/fstab -o fs  --使用lftp上傳文件想改名就要帶-o refile了

501 bytes transferred

[root@localhost pub]# ll fs

-rw------- 1 ftp ftp 501 Mar  5 20:20 fs

我們將本地文件fstab上傳到了匿名用戶的pub目錄並修改了文件名

 

lftp上傳多個文件,可以使用put和mput命令上傳,多個文件之間用空格分隔,如果想使用通配符,只有mput命令支持:

lftp 192.168.179.100:/pub> put /etc/fstab /etc/favicon.png

1580 bytes transferred         

Total 2 files transferred

lftp 192.168.179.100:/pub> mput /etc/f*

1688 bytes transferred                                

Total 4 files transferred

 

lftp下載目錄

[root@localhost pub]# mkdir -p test  --服務端創建目錄

[root@localhost pub]# ls

test

lftp 192.168.179.100:/pub> mirror test  --客戶端下載該目錄

Total: 1 directory, 0 files, 0 symlinks

lftp 192.168.179.100:/pub> rmdir test  --刪除目錄

rmdir ok, `test' removed

lftp 192.168.179.100:/pub> rm file1  --刪除文件

rm ok, `file1' removed

 


本地用戶訪問ftp服務器

修改配置文件,可以設置不讓匿名用戶登錄,只能本地用戶登錄:

重啓服務。然後再次訪問:

vim /etc/vsftpd/vsftpd.conf

anonymous_enable=NO

local_enable=YES

 

[root@localhost ~]# lftp 192.168.179.100  --這個登錄,表示匿名用戶已經無法登錄了。

lftp 192.168.179.100:~> ls

`ls' at 0 [Sending commands...]

 

 

如果使用本地用戶訪問,需要服務端有對應的用戶存在才行。

[root@localhost ~]# id jerry  --服務端的jerry用戶

uid=1000(jerry) gid=1000(jerry) groups=1000(jerry)

[root@localhost ~]# lftp [email protected] --這樣登入到的是服務端jerry的家目錄

Password:

lftp [email protected]:~> pwd   --並不是在服務端的匿名用戶的pub目錄,是在服務端jerry的家目錄 (上傳和下載和匿名用戶一樣的使用方法)

ftp://[email protected]/%2Fhome/jerry

 

lftp [email protected]:/var/ftp/pub> cd /  --可以看到jerry用戶可以隨意切換到服務端的任何目錄下面,這是不安全的,最好是限制只能在家目錄活動,限制系統用戶越獄:在安裝vsftpd後不做配置的話,系統用戶是可以向上切換到其他目錄的

 

cd ok, cwd=/

lftp [email protected]:/> ls

lrwxrwxrwx    1 0        0               7 Mar 01 19:43 bin -> usr/bin

dr-xr-xr-x    5 0        0            4096 Mar 01 19:52 boot

drwxr-xr-x   19 0        0            3200 Mar 05 10:05 dev

drwxr-xr-x   81 0        0            8192 Mar 05 12:39 etc

drwxr-xr-x    3 0        0              19 Mar 05 12:39 home

 

限制系統用戶越獄:在安裝vsftpd後不做配置的話,系統用戶是可以向上切換到其他目錄的。(限制在家目錄,不讓其看其他目錄信息)

vim /etc/vsftpd/vsftpd.conf

chroot_local_user=YES

chroot_list_enable=NO

allow_writeable_chroot=YES

# chroot_local_user: 是否將所有用戶限制在主目錄,YES爲啓用,NO禁用.(該項默認值是NO)

# chroot_list_enable: 是否啓動限制用戶(特例)的名單 YES爲啓用,NO禁用(包括註釋掉也爲禁用)

#allow_writeable_chroot=YES:允許用戶具有主目錄寫權限

 

如果不加allow_writeable_chroot=YES這個參數重啓服務客戶端會報錯,允許對家目錄的寫,即可以上傳

[root@localhost ~]# lftp [email protected]

Password:

lftp [email protected]:~> ls        

ls: Login failed: 500 OOPS: vsftpd: refusing to run with writable root inside chroot()

 

 

(1)如果想全部限制,所有用戶都不能切換家目錄

chroot_local_user=YES

chroot_list_enable=NO

allow_writeable_chroot=YES

 

(2)如果想讓部分用戶有切換的家目錄的權限,則需要開啓限制:

chroot_local_user=YES

chroot_list_enable=YES

chroot_list_file=/etc/vsftpd/chroot_list

chroot_list中寫上要放行用戶

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