1. 程式人生 > >linux入門系列15--檔案傳輸之vsftp服務

linux入門系列15--檔案傳輸之vsftp服務

前面的系列文章基本講完了linux管理相關的基礎知識,從本篇開始講解centos7中服務程式的部署和配置,以便為外部提供各種服務。

日常工作和娛樂中,我們所需的各種資源都離不開網路以及各種服務,我們通過網路獲取部署在其他伺服器上的各種服務資源,這些服務包括檔案服務、郵件服務、媒體服務等等。

一般情況下,我們使用計算機上網的一個重要目的就是為了獲取資料,而檔案傳輸則是獲取資料的方式。因此,我們首先來了解下linux中檔案傳輸相關的知識。

一、檔案傳輸協議FTP

1.1 FTP產生背景

“無規矩不成方圓”,這說明了規則的重要性。同樣,當今的網際網路由成千上萬臺機器組成,這些機器包括個人計算機、工作站、伺服器、巨型機等各種形形色色的裝置,並且這些裝置中使用的作業系統還不一樣,有的可能是用的windows,而有的則是Linux或其他系統。

要在這麼紛繁複雜的裝置之間傳輸檔案,那就必須要有一定的規則,大家都按規則辦事,傳輸的檔案才能相互識別,達到正確傳遞資訊的目的。在這種背景下,為了解決檔案傳輸的問題,檔案傳輸協議應運而生。

1.2 FTP相關概念

檔案傳輸協議(File Transfer Protocol,FTP),是一種在網際網路中進行檔案傳輸的協議,基於客戶端/伺服器模式,預設使用 20、21 號埠,其中20埠是資料埠用於進行資料傳輸,21埠為命令埠,用於接收客戶端發出的相關FTP命令和引數。

本系列文章第三篇提到的所有工具都可以實現在Windows中遠端到Linux主機並上傳下載檔案,並且在Linux主機之間也可以通過scp命令上傳檔案,那為什麼還需要FTP呢?個人認為應該是因為FTP一般搭建與內網之中,有具有容易搭建、方便管理的特點,並且一些FTP客戶端工具還具有檔案多點下載以及斷點續傳等功能,這些是scp做不到的。

FTP是C/S架構,也就是基於客戶端/伺服器的模式,FTP伺服器是按照FTP協議在網際網路上提供檔案儲存和訪問服務的主機,FTP客戶端則是向伺服器傳送連線請求,以建立資料傳輸鏈路的主機。

FTP協議的傳輸拓撲結構如下:

FTP協議工作模式有兩種:主動模式和被動模式,其中被動模式是預設的工作模式

  • 主動模式:FTP 伺服器主動向客戶端發起連線請求。

  • 被動模式:FTP 伺服器等待客戶端發起連線請求。

由於FTP一般部署在企業內網,如果開啟並配置了防火牆,有時候需要將FTP的工作模式設定為主動模式,才可以傳輸資料。

二、Linux下基於FTP協議工具

2.1 vsftpd服務安裝

vsftpd(very secure ftp daemon)非常安全的FTP守護程序,是一款執行在Linux系統上的免費開源的FTP服務端程式。其主要特點是:安全性高、傳輸速度快、支援虛擬使用者驗證。

預設情況Centos7中是沒有安裝vsftpd的,因此需要手動通過yum倉庫安裝,根據前文的講解可以用光碟資源自帶的yum源,也可以直接配置外網源。本例採用預設的外網yum源進行安裝。

[root@origin ~]# rpm -q vsftpd
package vsftpd is not installed
[root@origin ~]# yum install vsftpd
Loaded plugins: fastestmirror, langpacks
...省略部分內容
Installed size: 353 k
Is this ok [y/d/N]: y
Downloading packages:
vsftpd-3.0.2-25.el7.x86_64.rpm                             | 171 kB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : vsftpd-3.0.2-25.el7.x86_64                                   1/1 
  Verifying  : vsftpd-3.0.2-25.el7.x86_64                                   1/1 
Installed:
  vsftpd.x86_64 0:3.0.2-25.el7                                                 
Complete!
[root@origin ~]# rpm -q vsftpd     
vsftpd-3.0.2-25.el7.x86_64
[root@origin ~]# 

通過rpm命令檢視是否已經安裝過vsftpd,如果沒有安裝則通過yum install命令安裝即可,安裝過程中需要按提示輸入y繼續下載並安裝。

注意:從現在開始,凡是涉及到服務的配置,就要考慮防火牆和SELinux的因素,很多教程和書籍上都是直接關閉防火牆和selinux,雖然這樣在學習階段可以避免干擾,但是這樣非常不安全。另外凡是配置了服務,都要加入開機啟動中,讓其每次重啟自動生效。

安裝vsftpd服務後,會在/etc下自動生成配置檔案

[root@ftpserver ~]# ll /etc/vsftpd/
total 20
-rw-------. 1 root root  125 Oct 31  2018 ftpusers
-rw-------. 1 root root  361 Oct 31  2018 user_list
-rw-------. 1 root root 5116 Oct 31  2018 vsftpd.conf
-rwxr--r--. 1 root root  338 Oct 31  2018 vsftpd_conf_migrate.sh

