文件傳輸協議(File Transfer Protocol,FTP)部署


下面開始vsftpd 服務 程序三種認證模式(匿名開放模式、本地用戶模式、虛擬用戶模式)的配置方法

FTP 是一種在互聯網中進行文件傳輸的協議,基於客戶端/服務器模式,默認使用 20、21 號端口,其中端口 20(數據端口)用於進行數據傳輸,端口 21(命令端口)用於接受客戶端 發出的相關 FTP 命令與參數。FTP 服務器普遍部署於內網中,具有容易搭建、方便管理的特 點。而且有些 FTP 客戶端工具還可以支持文件的多點下載以及斷點續傳技術。

vsftpd(very secure ftp daemon,非常安全的 FTP 守護進程)是一款運行在 Linux 操作系 統上的 FTP 服務程序,不僅完全開源而且免費,此外,還具有很高的安全性、傳輸速度,以 及支持虛擬用戶驗證等其他 FTP 服務程序不具備的特點。

安裝vsftpd

yum install vsftpd -y

備份配置文件

cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.back

過濾掉配置文件的注釋信息

grep -v "#" /etc/vsftpd/vsftpd.conf.back > /etc/vsftpd/vsftpd.conf

配置文件內容如下

[root@k8s-master01 ~]# cat /etc/vsftpd/vsftpd.conf
anonymous_enable=YES            #是否允許匿名用戶訪問
local_enable=YES
write_enable=YES
local_umask=022              #本地用戶上傳文件的 umask 值
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

 

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

yum install ftp -y 

 

匿名開放模式

vsftpd 服務程序默認開啟了匿名開放模式,我們需要做的就是開放匿名用戶的上傳、下 載文件的權限,以及讓匿名用戶創建、刪除、更名文件的權限。

修改配置文件如下

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
anno_umask=022             #匿名用戶上傳文件的 umask 值 anno_upload_enable=YES #允許匿名用戶上傳文件 anno_mkdir_write_enable=YES #允許匿名用戶創建目錄 anno_other_write_enable=YES #允許匿名用戶修改目錄名稱或刪除目錄 

 

然后賦予/var/ftp目錄下pub目錄權限

chown -R  ftp  put

測試

[root@k8s-master01 ftp]# ftp 10.122.138.244
Connected to 10.122.138.244 (10.122.138.244).
220 (vsFTPd 3.0.2)
Name (10.122.138.244:root): anonymous
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd pub
250 Directory successfully changed.
ftp> mkdir app
257 "/pub/app" created
ftp> ls
227 Entering Passive Mode (10,122,138,244,127,159).
150 Here comes the directory listing.
drwx------    2 14       0               6 Sep 29 14:55 aa
drwx------    2 14       50              6 Sep 29 15:05 app
drwxr-xr-x    2 14       0               6 Sep 29 14:38 files
226 Directory send OK.
ftp> 

 

配置本地用戶模式

修改配置文件

drwx------ 2 ftp ftp 6 Sep 29 23:01 cdd
[root@k8s-master01 ftp]# vim /etc/vsftpd/vsftpd.conf
[root@k8s-master01 ftp]# systemctl restart vsftpd
[root@k8s-master01 ftp]# cat /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

#no_anon_password=YES
#anon_root=/var/ftp
#anon_upload_enable=YES
#anon_mkdir_write_enable=YES
#anon_other_write_enable=YES
#anon_umask=077

重啟服務

測試

[root@k8s-master01 ftp]# ftp 10.122.138.244
Connected to 10.122.138.244 (10.122.138.244).
220 (vsFTPd 3.0.2)
Name (10.122.138.244:root): root
530 Permission denied.
Login failed.
ftp> exit
221 Goodbye.
[root@k8s-master01 ftp]# ftp 10.122.138.244
Connected to 10.122.138.244 (10.122.138.244).
220 (vsFTPd 3.0.2)
Name (10.122.138.244:root): liutao
331 Please specify the password.
Password:
530 Login incorrect.
Login failed.
ftp> exit
221 Goodbye.
[root@k8s-master01 ftp]# ftp 10.122.138.244
Connected to 10.122.138.244 (10.122.138.244).
220 (vsFTPd 3.0.2)
Name (10.122.138.244:root): lili
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd pub
550 Failed to change directory.
ftp> ls
227 Entering Passive Mode (10,122,138,244,182,97).
150 Here comes the directory listing.
226 Directory send OK.
ftp> ls
227 Entering Passive Mode (10,122,138,244,25,153).
150 Here comes the directory listing.
226 Directory send OK.
ftp> cd pub
550 Failed to change directory.
ftp> mkdir files
257 "/home/lili/files" created
ftp> ls
227 Entering Passive Mode (10,122,138,244,213,198).
150 Here comes the directory listing.
drwxr-xr-x    2 1014     1017            6 Sep 29 15:11 files
226 Directory send OK.
ftp> 

