linux系統中部署vsftpd服務(虛擬使用者模式)
阿新 • • 發佈:2020-12-14
vsftpd服務採用伺服器端/客戶機端模式
以下實驗PC1為伺服器端,IP為192.168.10.10; PC2為客戶機端,IP為192.168.10.20
1、在PC1伺服器端安裝vsftpd服務
[root@PC1 ~]# yum install vsftpd.x86_64 -y
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
rhel7 | 4.1 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package vsftpd.x86_64 0:3.0.2-9.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vsftpd x86_64 3.0.2-9.el7 rhel7 166 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 166 k
Installed size: 343 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : vsftpd-3.0.2-9.el7.x86_64 1/1
rhel7/productid | 1.6 kB 00:00
Verifying : vsftpd-3.0.2-9.el7.x86_64 1/1
Installed:
vsftpd.x86_64 0:3.0.2-9.el7
Complete!
2、清空PC1伺服器端防火牆策略並儲存
[root@PC1 ~]# iptables -F
[root@PC1 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
3、在PC1伺服器端精簡vsftpd服務主配置檔案
[root@PC1 ~]# cd /etc/vsftpd/
[root@PC1 vsftpd]# ls
ftpusers user_list vsftpd.conf vsftpd_conf_migrate.sh
[root@PC1 vsftpd]# cp vsftpd.conf vsftpd.conf.bak
[root@PC1 vsftpd]# grep -v "#" vsftpd.conf.bak > vsftpd.conf
[root@PC1 vsftpd]# cat 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
4、在PC1伺服器端建立用於進行FTP認證的使用者資料庫檔案,奇數行為賬戶名、偶數行為密碼,使用雜湊演算法生成新的資料庫檔案,降低新的資料庫檔案許可權,並刪除原始資料庫檔案
[root@PC1 vsftpd]# cd /etc/vsftpd/
[root@PC1 vsftpd]# vim vuser.list
zhangsan
redhat
lisi
redhat
[root@PC1 vsftpd]# db_load -T -t hash -f vuser.list vuser.db
[root@PC1 vsftpd]# file vuser.list
vuser.list: ASCII text
[root@PC1 vsftpd]# file vuser.db
vuser.db: Berkeley DB (Hash, version 9, native byte-order)
[root@PC1 vsftpd]# chmod 600 vuser.db
[root@PC1 vsftpd]# rm -f vuser.list
[root@PC1 vsftpd]# ls
ftpusers vsftpd.conf vsftpd_conf_migrate.sh
user_list vsftpd.conf.bak vuser.db
[root@PC1 vsftpd]# cat vuser.db
�� ��$��cэh^redhatlisi[root@PC1 vsftpd]#
5、 建立vsftpd服務登入的根目錄、建立虛擬使用者(ftp登入的賬戶(比如zhangsan、lisi)需要對映到具體的使用者)
[root@PC1 vsftpd]# useradd -d /var/ftproot -s /sbin/nologin virtual
[root@PC1 vsftpd]# ls -ld /var/ftproot/
drwx------. 3 virtual virtual 74 Dec 14 20:19 /var/ftproot/
[root@PC1 vsftpd]# chmod -R 755 /var/ftproot/
[root@PC1 vsftpd]# ls -ld /var/ftproot/
drwxr-xr-x. 3 virtual virtual 74 Dec 14 20:19 /var/ftproot/
6、PC1中建立用於支援虛擬使用者的PAM檔案(zhangsan、lisi不是真實的使用者,可以實現登入??)
[root@PC1 vsftpd]# vim /etc/pam.d/vsftpd.vu ## vuser 為建立的經雜湊演算法處理後的使用者資料庫檔案
auth required pam_userdb.so db=/etc/vsftpd/vuser
account required pam_userdb.so db=/etc/vsftpd/vuser
7、PC1中修改主配置檔案
[root@PC1 vsftpd]# vim /etc/vsftpd/vsftpd.conf
anonymous_enable=NO
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
8、在PC1中為虛擬使用者設定不同的許可權
[root@PC1 vsftpd]# mkdir /etc/vsftpd/vusers_dir/
[root@PC1 vsftpd]# cd /etc/vsftpd/vusers_dir/
[root@PC1 vusers_dir]# touch lisi
[root@PC1 vusers_dir]# vim zhangsan
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
9、在PC1中修改vsftpd的主配置檔案
[root@PC1 vusers_dir]# vim /etc/vsftpd/vsftpd.conf
1 anonymous_enable=NO
2 local_enable=YES
3 guest_enable=YES
4 guest_username=virtual
5 allow_writeable_chroot=YES
6 write_enable=YES
7 local_umask=022
8 dirmessage_enable=YES
9 xferlog_enable=YES
10 connect_from_port_20=YES
11 xferlog_std_format=YES
12 listen=NO
13 listen_ipv6=YES
14
15 pam_service_name=vsftpd.vu
16 userlist_enable=YES
17 tcp_wrappers=YES
18 user_config_dir=/etc/vsftpd/vusers_dir ## 指定了虛擬使用者許可權檔案的路徑
10、在PC1主機中重啟vsftpd服務
[root@PC1 vusers_dir]# systemctl restart vsftpd
[root@PC1 vusers_dir]# systemctl enable vsftpd
ln -s '/usr/lib/systemd/system/vsftpd.service' '/etc/systemd/system/multi-user.target.wants/vsftpd.service'
[root@PC1 vusers_dir]# systemctl status vsftpd.service
vsftpd.service - Vsftpd ftp daemon
Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; enabled)
Active: active (running) since Mon 2020-12-14 20:43:17 CST; 22s ago
Main PID: 32049 (vsftpd)
CGroup: /system.slice/vsftpd.service
└─32049 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
Dec 14 20:43:17 PC1 systemd[1]: Starting Vsftpd ftp daemon...
Dec 14 20:43:17 PC1 systemd[1]: Started Vsftpd ftp daemon.
11、在PC1主機中檢視並設定SELinux
[root@PC1 vusers_dir]# 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@PC1 vusers_dir]# setsebool -P ftpd_full_access=on
[root@PC1 vusers_dir]# getsebool -a | grep ftp
ftp_home_dir --> off
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> on
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
12、在PC2客戶機中安裝vsftp客戶端工具
[root@PC2 test]# yum install ftp -y
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package ftp.x86_64 0:0.17-66.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
ftp x86_64 0.17-66.el7 rhel7 61 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 61 k
Installed size: 96 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ftp-0.17-66.el7.x86_64 1/1
Verifying : ftp-0.17-66.el7.x86_64 1/1
Installed:
ftp.x86_64 0:0.17-66.el7
Complete!
13、在PC2客戶端主機中登入測試
[root@PC2 test]# ftp 192.168.10.10
Connected to 192.168.10.10 (192.168.10.10).
220 (vsFTPd 3.0.2)
Name (192.168.10.10:root): lisi
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
257 "/"
ftp> cd /tmp ## 這一步說明僅能在指定的目錄
550 Failed to change directory.
ftp> mkdir files
550 Permission denied.
[root@PC2 test]# ftp 192.168.10.10
Connected to 192.168.10.10 (192.168.10.10).
220 (vsFTPd 3.0.2)
Name (192.168.10.10:root): zhangsan
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,10,10,170,162).
150 Here comes the directory listing.
226 Directory send OK.
ftp> pwd
257 "/"
ftp> cd /tmp
550 Failed to change directory.
ftp> mkdir files ## 這一步可以看書lisi 和 zhangsan 賬戶許可權的不同
257 "/files" created
14、利用zhangsan使用者測試傳輸檔案功能
[root@PC1 ~]# cd /var/ftproot/
[root@PC1 ftproot]# ls
[root@PC1 ftproot]# echo "i am pc1" > pc1.file
[root@PC1 ftproot]# ls
pc1.file
[root@PC2 test]# pwd
/home/test
[root@PC2 test]# ls
[root@PC2 test]# echo "i am pc2" > pc2.file
[root@PC2 test]# ls
pc2.file
[root@PC2 test]# ftp 192.168.10.10
Connected to 192.168.10.10 (192.168.10.10).
220 (vsFTPd 3.0.2)
Name (192.168.10.10:root): zhangsan
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,10,10,139,101).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 9 Dec 14 13:01 pc1.file
226 Directory send OK.
ftp> mput pc2.file ## 向伺服器端傳輸
mput pc2.file? y
227 Entering Passive Mode (192,168,10,10,126,134).
150 Ok to send data.
226 Transfer complete.
9 bytes sent in 4.3e-05 secs (209.30 Kbytes/sec)
ftp> ls
227 Entering Passive Mode (192,168,10,10,73,59).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 9 Dec 14 13:01 pc1.file
-rw------- 1 1001 1001 9 Dec 14 13:02 pc2.file
226 Directory send OK.
ftp> mget pc1.file ## 向本地傳輸
mget pc1.file?
227 Entering Passive Mode (192,168,10,10,145,33).
150 Opening BINARY mode data connection for pc1.file (9 bytes).
226 Transfer complete.
9 bytes received in 2.1e-05 secs (428.57 Kbytes/sec)
ftp> exit
221 Goodbye.
[root@PC2 test]# ls
pc1.file pc2.file
[root@PC2 test]# cat pc1.file
i am pc1
虛擬使用者模式的安全性體現在哪裡?
匿名使用者模式:任何人均可登入,但是僅能訪問設定的根目錄(/var/ftp)
本地使用者模式: 需要賬戶和密碼,但是可以根據用許可權訪問不同目錄(從這點講,本地使用者模式不見得比匿名使用者安全)。
虛擬使用者模式: 需要特定賬戶和密碼,同時僅能訪問設定的根目錄(建立的用於對映的虛擬使用者的家目錄),所以所虛擬使用者模式集合了匿名使用者模式和本地使用者模式兩者安全性的優點,因此說最安全。