各個檔案的解釋

檔名 作用
vsftpd.conf 主配置檔案
ftpusers 黑名單
vsftpd_conf_migrate.sh 遷移指令碼
user_list 使用者列表,與userlist_enbale和userlist_deny選項密切相關

檢視主配置檔案vsftpd.conf內容可以看到各項配置資訊,需要根據實際情況來進行配置,其中主要的引數和作用如下:

引數 作用
listen=[YES|NO] 是否以獨立執行的方式監聽服務
listen_address=IP地址 設定要監聽的IP地址
listen_port=21 設定FTP服務的監聽埠
download_enable=[YES|NO] 是否允許下載檔案
userlist_enable=[YES|NO] 設定使用者列表為“允許”操作
userlist_deny=[YES|NO] 設定使用者列表為“禁止”操作
max_clients=0 最大客戶端連線數,0為不限制
max_per_ip=0 同一IP地址的最大連線數,0為不限制
anonymous_enable=[YES|NO] 是否允許匿名使用者訪問
anon_upload_enable=[YES|NO] 是否允許匿名使用者上傳檔案
anon_umask=022 匿名使用者上傳檔案的umask值
anon_root=/var/ftp 匿名使用者的 FTP 根目錄
anon_mkdir_write_enable=[YES|NO] 是否允許匿名使用者建立目錄
anon_other_write_enable=[YES|NO] 是否開放匿名使用者的其他寫入許可權(包括重新命名、刪 除等操作許可權)
anon_max_rate=0 匿名使用者的最大傳輸速率(位元組/秒),0 為不限制
local_enable=[YES|NO] 是否允許本地使用者登入FTP
local_umask=022 本地使用者上傳檔案的umask值
local_root=/var/ftp 本地使用者的 FTP 根目錄
chroot_local_user=[YES|NO] 是否將使用者許可權禁錮在FTP目錄,以確保安全
local_max_rate=0 本地使用者最大傳輸速率(位元組/秒),0為不限制

不用完全記住這些引數,需要時再來檢視即可。

2.2 ftp客戶端安裝

ftp是Linux系統中以命令列介面的方式來管理FTP傳輸服務的客戶端工具。預設也是沒有安裝的,需要手動安裝。

[root@origin ~]# rpm -q ftp
package ftp is not installed
[root@origin ~]# yum install ftp
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
...省略部分內容
Total download size: 61 k
Installed size: 96 k
Is this ok [y/d/N]: y
Downloading packages:
ftp-0.17-67.el7.x86_64.rpm                                 |  61 kB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : ftp-0.17-67.el7.x86_64                                       1/1 
  Verifying  : ftp-0.17-67.el7.x86_64                                       1/1 
Installed:
  ftp.x86_64 0:0.17-67.el7                                                     
Complete!
[root@origin ~]# rpm -q ftp     
ftp-0.17-67.el7.x86_64
[root@origin ~]# 

通過yum install命令即可安裝成功,ftp客戶端工具安裝成功後,接下來就是及操作下檔案傳輸的功能。

2.3 vsftpd三種認證模式

vsftpd作為更加安全的檔案傳輸的服務程式,允許使用者以三種認證模式登入到FTP伺服器上:匿名開放模式、本地使用者模式、虛擬使用者模式。實際生產環境中虛擬使用者模式用的較多。

  • 匿名開放模式:任何人都可以無需密碼驗證而直接登入到FTP伺服器,是一種最不安全的認證模式。

  • 本地使用者模式:是通過Linux系統本地的賬戶密碼資訊進行認證的模式。如果被黑客破解了賬戶資訊,就可以暢通無阻地登入FTP伺服器,從而完全控制整臺伺服器。

  • 虛擬使用者模式:需要為FTP服務單獨 建立使用者資料庫檔案,虛擬出用來進行口令驗證的賬戶資訊,而這些賬戶資訊在 伺服器系統中實際上是不存在的,僅供 FTP 服務程式進行認證使用。這樣,即使 黑客破解了賬戶資訊也無法登入伺服器,從而有效降低了破壞範圍和影響。是三種模式中最安全的一種認證模式

2.4 匿名開放模式

匿名開放模式是最不安全的一種認證模式,任何人都可以無需密碼驗證而直接登入到FTP伺服器。針對匿名使用者放開這些許可權會帶來潛在危險,生產環境不建議這樣做,因此一般不會用匿名模式,儘管如此,但本節還是實際演示一下。

按之前的方法克隆準備2臺虛擬機器:一臺主機名為:ftpserver ,用於安裝vsftpd服務;另外一臺主機名為:ftp,用於安裝ftp客戶端。

vsftpd 服務程式預設開啟了匿名開放模式,我們需要做的就是開放匿名使用者的上傳、下 載檔案的許可權,以及讓匿名使用者建立、刪除、更名檔案的許可權。

