搭建ftp服務器,滿足以下要求:
1、允許匿名用戶登錄服務器並下載文件,下載速度設置為最高2MB/s
2、不允許本地用戶登錄ftp服務器
3、在服務器添加虛擬用戶vuser01、vuser02、vuser03,密碼自己設置。其中:
(1)vuser01用戶的下載速度最高為3MB/s,vuser02為4MB/s,vuser03為5MB/s;
(2)vuser01可以進行文件上傳,但不能進行其它操作;
(3)vuser02可以上傳和創建目錄;
(4)vuser03可以進行上傳、創建文件和刪除文件;
(5)所有虛擬用戶只能在/myserver/ftproot目錄下活動
4、設置服務器的最大並發客戶數為10,密碼輸入次數最大為3,每個ip地址最多只能建立5個連接
5、設置防火牆只允許進行ftp訪問和ping測試,不能訪問其它任何服務
實驗環境
服務器A:10.0.10.158
服務器B:10.0.100.191
客戶端C:10.0.100.198
服務器:安裝vsftpd和db_load加密工具
# yum -y install vsftp* # yum -y install db4-utils
要求1:允許匿名用戶登錄服務器並下載文件,下載速度設置為最高2MB/s
# vim /etc/vsftpd/vsftp.conf anonymous_enable=YES #允許匿名用戶登錄 anon_max_rate=2000000 #設置最大下載速度為2MB/s
結果驗證:
# ftp 10.0.10.158 Connected to 10.0.10.158 (10.0.10.158). 220 (vsFTPd 2.2.2) Name (10.0.10.158:root): ftp ------>#用系統默認的匿名用戶ftp登錄 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> ls -------->#匿名用戶ftp默認的家目錄是/var/ftp 227 Entering Passive Mode (10,0,10,158,118,127). 150 Here comes the directory listing. -rw-r--r-- 1 0 0 104857600 Nov 30 06:42 100m_file ------>#事先在服務器上傳好一個100M大小的文件 drwxr-xr-x 2 0 0 4096 Mar 02 2012 pub 226 Directory send OK. ftp> get 100m_file local: 100m_file remote: 100m_file 227 Entering Passive Mode (10,0,10,158,189,24). 150 Opening BINARY mode data connection for 100m_file (104857600 bytes). 226 Transfer complete. 104857600 bytes received in 61.3 secs (1711.39 Kbytes/sec) ------>#看耗時和下載速度 ftp> put /etc/passwd ------>#試圖上傳一個文件,deny local: /etc/passwd remote: /etc/passwd 227 Entering Passive Mode (10,0,10,158,126,52). 550 Permission denied. ftp> mkdir aa ------>#試圖建立目錄,deny 550 Permission denied.
要求2:在服務器添加虛擬用戶vuser01、vuser02、vuser03,限制在/myserver/ftproot目錄下活動。且每個虛擬用戶有不同的權限要求。
1.在配置文件中添加對虛擬用戶的支持
# vim /etc/vsftpd/vsftp.conf guest_enable=YES #實體用戶均被假設成‘guest’登錄 guest_username=virtftp #這個‘guest’被映射為本地的‘virtftp’用戶 pam_service_name=vsftpd #設置在PAM所使用的名稱,默認值為vsftpd user_config_dir=/etc/vsftpd/virt_dir #虛擬用戶的單獨配置信息設置放在/etc/vsftpd/virt_dir下
2.編輯虛擬用戶名和密碼的文本文件(奇數行是用戶名,偶數行是密碼)
# vim /etc/vsftpd/virt_user.txt vuser01 \\用戶名 123123 \\密碼 vuser02 123123 vuser03 123123
3.將文本文件生成數據庫文件
# db_load -T -t hash -f /etc/vsftpd/virt_user.tct /etc/vsftpd/virt_user.db
4.創建PAM認證文件
# vim /etc/pam.d/vsftpd #%PAM-1.0 auth sufficient /lib/security/pam_userdb.so db=/etc/vsftpd/virt_user --->#我的機子是32位的所以是/lib(64位的機子要寫成/lib64) account sufficient /lib/security/pam_userdb.so db=/etc/vsftpd/virt_user
5.創建本地用戶virtftp(我們在配置文件中已經寫了,虛擬用戶都映射為本地用戶virtftp,且要求虛擬用戶的家目錄為/myserver/ftproot,所以virtftp的家目錄也應該是這個)
# useradd -d /myserver/ftproot/ -s /sbin/nologin virtftp #設置家目錄為/myserver/ftproot,shell為不可登錄 # ll -d /myserver/ftproot/ drwx------. 3 virtftp virtftp 4096 11月 29 20:17 /myserver/ftproot/ # chmod 755 /myserver/ftproot/ #將家目錄的權限改成755 # ll -d /myserver/ftproot/ drwxr-xr-x. 3 virtftp virtftp 4096 11月 29 20:17 /myserver/ftproot/ #家目錄的權限和屬主屬組一定要正確
6.創建/etc/vsftpd/virt_dir目錄,在其中寫每個用戶的不同權限配置要求(為什么是/etc/vsftpd/virt_dir這個目錄,也是因為我們在配置文件中設置好的)
# mkdir /etc/vsftpd/virt_dir # cd /etc/vsftpd/virt_dir # vim vuser01 ------>#在里面寫虛擬用戶vuser01的相關配置 local_root=/myserver/ftproot ------>#用戶家目錄 anon_upload_enable=YES ------>#允許上傳 anon_max_rate=3000000 ------>#設置最大不超過3MB/s # vim vuser02 local_root=/myserver/ftproot anon_upload_enable=YES ------>#允許上傳 anon_mkdir_write_enable=YES ------>#允許創建目錄 anon_max_rate=4000000 # vim vuser03 local_root=/myserver/ftproot anon_upload_enable=YES anon_mkdir_write_enable=YES anon_other_write_ebale=YES ------>#允許有‘寫’以外的權限 anon_max_rate=5000000
7.注意服務器上防火牆和SELinux的設置(無論在服務器還是客戶端,若出現配置無問題但總是不成功就要考慮到這兩個的設置)
# iptables -F # setenforce 0 # getsebool -a | grep ftp allow_ftpd_anon_write --> off allow_ftpd_full_access --> off allow_ftpd_use_cifs --> off allow_ftpd_use_nfs --> off ftp_home_dir --> off ftpd_connect_db --> off ftpd_use_passive_mode --> off httpd_enable_ftp_server --> off tftp_anon_write --> off # setsebool ftp_home_dir 1 # setsebool tftp_anon_write 1 # setsebool allow_ftpd_anon_write 1 # setsebool allow_ftpd_full_access 1 # getsebool -a | grep ftp allow_ftpd_anon_write --> on allow_ftpd_full_access --> on allow_ftpd_use_cifs --> off allow_ftpd_use_nfs --> off ftp_home_dir --> on ftpd_connect_db --> off ftpd_use_passive_mode --> off httpd_enable_ftp_server --> off tftp_anon_write --> on
8.還有一個要求是“不允許本地用戶登錄”,但是配置文件中不能直接就寫成"local_enable=NO",因為虛擬用戶還要登錄(即映射在本地的virtftp要能登錄)。所以利用user_list來實現這一要求
# vim /etc/vsftpd/vsftp.conf local_enable=YES userlist_enable=YES ------>#啟用user_list文件 userlist_deny=NO ------>#userlist文件變成白名單!表示只允許userlist列表中的用戶登錄 # vim /etc/vsftp/userlist ------>#在userlist中寫入允許登錄的用戶(即虛擬用戶) (注意並不是寫virtftp) vuser01 vuser02 vuser03
要求3:服務器的最大並發客戶數為10,密碼輸入次數最大為3,每個ip地址最多只能建立5個連接
# vim /etc/vsftpd/vsftp.conf max_clients=10 ------>#最大並發客戶連接數 max_per_ip=5 ------>#每個IP最大連接數
驗證
(1) vuser02用戶驗證
# ftp 10.0.10.158 Connected to 10.0.10.158 (10.0.10.158). 220 (vsFTPd 2.2.2) Name (10.0.10.158:root): vuser02 ------>#以vuser02用戶登錄 331 Please specify the password. Password: 230 Login successful. ------>#可登錄 Remote system type is UNIX. Using binary mode to transfer files. ftp> mkdir aa ------>#可創建目錄 257 "/aa" created ftp> ls 227 Entering Passive Mode (10,0,10,158,131,117). 150 Here comes the directory listing. -rw-r--r-- 1 0 0 104857600 Nov 30 06:48 100m_file drwx------ 2 503 503 4096 Nov 30 08:19 aa drwxr-xr-x 2 0 0 4096 Nov 30 04:56 test 226 Directory send OK. ftp> put /test.txt ------>#可上傳文件 local: /test.txt remote: /test.txt 227 Entering Passive Mode (10,0,10,158,106,249). 150 Ok to send data. 226 Transfer complete. ftp> ls 227 Entering Passive Mode (10,0,10,158,188,10). 150 Here comes the directory listing. -rw-r--r-- 1 0 0 104857600 Nov 30 06:48 100m_file drwx------ 2 503 503 4096 Nov 30 08:19 aa drwxr-xr-x 2 503 503 4096 Nov 30 09:52 test -rw------- 1 503 503 0 Nov 30 09:55 test.txt 226 Directory send OK.
(2) vuser01用戶驗證
# ftp 10.0.10.158 Connected to 10.0.10.158 (10.0.10.158). 220 (vsFTPd 2.2.2) Name (10.0.10.158:root): vuser01 ------>#用vuser01登錄 331 Please specify the password. Password: 230 Login successful. ------>#可登錄 Remote system type is UNIX. Using binary mode to transfer files. ftp> put /test2.txt ------>#可上傳 local: /test2.txt remote: /test2.txt 227 Entering Passive Mode (10,0,10,158,94,158). 150 Ok to send data. 226 Transfer complete. ftp> ls 227 Entering Passive Mode (10,0,10,158,208,4). 150 Here comes the directory listing. -rw-r--r-- 1 0 0 104857600 Nov 30 06:48 100m_file drwx------ 2 503 503 4096 Nov 30 08:19 aa drwxr-xr-x 2 503 503 4096 Nov 30 09:52 test -rw------- 1 503 503 0 Nov 30 09:55 test.txt -rw------- 1 503 503 0 Nov 30 09:57 test2.txt 226 Directory send OK. ftp> mkdir aa ------>#不可新建目錄 550 Permission denied.
(3) vuser03用戶驗證
# ftp 10.0.10.158 Connected to 10.0.10.158 (10.0.10.158). 220 (vsFTPd 2.2.2) Name (10.0.10.158:root): vuser03 ------>#用vuser03登錄 331 Please specify the password. Password: 230 Login successful. ------>#可登錄 Remote system type is UNIX. Using binary mode to transfer files. ftp> put test3.txt ------>#可上傳 local: test3.txt remote: test3.txt 227 Entering Passive Mode (10,0,10,158,89,248). 150 Ok to send data. 226 Transfer complete. ftp> mkdir bb ------>#可新建目錄 257 "/bb" created ftp> rm bb ------>#可刪除目錄(但貌似只能刪除自己創建的目錄??) 250 Remove directory operation successful. ftp> delete test2.txt ------>#可刪除文件 250 Delete operation successful. ftp> delete test.txt 250 Delete operation successful. ftp> get 100m_file local: 100m_file remote: 100m_file 227 Entering Passive Mode (10,0,10,158,19,173). 150 Opening BINARY mode data connection for 100m_file (104857600 bytes). 226 Transfer complete. 104857600 bytes received in 21.6 secs (4856.31 Kbytes/sec) ------>#下載的速度也符合設置
(4)本地普通用戶驗證
# ftp 10.0.10.158 Connected to 10.0.10.158 (10.0.10.158). 220 (vsFTPd 2.2.2) Name (10.0.10.158:root): user_00 ------>#拒絕了本地用戶user_00的登錄 530 Permission denied. Login failed.
補充
1.貼出該實驗中的配置文件全部的有效選項
# cat vsftpd.conf | grep -v "^#" | grep -v "^$" anonymous_enable=YES local_enable=YES write_enable=YES local_umask=022 anon_upload_enable=YES anon_max_rate=2000000 dirmessage_enable=YES xferlog_enable=YES connect_from_port_20=YES xferlog_std_format=YES listen=YES guest_enable=YES guest_username=virtftp pam_service_name=vsftpd user_config_dir=/etc/vsftpd/virt_dir userlist_enable=YES userlist_deny=NO tcp_wrappers=YES max_clients=10 max_per_ip=5 max_login_fails=3
2.實驗中出現的錯誤記錄,請參考: