使用vsftpd服務傳輸文件 1 安裝 2 安裝 `lftp`客戶端軟體 3 啟動服務並加入開機自啟動 4 查看監聽埠狀態 使用匿名開放模式 服務啟動後預設訪問的目錄為/var/ftp,且匿名用戶沒有上傳、創建、重命名、刪除功能.但是具有下載功能. 服務開啟後直接訪問 創建文件夾被拒絕 重命名失敗 ...
使用vsftpd服務傳輸文件
1 安裝vsftpd
[root@qdlinux ~]# yum install vsftpd
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package vsftpd.x86_64 0:3.0.2-22.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================
Installing:
vsftpd x86_64 3.0.2-22.el7 dvd 169 k
Transaction Summary
=======================================================================================================================
Install 1 Package
Total download size: 169 k
Installed size: 348 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : vsftpd-3.0.2-22.el7.x86_64 1/1
Verifying : vsftpd-3.0.2-22.el7.x86_64 1/1
Installed:
vsftpd.x86_64 0:3.0.2-22.el7
Complete!
[root@qdlinux ~]#
2 安裝ftp
lftp
客戶端軟體
[root@qdlinux ~]# yum install ftp lftp -y
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package ftp.x86_64 0:0.17-67.el7 will be installed
---> Package lftp.x86_64 0:4.4.8-8.el7_3.2 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================
Installing:
ftp x86_64 0.17-67.el7 dvd 61 k
lftp x86_64 4.4.8-8.el7_3.2 dvd 752 k
Transaction Summary
=======================================================================================================================
Install 2 Packages
Total download size: 812 k
Installed size: 2.5 M
Downloading packages:
-----------------------------------------------------------------------------------------------------------------------
Total 8.7 MB/s | 812 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : lftp-4.4.8-8.el7_3.2.x86_64 1/2
Installing : ftp-0.17-67.el7.x86_64 2/2
Verifying : ftp-0.17-67.el7.x86_64 1/2
Verifying : lftp-4.4.8-8.el7_3.2.x86_64 2/2
Installed:
ftp.x86_64 0:0.17-67.el7 lftp.x86_64 0:4.4.8-8.el7_3.2
Complete!
3 啟動服務並加入開機自啟動
[root@qdlinux ~]# systemctl start vsftpd
[root@qdlinux ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
4 查看監聽埠狀態
[root@qdlinux ~]# ss -lntup | grep vsftpd
tcp LISTEN 0 32 :::21 :::* users:(("vsftpd",pid=1411,fd=3))
使用匿名開放模式
服務啟動後預設訪問的目錄為/var/ftp,且匿名用戶沒有上傳、創建、重命名、刪除功能.但是具有下載功能.
服務開啟後直接訪問
創建文件夾被拒絕
重命名失敗
我們來修改配置文件讓匿名用戶可以上傳文件,編輯配置文件vim /etc/vsftpd/vsftpd.conf
,修改為如下內容
anon_upload_enable=YES
anon_umask=022
現在依然無法上傳文件,這是因為ftp的許可權問題導致的,我們將其修改為ftp用戶許可權.
==註意:預設匿名用戶家目錄/var/ftp的許可權是755,這個許可權是不能改變的,切記!==
[root@qdlinux var]# ll -d ftp
drwxr-xr-x 3 root root 30 Aug 13 21:32 ftp
[root@qdlinux var]# chown -Rf ftp /var/ftp/pub
[root@qdlinux var]# ll ftp
total 0
-rw-r--r-- 1 root root 0 Aug 13 21:32 1.txt
drwxr-xr-x 2 ftp root 19 Aug 13 21:32 pub
現在已經可以上傳文件了,但是只能上傳到ftp/pub目錄中,因為剛纔給的就是這個目錄的許可權.現在就做到了只能上傳文件和下載的目的,但是不能創建文件夾.
上傳文件
重命名失敗
創建文件夾被拒絕
[root@qdlinux var]# ll ftp/pub
total 0
-rw-r--r-- 1 ftp root 0 Aug 13 21:32 2.txt
-rw-r--r-- 1 ftp ftp 0 Aug 13 21:51 new.txt
[root@qdlinux var]#
如果需要開啟匿名用戶可以創建文件夾功能,修改配置文件如下所示.
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_umask=022
現在匿名用戶可以上傳文件和下載文件以及創建文件夾.但是不能重命名.
如果需要開啟匿名用戶上傳下載和創建文件夾以及重命名和刪除功能,修改配置文件如下所示.
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
anon_umask=022
現在匿名用戶可以做到上傳和下載,創建文件夾和刪除文件夾和文件以及重命名.
上傳文件
新建文件夾
這是匿名用戶的最大許可權了,一般匿名訪問的話,只要做到下載功能就可以了。網站提供什麼用戶下載什麼。
使用本地用戶模式
示例演示公司內部有一臺FTP和WEB伺服器,FTP的功能主要用於維護網站內容,用於上傳文件、創建目錄、更新網頁等等。公司有兩個部門負責維護任務,他們分別用team1和team2賬號進行管理,要求僅允許team1和team2賬號登錄ftp伺服器,單不能登錄本地系統,並將這兩個賬號的根目錄限製為/var/www/html,不能進去該目錄以外的任何目錄。
前期準備首先安裝apache
和vsftpd
,具體安裝方法這裡就不介紹了。
創建測試賬號team1和team2
[root@qdlinux ~]# useradd -s /sbin/nologin team1
[root@qdlinux ~]# useradd -s /sbin/nologin team2
[root@qdlinux ~]# echo "123456" | passwd --stdin team1
Changing password for user team1.
passwd: all authentication tokens updated successfully.
[root@qdlinux ~]# echo "123456" | passwd --stdin team2
Changing password for user team2.
passwd: all authentication tokens updated successfully.
接下來修改配置文件,這裡在RHEL6中和RHEL7中配置有些不同
anonymous_enable=NO
local_enable=YES
chroot_local_user=YES
local_root=/var/www/html
allow_writeable_chroot=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
可以把不限制的賬戶放到這裡面,創建用戶列表文件,因為此賬戶會訪問到其他目錄.但這樣做事很危險的.
[root@qdlinux ~]# touch /etc/vsftpd/chroot_list
[root@qdlinux ~]# vim /etc/vsftpd/chroot_list
teamleader
修改apache的根目錄許可權/var/www/html
[root@qdlinux ~]# ll -d /var/www/html/
drwxr-xr-x 2 root root 6 May 9 2017 /var/www/html/
[root@qdlinux ~]# chmod -R o+w /var/www/html/
訪問測試
不限制用戶訪問
不限制用戶訪問,用戶可以隨意到任何目錄
限制用戶訪問,用戶只能在此目錄中,在安全方面相對要好很多.
文件上傳後文件擁有者許可權
虛擬用戶模式
示例演示為了宣傳公司的產品信息,計劃搭建FTP伺服器,為客戶提供相關資料的下載,僅允許下載產品信息,禁止上傳。公司的合作單位和可以在FTP伺服器上上傳和下載,但是不能刪除數據。
創建用戶進行FTP認證的用戶資料庫文件,其中奇數行為賬戶名,偶數行為密碼。編輯vim /etc/vsftpd/vuser.list
ftp
123456
vip
123456
由於明文的信息既不安全,也不符合讓vsftpd服務程式直接加在的格式,因此需要使用db_load命令用哈希演算法將明文信息文件轉成資料庫文件。在RHEL6中需要安裝db4-utils
,在RHEL7中libdb-utils
[root@qdlinux ~]# rpm -qf `which db_load`
libdb-utils-5.3.21-20.el7.x86_64
[root@qdlinux ~]# db_load -T -t hash -f /etc/vsftpd/vuser.list /etc/vsftpd/vuser.db
[root@qdlinux ~]# file /etc/vsftpd/vuser.db
/etc/vsftpd/vuser.db: Berkeley DB (Hash, version 9, native byte-order)
降低資料庫文件的許可權避免其他人看到資料庫文件的內容,然後把明文信息文件刪除。
[root@qdlinux ~]# chmod 600 /etc/vsftpd/vuser.db
[root@qdlinux ~]# ll /etc/vsftpd/vuser.db
-rw------- 1 root root 12288 Aug 14 01:19 /etc/vsftpd/vuser.db
建立用於支持虛擬用戶的PAM文件
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@qdlinux ~]# useradd -d /var/ftp/share -s /sbin/nologin ftpuser
[root@qdlinux ~]# useradd -d /var/ftp/vip -s /sbin/nologin ftpvip
[root@qdlinux ~]# chmod -R 500 /var/ftp/share/
[root@qdlinux ~]# chmod -R 700 /var/ftp/vip/
修改配置文件
anonymous_enable=NO
local_enable=YES
allow_writeable_chroot=YES
pam_service_name=vsftpd.vu
user_config_dir=/etc/vsftpd/vuserconfig
max_clients=300
max_per_ip=10
建立虛擬賬號配置文件
[root@qdlinux ~]# mkdir /etc/vsftpd/vuserconfig
[root@qdlinux ~]# touch /etc/vsftpd/vuserconfig/ftp
[root@qdlinux ~]# touch /etc/vsftpd/vuserconfig/vip
vim編輯/etc/vsftpd/vuserconfig/ftp
中的內容
guest_enable=YES
guest_username=ftpuser
anon_world_readable_only=NO
anon_max_rate=50000
vim編輯/etc/vsftpd/vuserconfig/vip
中的內容
guest_enable=YES
guest_username=ftpvip
anon_world_readable_only=NO
write_enable=YES
anon_mkdir_write_enable=YES
anon_upload_enable=YES
anon_max_rate=2000000
測試ftp賬戶
[root@qdlinux ~]# lftp 192.168.56.15 -u ftp,123456
lftp [email protected]:~> ls
drwx------ 5 0 0 97 Aug 13 18:14 grub2
-r--r--r-- 1 0 0 11607904 Aug 13 18:13 mariadb-server.rpm
訪問限速測試
測試vip賬戶
[root@qdlinux ~]# lftp 192.168.56.15 -u vip
Password:
lftp [email protected]:~> ls
-rw------- 1 1004 1004 5505024 Aug 13 18:33 kali-linux-2018.2-amd64.iso
-rw------- 1 1004 1004 111017984 Aug 13 18:36 kali-linux-2018.2-amd641.iso
-r--r--r-- 1 0 0 11607904 Aug 13 18:30 mariadb-server.rpm
上傳和下載訪問速度測試