針對匿名模式的配置主要有如下幾個引數:

引數 作用
anonymous_enable=YES 允許匿名訪問模式
anon_umask=022 匿名使用者上傳檔案的umask值
anon_upload_enable=YES 允許匿名使用者上傳檔案
anon_mkdir_write_enable=YES 允許匿名使用者建立目錄
anon_other_write_enable=YES 允許匿名使用者修改目錄名稱或刪除目錄

配置匿名模式主要步驟如下:

2.4.1 伺服器配置vsftp服務,開啟匿名模式

只需要按照上表查詢主配置檔案中對應的引數,如果是注射掉的就把注射去掉,如果是沒有的就新加即可。

但是考慮到該配置檔案裡邊的註釋資訊是在是太多了,影響配置,乾脆我們把與配置無關的注射直接去掉。

[root@ftpserver ~]# mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak
[root@origin ~]# grep -v "#" /etc/vsftpd/vsftpd.conf.bak >/etc/vsftpd/vsftpd.conf
[root@ftpserver ~]# cat /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
[root@ftpserver ~]# 

上邊示例中,我們先把主配置檔案備份,然後通過grep命令匹配出非註釋的引數,然後再重定向將其寫入到主配置檔案。這樣配置檔案看起來內容就簡潔多了。

然後我們將上表的引數配置到主配置檔案即可:

[root@origin ~]# vi /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
anon_umask=022
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

實際上第一行已經預設開啟了,等於是新增第二到第五行即可。儲存並退出,這樣就完成了vsftpd服務的配置。

配置完成後重啟服務,並新增到開機啟動。配置並開啟vsftp服務後,就可以在客戶端執行ftp命令連線到遠端的FTP伺服器了。

[root@ftpserver ~]# systemctl restart vsftpd
[root@ftpserver ~]# systemctl enable vsftpd
ln -s '/usr/lib/systemd/system/vsftpd.service' '/etc/systemd/system/multi-user.target.wants/vsftpd.service'
[root@ftpserver ~]# 
[root@ftpserver ~]# ll /var/ftp/pub/
total 0
[root@ftpserver ~]# echo "ftp server content">/var/ftp/pub/ftpserver.txt
[root@ftpserver ~]# ll /var/ftp/pub/
total 4
-rw-r--r--. 1 root root 19 Jan 13 23:09 ftpserver.txt
[root@ftpserver ~]# 

在/var/ftp/pub目錄下準備檔案,供ftp客戶端下載。因為客戶端連線到FTP伺服器後,預設訪問的是伺服器的/var/ftp目錄(該目錄下還有一個pub目錄)。

2.4.2 客戶機使用ftp服務下載檔案

在vsftpd服務程式的匿名開放認證模式下,其賬戶統一為 anonymous,密碼為空

[root@ftp ~]# ftp 192.168.78.101
ftp: connect: No route to host
ftp>

發現報錯了,此時我們首先應該想到的就是ftpserver伺服器的防火牆,因此我們關閉防火牆來驗證一下

[root@ftpserver ~]# firewall-cmd --state
running
[root@ftpserver ~]# systemctl stop firewalld.service 
[root@ftpserver ~]# firewall-cmd --state
not running
[root@ftpserver ~]# 

在ftpserver伺服器關閉防火牆後,在嘗試連線就可以正常連線並下載伺服器上的ftpserver.txt檔案了。

[root@ftp ~]# ftp 192.168.78.101
Connected to 192.168.78.101 (192.168.78.101).
220 (vsFTPd 3.0.2)
Name (192.168.78.101:root): anonymous
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd pub
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (192,168,78,101,132,61).
150 Here comes the directory listing.
-rw-r--r--    1 0        0              19 Jan 13 15:09 ftpserver.txt
226 Directory send OK.
ftp> get ftpserver.txt
local: ftpserver.txt remote: ftpserver.txt
227 Entering Passive Mode (192,168,78,101,115,121).
150 Opening BINARY mode data connection for ftpserver.txt (19 bytes).
226 Transfer complete.
19 bytes received in 7e-05 secs (271.43 Kbytes/sec)
ftp> 

這樣就把ftpserver伺服器上的ftpserver.txt檔案下載到了當前使用者的家目錄下

[root@ftp ~]# ll
-rw-r--r--. 1 root root   19 Jan 13 23:20 ftpserver.txt
...省略部分無關的檔案
[root@ftp ~]# cat ftpserver.txt 
ftp server content
[root@ftp ~]# 

到此我們就可以使用ftp下載伺服器上的檔案了。但是有個問題必須的說明一下,前邊遇到ftp連線問題時,我們直接關閉了伺服器上的防火牆。但這是非常不安全的,生產環境中一般都是要求開啟防火牆。

那麼防火牆是如何阻止了ftp客戶端連線呢?其實就是因為firewall防火牆預設禁止了ftp傳輸的埠號,因此可以清除防火牆的預設策略

[root@ftpserver ~]# systemctl start firewalld.service 
[root@ftpserver ~]# firewall-cmd --state              
running
[root@ftpserver ~]# iptables -F
[root@ftpserver ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@ftpserver ~]# 

清除防火牆預設策略並儲存,這樣即使伺服器不關閉防火牆也可以正常使用功能ftp功能下載檔案了。

2.4.3 通過ftp客戶端在伺服器上建立目錄

接下來我們切換到pub目錄,並嘗試建立一個目錄test

ftp> cd pub
250 Directory successfully changed.
ftp> mkdir test
550 Create directory operation failed.
ftp> 

好神奇,居然報錯了。大家不用擔心,學習階段要的就是這種效果,遇到的錯誤越多,越能快速積累經驗,並建立一套分析問題和解決問題的邏輯思維套路。

分析一下,我們第一步時已經在主配置檔案中添加了執行匿名使用者建立目錄和寫入檔案的許可權,但為何還是會報錯呢?既然排除了是使用者許可權的問題,那麼是不是/var/ftp這個目錄的許可權問題呢?

[root@ftpserver ~]# ll -d /var/ftp/pub
drwxr-xr-x. 2 root root 26 Jan 13 23:09 /var/ftp/pub
[root@ftpserver ~]# chown -Rf ftp /var/ftp/pub
[root@ftpserver ~]# ll -d /var/ftp/pub        
drwxr-xr-x. 2 ftp root 26 Jan 13 23:09 /var/ftp/pub
[root@ftpserver ~]# ll -d /var/ftp/
drwxr-xr-x. 3 root root 16 Jan 13 22:59 /var/ftp/

通過檢視,果然發現/var/ftp目錄只有root使用者才有寫入的許可權,因此我們直接把pub目錄所有者改為ftp使用者,讓ftp使用者擁有寫入的許可權,我們再次來試下看能否建立資料夾

ftp> mkdir test
550 Create directory operation failed.
ftp> 

奇怪,依然報錯。前面排除了使用者許可權、目錄許可權之外,我們應該想到很有可能是SELinux造成的,因此我們關閉SELinux再來驗證一下

[root@ftpserver ~]# setenforce 0
[root@ftpserver ~]# 

關閉SeLinux後後,再去建立目錄,發現建立成功

ftp> mkdir test
257 "/pub/test" created
ftp> 

因此,說明不僅要讓pub目錄的屬主擁有寫入許可權,並且同時還要關閉SELinux才能建立目錄。

但是實際生產環境中不建議關閉SELinux,所以我們要弄清楚究竟SELinux哪個配置影響了

[root@ftpserver ~]# setenforce 1
[root@origin ~]# getenforce 
Enforcing
[root@origin ~]# getsebool -a | grep ftp
ftp_home_dir --> off
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off
[root@origin ~]# 

我們檢視下SELinux狀態為開啟,檢視與ftp域相關的策略,其中有一條ftpd_full_access --> off其實就是這條規則導致了操作失敗。

修改該策略,並使用-P引數使其永久生效。

[root@origin ~]# setsebool -P ftpd_full_access=on

再次登入建立目錄,發現成功了

ftp> mkdir test2
257 "/pub/test2" created
ftp> 

因此遇到問題,從使用者許可權、目錄許可權、網路防火牆、SELinux服務幾個方面去逐個排查。

2.5 本地使用者模式

本地模式比匿名模式更加安全,現在繼續在前面的虛擬機器中做實驗,開始之前先關閉上一實驗中開啟的匿名模式。

[root@ftpserver ~]# vim /etc/vsftpd/vsftpd.conf
[root@ftpserver ~]# cat /etc/vsftpd/vsftpd.conf
anonymous_enable=NO
#anon_umask=022
#anon_upload_enable=YES
#anon_mkdir_write_enable=YES
#anon_other_write_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
[root@ftpserver ~]# 

將anonymous_enable=YES改為NO,即關閉了本地模式,此時就不能在使用匿名模式登陸了。

另外前面新加的與匿名配置引數可以保留,也可以直接刪除,只要總開關anonymous_enable關閉了就禁用了匿名登入。

現在開始演示本地使用者模式操作ftp。

其實從前面的配置檔案可以看出,vsfftpd服務預設既開啟了匿名模式也開啟了本地使用者模式(local_enable=YES)。以下幾個引數是本地使用者模式相關的配置

引數 作用
local_enable=YES 允許本地使用者模式
write_enable=YES 設定可寫許可權
local_umask=022 本地使用者模式建立檔案的 umask 值
userlist_enable=YES 啟用“禁止使用者名稱單”,名單檔案為 ftpusers 和 user_list
userlist_deny=YES 開啟使用者作用名單檔案功能
2.5.1 配置本地使用者模式

其實vsftpd預設已經開啟了本地使用者模式,因此沒什麼可以配置的,檢視下配置檔案:

[root@ftpserver ~]# cat /etc/vsftpd/vsftpd.conf
anonymous_enable=NO
#anon_umask=022
#anon_upload_enable=YES
#anon_mkdir_write_enable=YES
#anon_other_write_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
[root@ftpserver ~]# 