上面測試顯示,root無法登陸,因為vsftpd 服務程序為了保證服務器的安全性而默認禁止了 root 管理員和大多數 系統用戶的登錄行為,這樣可以有效地避免黑客通過 FTP 服務對 root 管理員密碼進行暴力破 解。如果您確認在生產環境中使用 root 管理員不會對系統安全產生影響,只需按照上面的提 示刪除掉 root 用戶名即可。我們也可以選擇 ftpusers 和 user_list 文件中沒有的一個普通用戶 嘗試登錄 FTP 服務器

[root@k8s-master01 ftp]# 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@k8s-master01 ftp]# 

這上面的用戶都不能登陸,測試的liutao用戶不存在,lili用戶存在。在采用本地用戶模式登錄 FTP 服務器后,默認訪問的是該用戶的家目錄,也就是說,訪 問的是/home/linuxprobe 目錄。而且該目錄的默認所有者、所屬組都是該用戶自己,因此不存 在寫入權限不足的情況

 

下面配置虛擬用戶模式

配置文件修改為:

anonymous_enable=NO
local_enable=YES
write_enable=YES
guest_enable=YES
guest_username=vsftpd
allow_writeable_chroot=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

創建用於進行 FTP 認證的用戶數據庫文件,其中奇數行為賬戶名,偶數行為密 碼。例如,我們分別創建出 zhangsan 和 lisi 兩個用戶,密碼均為 redhat

[root@linuxprobe ~]# cd /etc/vsftpd/
[root@linuxprobe vsftpd]# vim vuser.list
zhangsan
redhat
lisi
redhat

 

但是,明文信息既不安全,也不符合讓 vsftpd 服務程序直接加載的格式,因此需要使 用 db_load 命令用哈希(hash)算法將原始的明文信息文件轉換成數據庫文件,並且降低 數據庫文件的權限(避免其他人看到數據庫文件的內容),然后再把原始的明文信息文件 刪除

[root@linuxprobe vsftpd]# db_load -T -t hash -f vuser.list vuser.db
[root@linuxprobe vsftpd]# file vuser.db
vuser.db: Berkeley DB (Hash, version 9, native byte-order)
[root@linuxprobe vsftpd]# chmod 600 vuser.db
[root@linuxprobe vsftpd]# rm -f vuser.list

 

新建一個用於虛擬用戶認證的 PAM 文件 vsftpd.vu,其中 PAM 文件內的“db=”參數為 使用 db_load 命令生成的賬戶密碼數據庫文件的路徑,但不用寫數據庫文件的后綴:

[root@linuxprobe ~]# vim /etc/pam.d/vsftpd.vu
auth required pam_userdb.so db=/etc/vsftpd/vuser
account required pam_userdb.so db=/etc/vsftpd/vuser

 

現在可以在客戶端測試

[root@localhost ~]# ftp 192.168.172.249
Connected to 192.168.172.249 (192.168.172.249).
220 (vsFTPd 3.0.2)
Name (192.168.172.249:root): lisi
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> ll
?Invalid command
ftp> ls
227 Entering Passive Mode (192,168,172,249,25,1).
150 Here comes the directory listing.
226 Directory send OK.
ftp> pwd
257 "/pub"
ftp> 

 

為虛擬用戶設置不同的權限。雖然賬戶 zhangsan 和 lisi 都是用於 vsftpd 服務程 序認證的虛擬賬戶,但是我們依然想對這兩人進行區別對待。比如,允許張三上傳、創建、修 改、查看、刪除文件,只允許李四查看文件。這可以通過 vsftpd 服務程序來實現。只需新建 一個目錄,在里面分別創建兩個以 zhangsan 和 lisi 命名的文件,其中在名為 zhangsan 的文件 中寫入允許的相關權限(使用匿名用戶的參數)

[root@linuxprobe ~]# mkdir /etc/vsftpd/vusers_dir/
[root@linuxprobe ~]# cd /etc/vsftpd/vusers_dir/
[root@linuxprobe vusers_dir]# touch lisi
[root@linuxprobe vusers_dir]# vim zhangsan
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES

然后再次修改 vsftpd 主配置文件,通過添加 user_config_dir 參數來定義這兩個虛擬用戶 不同權限的配置文件所存放的路徑。為了讓修改后的參數立即生效,需要重啟 vsftpd 服務程 序並將該服務添加到開機啟動項中:

user_config_dir=/etc/vsftpd/vusers_dir

systemctl restart vsftpd

systemctl enable vsftpd


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM