所謂虛擬用戶就是,全部虛擬用戶會統一映射爲一個指定的系統普通帳號:訪問共享位置,即爲此係統普通用戶的家目錄,固然每一個虛擬用戶也可被賦予不一樣的訪問權限,經過匿名用戶的權限控制參數進行指定數據庫
虛擬用戶賬號的存儲方式: vim
1,建立專門一個文件存放用戶密碼,可是該文件須要用hash格式。因此須要用下面的這條命令:安全
db_load -T -t hash -f vusers.txt vusers.db
實現步驟:ide
"注意該文件的格式:第一行 用戶名,第二行 密碼,以此類推" vim /etc/vsftpd/vusers.txt wang #用戶名 wangpass #密碼 mage #用戶名 magepass #密碼
1,cd /etc/vsftpd/ 2,db_load -T -t hash -f vusers.txt vusers.db #爲了安全修改文件權限 3,chmod 600 vusers.*
1,建立用戶 useradd -d /var/ftproot -s /sbin/nologin vuser • 2,由於FTP是隻能普通用戶登入,因此普通用戶須要對目錄有讀和執行權限,這樣才能上傳下載文件。 chmod +rx /var/ftproot/ 3,由於FTP爲了安全根目錄不能有寫權限,由於根目錄有寫權限,咱們登入上去是否是就能根目錄給刪了對吧,因此須要執行下面的命令。 chmod -w /var/ftproot/ #爲了能有個上傳下載的目錄單獨在建立一個目錄。 mkdir /var/ftproot/upload #由於登入上來映射的用戶都是vuser用戶因此要給vuser用戶對upload目錄有徹底控制的權限,因此能夠給它ACL權限這樣比較安全 setfacl -m u:vuser:rwx /var/ftproot/upload
由於如今的驗證方式跟以前的不一樣了,因此須要本身建立一個PAM模塊文件,而後在主配置文件中調用該文件測試
1,建立PAM模塊文件ui
vim /etc/pam.d/vsftpd.db 文件內容以下: auth required pam_userdb.so db=/etc/vsftpd/vusers account required pam_userdb.so db=/etc/vsftpd/vusers
2,從新指定PAM配置文件rest
vim /etc/vsftpd/vsftpd.conf guest_enable=YES guest_username=vuser "pam_service_name=vsftpd.db"注意該項在配置文件中已經有須要修改一下
禁用SELinux或者 setsebool -P ftpd_full_access 1 code
若是是CentOS-7:systemctl restart vsftpd 若是是CentOS-6:service vsftpd restart
這時候登入就能夠用剛纔建立的vusers.txt文件裏的用戶名密碼了ci
例如: [root@ansible-7 ~]# ftp 192.168.136.7 Connected to 192.168.136.7 (192.168.136.7). 220 (vsFTPd 3.0.2) Name (192.168.136.7:root): "wang" 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,136,7,149,32). 150 Here comes the directory listing. drwxrwxr-x 2 0 0 6 Feb 27 00:58 upload 226 Directory send OK.
成功了,可是有一個不足就是每一個用戶登入上來都是一個權限一個目錄這樣不太靈活,下面實現每一個虛擬用戶登入上來各自對應不一樣的權限,而且還可以讓他登入上來對應的目錄都不一樣一個。rem
1,修改FTP主配置文件
vim /etc/vsftpd/vsftpd.conf #下面這個選項,做用就是讓虛擬用戶支持獨立的權限配置文件 user_config_dir=/etc/vsftpd/vusers.d/
2,給每一個虛擬用戶建立一個獨立的權限配置文件
#建立配置文件存放的路徑 mkdir /etc/vsftpd/vusers.d/ #進入此目錄 cd /etc/vsftpd/vusers.d #建立各自用戶的權限配置文件 vim wang anon_upload_enable=YES #容許wang用戶上傳文件 anon_mkdir_write_enable=YES #容許wang用戶建立文件 anon_other_write_enable=YES #容許wang用戶刪除文件 local_root=/wangroot #登陸wang目錄改變至指定wangroot目錄裏 注意"wangroot這個目錄手工建立,而且該目錄不能有w權限,能夠爲wang用戶單首創建一個共享目錄而且權限也須要改成766,讓Other具備讀寫權限" vim li anon_other_write_enable=YES #容許li用戶刪除文件 local_root=/liroot #登陸li目錄改變至指定liroot目錄裏 "一樣liroot目錄也須要手工建立,操做跟wang帳號的同樣"
3,測試
一,wang帳號測試 登入測試:成功 [root@ansible-7 ~]# ftp 192.168.136.7 Connected to 192.168.136.7 (192.168.136.7). 220 (vsFTPd 3.0.2) Name (192.168.136.7:root): wang 331 Please specify the password. Password: 上傳文件測試:成功 ftp> !ls #查看本機文件 anaconda-ks.cfg Downloads Music Templates Desktop f1 Pictures Videos Documents Public ftp> put f1 #上傳文件 local: f1 remote: f1 227 Entering Passive Mode (192,168,136,7,226,193). 150 Ok to send data. 226 Transfer complete. 1073741824 bytes sent in 3.56 secs (301946.46 Kbytes/sec) ftp> ls #查看目錄列表 227 Entering Passive Mode (192,168,136,7,40,213). 150 Here comes the directory listing. -rw------- 1 1001 1001 1073741824 Feb 27 03:12 f1 建立目錄測試:成功 ftp> mkdir wangdir #建立目錄 257 "/upload/wangfile" created ftp> ls #查看目錄列表 227 Entering Passive Mode (192,168,136,7,208,167). 150 Here comes the directory listing. -rw------- 1 1001 1001 1073741824 Feb 27 03:12 f1 drwx------ 2 1001 1001 6 Feb 27 03:12 wangdir 226 Directory send OK. "下載文件測試:失敗測試" "爲何呢,由於FTP默認,被下載的文件必須全部人都有讀權限,若是想什麼文件都能下載的話," "須要修改主配置文件加上這一項: anon_world_readable_only=NO便可 (默認YES)" ftp> get f1 local: f1 remote: f1 227 Entering Passive Mode (192,168,136,7,74,156). 550 Failed to open file. 刪除文件測試:成功 ftp> delete f1 250 Delete operation successful. ftp> ls 227 Entering Passive Mode (192,168,136,7,130,109). 150 Here comes the directory listing. drwx------ 2 1001 1001 6 Feb 27 03:12 wangfile 226 Directory send OK. 二,li帳號測試 登錄測試:成功 [root@ansible-7 ~]# ftp 192.168.136.7 Connected to 192.168.136.7 (192.168.136.7). 220 (vsFTPd 3.0.2) Name (192.168.136.7:root): li 331 Please specify the password. Password: 上傳文件測試:失敗 ftp> put f1 local: f1 remote: f1 227 Entering Passive Mode (192,168,136,7,232,207). 550 Permission denied. 下載文件測試:成功。默認FTP就支持匿名下載因此限制不了 ftp> get f1 local: f1 remote: f1 227 Entering Passive Mode (192,168,136,7,219,59). 150 Opening BINARY mode data connection for f1 (1073741824 bytes). 226 Transfer complete. 1073741824 bytes received in 2.55 secs (421099.01 Kbytes/sec) 刪除文件測試:成功 ftp> delete f1 250 Delete operation successful. ftp> ls 227 Entering Passive Mode (192,168,136,7,195,31). 150 Here comes the directory listing. drwx------ 2 1001 1001 6 Feb 27 03:34 a 建立目錄測試:失敗 ftp> mkdir lidir 550 Permission denied.