參考上表,與本地使用者配置相關的主要引數為:local_enable=YES,write_enable=YES,local_umask=022。如果你的配置檔案預設沒配置這幾個項則手動配置一下即可。

注意,如果修改了配置需要從新啟動下vsftpd服務

[root@ftpserver ~]# systemctl restart vsftpd
[root@ftpserver ~]# systemctl enable vsftpd
[root@ftpserver ~]# 

另外再建立一個普通使用者ftptest並設定密碼,用於客戶端登入

[root@ftpserver home]# useradd ftptest
[root@ftpserver home]# passwd ftptest
Changing password for user ftptest.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@ftpserver home]# 

這樣ftp伺服器就準備好了。

特別說明:根據前文的經驗,需要按前文方法設定防火牆和SELinux策略客戶端才能使用,由於我們是沿用之前的配置,之前已經配置好了,所以這裡不用配置。預設情況下防火牆將限制登入、SELinux則限制建立目錄等操作。

2.5.2 ftp客戶端操作伺服器

現在就可以直接採用ftpserver伺服器上的普通賬戶ftptest進行檔案操作了。

[root@ftp ~]# ftp 192.168.78.101
Connected to 192.168.78.101 (192.168.78.101).
220 (vsFTPd 3.0.2)
Name (192.168.78.101:root): ftptest
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> mkdir test
257 "/home/ftptest/test" created
ftp> rename test test1
350 Ready for RNTO.
250 Rename successful.
ftp> ls
227 Entering Passive Mode (192,168,78,101,106,178).
150 Here comes the directory listing.
drwxr-xr-x    2 1001     1001            6 Jan 14 09:01 test1
226 Directory send OK.
ftp> rmdir test1
250 Remove directory operation successful.
ftp> exit
221 Goodbye.
[root@ftp ~]# 

案例中成功通過ftp伺服器中的ftptest使用者對目錄進行建立、重新命名、刪除等操作。並且ftp客戶端建立的目錄,在ftpserver伺服器對應的使用者目錄下也可以查到新建的目錄。

因此可以看到,本地使用者模式相對匿名模式,配置更簡單一些。但是大家想過為啥ftptest使用者登入後就可以對目錄進行建立、刪除等操作嗎?那是因為本地使用者模式登入FTP伺服器後,預設訪問的是該使用者的家目錄,即/home/ftptest目錄,它的預設所有者、所屬組都是該使用者自己,因此不存在寫入許可權不足的情況。

2.5.3 本地使用者登入模式之填坑

如果你是按照我的文章步驟操作,一定會很順暢,但是如果一開始並不是新建ftptest使用者來登入,而是直接使用root使用者來進行遠端登入,那就有個坑需要注意一下。

在客戶端用root登入試試看

[root@ftp ~]# ftp 192.168.78.101
Connected to 192.168.78.101 (192.168.78.101).
220 (vsFTPd 3.0.2)
Name (192.168.78.101:root): root
530 Permission denied.
Login failed.
ftp> 

哦豁,目前的系統root是擁有最高許可權的,居然被拒了,而前邊建立的普通使用者ftptest使用者居然可以正常使用ftp功能!我就遇到過,哈哈哈哈,百思不得解。

如果是初次接觸真可能懷疑是自己哪個步驟操作錯了,其實這裡有一個規則:

vsftpd服務程式配置目錄存放著兩個“使用者名稱單”的檔案:ftpusers、user_list。原因就在這2個檔案,只要這2個檔案中存在的使用者,就不允許登入FTP伺服器上。

來看看這2個檔案內容:

[root@ftpserver ~]# cat /etc/vsftpd/user_list 
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
[root@ftpserver ~]# cat /etc/vsftpd/ftpusers 
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
[root@ftpserver ~]# 

果然裡邊存在root使用者,這正是vsftpd服務為了保證伺服器安全預設就禁止了root和大部分系統使用者登入ftp。這樣做的好處是避免黑客通過FTP伺服器對root密碼進行暴力破解。

而為啥之前建立的ftptest使用者可以登入ftp呢,那是因為建立的使用者並沒有被預設寫入這2個檔案。如果確保在生產環境中直接使用root使用者對安全性沒有影響,則直接在這2個檔案中把root刪除即可,否則請使用這2個檔案中不存在的普通使用者來登入ftp伺服器。

刪除這2個檔案中的root使用者,然後重啟vsftpd服務,再次使用root登入ftp即可成功操作。

2.6 虛擬使用者模式

再來看一種更加安全的認證模式即虛擬使用者模式,因為更加安全所以配置起來稍微要麻煩一些。

我們仍然在之前的機器上繼續操作,步驟步驟如下:

2.6.1 建立使用者資料庫檔案

在ftpserver伺服器中的vsftpd配置目錄下建立包含使用者資訊的檔案vuser.list(檔名字任意取)

