超詳細講解如何搭建自己的檔案伺服器

java小豪發表於2022-12-13

Linux上安裝檔案伺服器FTP

由於FTP、HTTP、Telnet等協議的資料都是使用明文進行傳輸的,因此從設計上就是不可靠的。人們為了滿足以密文方式傳輸檔案的需求,發明了vsftpd服務程式。vsftpd(very secure ftp daemon,非常安全的FTP守護程式)是一款執行在Linux作業系統上的FTP服務程式,不僅完全開源而且免費。此外,它還具有很高的安全性、傳輸速度,以及支援虛擬使用者驗證等其他FTP服務程式不具備的特點。在不影響使用的前提下,管理者可以自行決定客戶端是採用匿名開放、本地使用者還是虛擬使用者的驗證方式來登入vsftpd伺服器。這樣即便駭客拿到了虛擬使用者的賬號密碼,也不見得能成功登入vsftpd伺服器。

安裝VSFTP

下載dnf

[root@chenstudy ~]# yum install epel-release

image-20221212151043438

[root@chenstudy ~]# yum install dnf

image-20221212151400707

image-20221212151435242

下載VSFTP

[root@chenstudy ~]# dnf install vsftpd

image-20221212151836706

清除防火牆的iptables快取

iptables防火牆管理工具預設禁止了FTP協議的埠號,因此在正式配置vsftpd服務程式之前,為了避免這些預設的防火牆策略“搗亂”,還需要清空iptables防火牆的預設策略,並把當前已經被清理的防火牆策略狀態儲存下來:

[root@chenstudy ~]# iptables -F
[root@chenstudy ~]# iptables-save

image-20221212152116686

然後再把FTP協議新增到firewalld服務的允許列表中(前期準備工作一定要做充足):

[root@chenstudy ~]# firewall-cmd --permanent --zone=public --add-service=ftp
success
[root@chenstudy ~]# firewall-cmd --reload
success

image-20221212152751773

檢視vsftpd服務程式的主配置檔案(/etc/vsftpd/vsftpd.conf):

[root@chenstudy ~]# mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf_bak
[root@chenstudy ~]# grep -v "#" /etc/vsftpd/vsftpd.conf_bak  > /etc/vsftpd/vsftpd.conf
[root@chenstudy ~]# 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@chenstudy ~]# 

image-20221212153251812

																 **vsftpd服務程式常用的引數以及作用**
引數 作用
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為不限制

下載FTP

vsftpd作為更加安全的檔案傳輸協議服務程式,允許使用者以3種認證模式登入FTP伺服器。

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

  • 本地使用者模式:是透過Linux系統本地的賬戶密碼資訊進行認證的模式,相較於匿名開放模式更安全,而且配置起來也很簡單。但是如果駭客破解了賬戶的資訊,就可以暢通無阻地登入FTP伺服器,從而完全控制整臺伺服器。

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

ftp是Linux系統中以命令列介面的方式來管理FTP傳輸服務的客戶端工具。我們首先手動安裝這個ftp客戶端工具:

[root@chenstudy ~]# dnf install ftp

image-20221212153835464

匿名訪問模式

vsftpd服務程式中,匿名開放模式是最不安全的一種認證模式。任何人都可以無須密碼驗證而直接登入FTP伺服器。這種模式一般用來訪問不重要的公開檔案(在生產環境中儘量不要存放重要檔案)。當然,如果採用第8章中介紹的防火牆管理工具(如TCP Wrapper服務程式)將vsftpd服務程式允許訪問的主機範圍設定為企業內網,也可以提供基本的安全性。

vsftpd服務程式預設關閉了匿名開放模式,我們需要做的就是開放匿名使用者的上傳、下載檔案的許可權,以及讓匿名使用者建立、刪除、更名檔案的許可權。需要注意的是,針對匿名使用者放開這些許可權會帶來潛在危險,我們只是為了在Linux系統中練習配置vsftpd服務程式而放開了這些許可權,不建議在生產環境中如此行事。表11-2羅列了可以向匿名使用者開放的許可權引數以及作用。

​ 向匿名使用者開放的許可權引數以及作用

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

配置vsftp配置檔案:

[root@chenstudy ~]# vim /etc/vsftpd/vsftpd.conf
# 重啟vsftp
[root@chenstudy ~]# systemctl restart vsftpd
# 把vsftp加入開機自啟動
[root@chenstudy ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
[root@chenstudy ~]# 

image-20221212160751359

在linux中採用匿名訪問ftp

[root@chenstudy ~]# ftp 192.168.200.130
Connected to 192.168.200.130 (192.168.200.130).
220 (vsFTPd 3.0.2)
Name (192.168.200.130: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> mkdir files
550 Create directory operation failed.
ftp> 
# 退出ftp客戶端,修改所有者身份
[root@chenstudy ~]# ls -ld /var/ftp/pub
drwxr-xr-x. 2 root root 6 Jun 10  2021 /var/ftp/pub
[root@chenstudy ~]# chown -R ftp /var/ftp/pub
[root@chenstudy ~]# ls -ld /var/ftp/pub
drwxr-xr-x. 2 ftp root 6 Jun 10  2021 /var/ftp/pub
[root@chenstudy ~]# 

image-20221212161200821

系統提示“建立目錄的操作失敗”(Create directory operation failed),我猜應該是SELinux服務在“搗亂”

[root@chenstudy ~]# getsebool -a | grep ftp
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
tftp_anon_write --> off
tftp_home_dir --> off
[root@chenstudy ~]# setsebool -P ftpd_full_access=on

image-20221212161449935

SELinux域策略就可以順利執行檔案的建立、修改及刪除等操作了:

image-20221212162002322

本地使用者模式

本地使用者模式要更安全,而且配置起來也很簡單

本地使用者模式使用的許可權引數以及作用

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

修改vsftp的配置檔案:

[root@chenstudy ~]# vim /etc/vsftpd/vsftpd.conf
	anonymous_enable=NO
	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

image-20221212162723751

在我們輸入root管理員的密碼之前,就已經被系統拒絕訪問了。這是因為vsftpd服務程式所在的目錄中預設存放著兩個名為“使用者名稱單”的檔案(ftpusers和user_list):vsftpd服務程式目錄中的這兩個檔案也有類似的功能—只要裡面寫有某位使用者的名字,就不再允許這位使用者登入到FTP伺服器上。

[root@chenstudy ~]# cat /etc/vsftpd/user_list 
# vsftpd userlist
# If userlist_deny=NO, only allow users in this file
# If userlist_deny=YES (default), never allow users in this file, and
# do not even prompt for a password.
# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
# for users that are denied.
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
[root@chenstudy ~]# cat /etc/vsftpd/ftpusers
# Users that are not allowed to login via ftp
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
[root@chenstudy ~]# 

image-20221212162653761

我們可以使用普通使用者登入vsftp伺服器:

[root@chenstudy ~]# ftp 192.168.200.130
Connected to 192.168.200.130 (192.168.200.130).
220 (vsFTPd 3.0.2)
Name (192.168.200.130:root): chen             
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> 

image-20221212163228776

相關文章