[root@ftpserver ~]# cd /etc/vsftpd/
[root@ftpserver vsftpd]# ls
ftpusers  user_list  vsftpd.conf  vsftpd.conf.bak  vsftpd_conf_migrate.sh
[root@ftpserver vsftpd]# vim vuser.list
vuser1
123456
vuser2
123456

vuser.list檔案中包含vuser1和vuser2兩個使用者,密碼均為123456。檔案中奇數行表示賬號名,偶數行表示密碼。

接下來要將vuser.list檔案轉換為vsftpd服務程式能直接載入的格式,轉換格式使用db_load命令,該命令用雜湊演算法將原始的明文資訊檔案轉換為資料庫檔案。

在使用db_load命令前,檢視該命令是否安裝

[root@ftpserver vsftpd]# db_load
usage: db_load [-nTV] [-c name=value] [-f file]
        [-h home] [-P password] [-t btree | hash | recno | queue] db_file
usage: db_load -r lsn | fileid [-h home] [-P password] db_file
[root@ftpserver vsftpd]# 

出現該命令的用法,說明預設已經安裝了。

如果未安裝需要手動安裝,該命令包含在db4包中,使用命令yum -y install db4安裝即可。

既然安裝了db_load命令,那就使用它來轉換使用者檔案格式

[root@ftpserver vsftpd]# db_load -T -t hash -f vuser.list vuser.db
[root@ftpserver vsftpd]# file vuser.db 
vuser.db: Berkeley DB (Hash, version 9, native byte-order)
[root@ftpserver vsftpd]# ll vuser.db 
-rw-r--r--. 1 root root 12288 Jan 14 21:16 vuser.db
[root@ftpserver vsftpd]# chmod 600 vuser.db 
-rw-------. 1 root root 12288 Jan 14 21:16 vuser.db
[root@ftpserver vsftpd]# rm -rf vuser.list 
[root@ftpserver vsftpd]# 

通過db_load命令就把明文的vuser.list檔案轉換為了vuser.db格式的檔案,並通過chmod命令降低該檔案的許可權,從而避免其他人檢視到該資料庫檔案的內容,最後直接把明文的vuser.list檔案刪除。

2.6.2 建立PAM檔案

PAM(Pluggable Authentication Module)是一種認證機制,它將服務與認證方式分開,使得系統管理員可以輕易的調整服務程式的認證方式,而不必對應用程式進行任何修改。它涉及的內容挺多的,此處僅僅先了解概念即可。

在ftpserver伺服器上新建一個PAM檔案vsftpd.vu(檔名可以任意)用於虛擬使用者認證,檔案內的db引數為上一步使用功能db_load命令生成的資料庫檔案的路徑,但不用寫字尾名

[root@ftpserver vsftpd]# vim /etc/pam.d/vsftpd.vu
auth required pam_userdb.so db=/etc/vsftpd/vuser
account required pam_userdb.so db=/etc/vsftpd/vuser
2.6.3 建立本地使用者

既然是虛擬使用者認證,為啥還需要建立本地使用者呢?可以簡單理解為虛擬指定一個ftp檔案儲存的根目錄,當虛擬使用者登入ftp伺服器後預設訪問的就是這個目錄。

由於Linux中一切檔案都有所有者和所屬組,假設前文建立的虛擬使用者vuser1建立了一個檔案,但是系統中卻沒有vuser1這個使用者,就會導致檔案的許可權出現錯誤。因此需要建立一個本地使用者來對映虛擬使用者,讓虛擬使用者預設登入到與之有對映關係的這個系統本地使用者的家目錄中,虛擬使用者建立的檔案的屬性也都歸屬於這個系統本地使用者,從而避免 Linux 系統無法處理虛擬使用者所建立檔案的屬性許可權。

由於ftp目錄中的檔案可能經常變化,因此我們新建一個本地使用者virtual並指定其家目錄為/var/ftpdir目錄。同時我們禁用此本地使用者登入FTP伺服器,從而避免黑客利用該本地使用者登入系統。禁止與虛擬使用者關聯的本地使用者並不會影響虛擬使用者的登入。

[root@ftpserver vsftpd]# useradd -d /var/ftpdir -s /sbin/nologin virtual
[root@ftpserver vsftpd]# ll -d /var/ftpdir/
drwx------. 3 virtual virtual 74 Jan 14 21:46 /var/ftpdir/
[root@ftpserver vsftpd]# chmod -Rf 755 /var/ftpdir/
[root@ftpserver vsftpd]# ll -d /var/ftpdir/        
drwxr-xr-x. 3 virtual virtual 74 Jan 14 21:46 /var/ftpdir/
[root@ftpserver vsftpd]# 

修改目錄許可權,讓其他人可讀。

2.6.4 vsftpd主配置檔案配置

配置vsftpd主配置檔案,指定之前建立的PAM認證檔案以及與虛擬使用者關聯的本地使用者。

[root@ftpserver vsftpd]# vim /etc/vsftpd/vsftpd.conf
anonymous_enable=NO
#anon_umask=022
#anon_upload_enable=YES
#anon_mkdir_write_enable=YES
#anon_other_write_enable=YES
local_enable=YES
guest_enable=YES
guest_username=virtual
allow_writeable_chroot=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd.vu
userlist_enable=YES
tcp_wrappers=YES

實際就是在原有基礎上新增:guest_enable=YES、guest_username=virtual、allow_writeable_chroot=YES,並修改pam_service_name=vsftpd.vu為之前建立的pam認證檔案。另外注意,必須開啟本地模式(local_enable=YES)。

虛擬使用者認證相關引數及作用如下:

引數 作用
anonymous_enable=NO 禁止匿名開放模式
local_enable=YES 允許本地使用者模式
guest_enable=YES 開啟虛擬使用者模式
guest_username=virtual 指定虛擬使用者賬戶
pam_service_name=vsftpd.vu 指定PAM檔案,存放於/etc/pam.d/目錄下,預設是vsftpd檔案
allow_writeable_chroot=YES 允許對禁錮的FTP根目錄執行寫入操作,而且不拒絕使用者的登入請求
2.6.5 虛擬使用者許可權設定

假設我們要區別對待vuser1和vuser2,只允許vuser1檢視檔案,而允許vuser2上傳、建立、修改、檢視、刪除檔案。這種需求在企業真實環境中是常見的,我們可以通過vsftpd伺服器程式來實現。

[root@ftpserver vsftpd]# mkdir /etc/vsftpd/vusers_dir
[root@ftpserver vsftpd]# cd vusers_dir/
[root@ftpserver vusers_dir]# touch vuser1
[root@ftpserver vusers_dir]# vim vuser2
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES

只需要在vsftpd配置目錄下新建vusers_dir目錄(名字可以任意取),在目錄中分別對應這2個使用者建立檔案並寫入許可權即可。

同時修改vsftpd主配置檔案,過新增 user_config_dir引數來定義這兩個虛擬使用者不同許可權的配置檔案所存放的路徑。

[root@ftpserver vsftpd]# vim /etc/vsftpd/vsftpd.conf
...省略原有內容
user_config_dir=/etc/vsftpd/vusers_dir

配置完成之後重啟vsftpd服務。

2.6.6 驗證不同虛擬使用者設定的許可權是否生效

此時我們就可以使用虛擬使用者vuser1和vuser2登入ftp進行測試了

由於我們按照之前的基礎上進行演示,之前已經配置好了防火牆和SELinux相關策略,因此這裡無需配置(實際操作時要根據實際情況進行,防火牆和SELinux會對ftp客戶端的操作造成影響)。

使用vuser1登入測試驗證是否有建立目錄許可權

[root@ftp ~]# ftp 192.168.78.101
Connected to 192.168.78.101 (192.168.78.101).
220 (vsFTPd 3.0.2)
Name (192.168.78.101:root): vuser1
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> mkdir test
550 Permission denied.
ftp> exit
221 Goodbye.
[root@ftp ~]#

正如之前設定一樣,vuser1只有檢視許可權,沒有建立目錄的許可權。

再來看看vuser2

[root@ftp ~]# ftp 192.168.78.101
Connected to 192.168.78.101 (192.168.78.101).
220 (vsFTPd 3.0.2)
Name (192.168.78.101:root): vuser2
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> mkdir vuser2
257 "/vuser2" created
ftp> rmdir vuser2
250 Remove directory operation successful.
ftp> exit
221 Goodbye.
[root@ftp ~]# 

正如需求一致,vuser2具備建立和刪除目錄的許可權。這樣就可以根據實際需要靈活配置不同使用者的許可權。

2.6.7 其他注意事項及常見錯誤

(1)配置了虛擬使用者登入後,本地使用者登入就會失效

[root@ftp ~]# ftp 192.168.78.101
Connected to 192.168.78.101 (192.168.78.101).
220 (vsFTPd 3.0.2)
Name (192.168.78.101:root): root
331 Please specify the password.
Password:
530 Login incorrect.
Login failed.
ftp> exit
221 Goodbye.
[root@ftp ~]# 

(2)2.6.4節中的vsftpd主配置檔案中必須要開啟本地登入模式

如果不開,將報錯,提示本地模式和匿名模式必須開啟一個,顯然虛擬使用者模式就是為了提高安全,因此我們不開匿名模式而開啟本地模式。

[root@ftp ~]# ftp 192.168.78.101
Connected to 192.168.78.101 (192.168.78.101).
500 OOPS: vsftpd: both local and anonymous access disabled!
ftp> exit
[root@ftp ~]# 

(3)2.6.4節中的主配置檔案引數allow_writeable_chroot=YES必須開啟

如果不開將報錯

[root@ftp ~]# ftp 192.168.78.101
Connected to 192.168.78.101 (192.168.78.101).
220 (vsFTPd 3.0.2)
Name (192.168.78.101:root): vuser1
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> exit
[root@ftp ~]# 

(3)防火牆影響ftp登入不了

預設情況防火牆策略阻止了ftp埠,ftp客戶端登入將得到如下錯誤

[root@ftp ~]# ftp 192.168.78.101
ftp: connect: No route to host
ftp> exit

此時需要在ftp伺服器上執行:iptables -F

(4)SELinux阻止檔案建立

本來vuser2是有建立檔案許可權的,但是由於受到預設SELinux策略影響,將不能建立檔案

[root@ftp ~]# ftp 192.168.78.101
Connected to 192.168.78.101 (192.168.78.101).
220 (vsFTPd 3.0.2)
Name (192.168.78.101:root): vuser2
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> mkdir vuser2
550 Create directory operation failed.
ftp> exit
221 Goodbye.
[root@ftp ~]# 

此時需要在ftp伺服器執行:setsebool -P ftpd_full_access=off命令即可。

三、簡單檔案傳輸協議TFTP

3.1 TFTP概述

TFTP(Trivial File Transfer Protocol)簡單檔案傳輸協議,是一種基於UDP協議在客戶端和伺服器之間進行簡單檔案傳輸的協議。可以認為它是FTP協議的簡化版本。

3.2.1 TFTP缺點

TFTP的命令功能不如FTP服務強大,不能遍歷目錄,在安全性方比FTP差。傳輸檔案採用 UDP 協議,埠號為 69,檔案傳輸過程不如FTP協議可靠。

3.2.2 TFTP優點

TFTP不需要客戶端的許可權認證, 減少了無謂的系統和網路頻寬消耗,因此在傳輸瑣碎(trivial)不大的檔案時,效率更高。

3.2 TFTP操作案例

3.2.1 TFTP安裝並啟動

我們還是繼續使用之前的兩臺虛擬機器,在主機ftpserver上安裝tftpsever服務,在主機ftp上安裝tftp服務。

(1)tftp伺服器安裝

[root@ftpserver ~]# rpm -q tftp-server
package tftp-server is not installed
[root@ftpserver ~]# yum install tftp-server
...省略中間內容
Installed:
  tftp-server.x86_64 0:5.2-22.el7                                           
Complete!
[root@ftpserver ~] 

在RHEL7系統中,TFTP服務是使用 xinetd 服務程式來管理的,在安裝TFTP軟體包後,還需要在 xinetd服務程式中將其開啟。

配置之前先看tftp伺服器是否安裝xinetd服務

[root@ftpserver ~]# systemctl restart xinetd
Failed to issue method call: Unit xinetd.service failed to load: No such file or directory.

出現這個結果表示為安裝xinetd,需要手動進行安裝。

[root@ftpserver ~]# yum install xinetd

在tftp伺服器中,修改/etc/xinetd.d/tftp配置檔案,將disable=yes改為no

[root@ftpserver ~]# vim /etc/xinetd.d/tftp 
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot
        disable                 = no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}
[root@ftpserver ~]#

修改後重啟xinetd服務並新增到開機啟動。

[root@ftpserver ~]# systemctl restart xinetd
[root@ftpserver ~]# systemctl enable xinetd

考慮到有些系統的防火牆預設沒有允許 UDP 協議的 69 埠,因此 需要手動將該埠號加入到防火牆的允許策略中。

[root@ftpserver ~]# firewall-cmd --permanent --add-port=69/udp
success
[root@ftpserver ~]# firewall-cmd --reload
success
[root@ftpserver ~]#

TFTP 的根目錄為/var/lib/tftpboot。

在tftp伺服器上,建立檔案tftptest.txt以供tftp下載

[root@ftpserver ~]# echo "test tftp">/var/lib/tftpboot/tftptest.txt
[root@ftpserver ~]# ll /var/lib/tftpboot/                          
total 4
-rw-r--r--. 1 root root 10 Jan 14 23:05 tftptest.txt
[root@ftpserver ~]# 

(2)fttp客戶端安裝

[root@ftp ~]# rpm -q tftp
package tftp is not installed
[root@ftp ~]# yum install tftp
...省略中間內容
Installed:
  tftp.x86_64 0:5.2-22.el7                                                 
Complete!
[root@ftp ~]#
3.2.2 使用TFTP

在tftp客戶端連線到tftpserver並下載檔案

[root@ftp ~]# tftp 192.168.78.101
tftp> get tftptest.txt
tftp> quit
[root@ftp ~]#  ll tftptest.txt 
-rw-r--r--. 1 root root 10 Jan 14 23:09 tftptest.txt
[root@ftp ~]#  cat tftptest.txt
test tftp
[root@ftp ~]# 

可以看到已經成功把檔案下載到本地了。

3.2.3 TFTP相關的命令

列舉幾個tftp相關的命令和引數

命令 作用
? 幫助資訊
put 上傳檔案
get 下載檔案
verbose 顯示詳細的處理資訊
status 顯示當前的狀態資訊
binary 使用二進位制進行傳輸
ascii 使用 ASCII 碼進行傳輸
timeout 設定重傳的超時時間
quit 退出

當然TFTP還有很多其他使用者,以後有機會在深入討論。

下一篇文章將講解Linux下檔案共享的方法