使用指定虛擬用戶Allen與Barry登陸ftp,認證的源是mysql服務器; Allen能夠上傳文件,Barry不能夠上傳文件;
[root@centos7 ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@centos7 ~]# uname -r 4.18.14-1.el7 vsftpd-3.0.2-22.el7.x86_64 pam-1.1.8-22.el7.x86_64 mariadb-libs-5.5.60-1.el7_5.x86_64 pam_mysql-0.7RC1.tar.gz mysql服務器:10.207.51.40 vsftp服務器:10.207.51.31
yum -y groupinstall "Development Tools" "server Platform Development" yum -y install mariadb-devel openssl-devel pam-devel
yum install -y vsftpd
mysql
有不少默認參數,不用去管,有標註的參數爲本次實驗關鍵參數 anonymous_enable=NO local_umask=022 dirmessage_enable=YES xferlog_enable=YES connect_from_port_20=YES xferlog_file=/var/log/xferlog xferlog_enable=YES xferlog_std_format=YES dual_log_enable=YES vsftpd_log_file=/var/log/vsftpd.log use_localtime=YES ascii_upload_enable=YES ascii_download_enable=YES listen=YES listen_ipv6=NO userlist_enable=YES tcp_wrappers=YES 當chroot_list_enable=NO,chroot_local_user=YES時,全部的用戶均不能切換到其餘目錄。 chroot_list_enable=NO chroot_local_user=YES local_enable=YES ##容許本地用戶登陸,雖然使用的虛擬用戶,可是虛擬用戶適合本地用戶vuser關聯了的 write_enable=YES ##容許寫入 local_root=/var/ftp ##指定本地用戶登陸後的目錄 pam_service_name=vsftpd.mysql ##指定pam文件,這是相對路徑,等於/etc/pam.d/vsftpd.mysql,這裏也能夠寫全路徑 guest_enable=YES ##容許虛擬用戶登陸 guest_username=vuser ##指定虛擬用戶要映射成哪一個本地用戶 allow_writeable_chroot=YES ##默認狀況下ftp根目錄的ogt都不能有w權限,開啓其選項後能夠設置w權限; user_config_dir=/etc/vsftpd/vusers.conf.d ##指定虛擬用戶配置文件目錄,目錄中的每一個配置文件的名字對應一個虛擬用戶的名字;
文件名稱要與虛擬用戶命名徹底一致 mkdir /etc/vsftpd/vusers.conf.d echo ' anon_upload_enable=YES' > /etc/vsftpd/vusers.conf.d/Allen echo ' anon_upload_enable=NO' > /etc/vsftpd/vusers.conf.d/Barry
也能夠經過epel源安裝 tar xf pam_mysql-0.7RC1.tar.gz cd pam_mysql-0.7RC1 ./configure --with-mysql=/usr --with-openssl=/usr --with-pam=/usr --with-pam-mods-dir=/lib64/security make make install
vim /etc/pam.d/vsftpd.mysql auth required pam_mysql.so user=vsftpd passwd=123123 host=jzbg db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2 verbose=1 account required pam_mysql.so user=vsftpd passwd=123123 host=jzbg db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2 verbose=1 配置字段說明: auth 表示認證; required 用於認證登陸mysql數據庫; account 用於驗證登陸ftp使用的虛擬帳號的密碼; pam_mysql.so模塊是默認的相對路徑,是相對/lib64/security/路徑而言,也能夠寫絕對路徑;後面爲給此模塊傳遞的參數; user=vsftpd爲 登陸mysql的用戶; passwd=mageedu 登陸mysql的的密碼; host=localhost 在mysql中定義的容許鏈接的主機名或ip地址; db=vsftpd 鏈接msyql的哪個庫; table=users 鏈接庫裏的哪個表; usercolumn=name 當作用戶名的字段 passwdcolumn=password 當作用戶名字段的密碼 crypt=2 密碼的加密方式爲mysql password()函數加密; verbose=1 會打印詳細的日誌,能夠再/var/log/secure中看到;
systemctl restart vsftpd.service
sql
yum -y install mariadb_server
數據庫
systemctl enable mariadb.service && systemctl start mariadb.service
vim
在/etc/my.cnf文件的[mysqld]中增長skip-name-resolve=ON [mysqld] skip-name-resolve=ON
mysql CREATE DATABASE vsftpd; use vsftpd; CREATE TABLE users (id int AUTO_INCREMENT NOT NULL PRIMARY KEY, name char(30) NOT NULL, password char(48) binary NOT NULL); INSERT INTO users(name,password) VALUES ('Allen',password('123123')); INSERT INTO users(name,password) VALUES ('Barry',password('123123')); 頗有可能,pam-mysql模塊支持的加密方式,與mysql或 mariadb加密方式不兼容;若是不兼容,則須要把密碼改成明文格式: INSERT INTO users(name,password) VALUES ('tom',123123); INSERT INTO users(name,password) VALUES ('jerry',123123); 容許vsftp服務器使用vsftpd用戶讀取vsftpd數據庫到的表 若是將vsftp與mysql安裝在一臺機器上,則能夠將地址改成127.0.0.1 GRANT SELECT ON vsftpd.* TO vsftpd@'10.207.51.31' IDENTIFIED BY '123123'; 刷新權限 FLUSH PRIVILEGES;
Allen用戶能夠上傳文件,結果爲正確 [root@~]$ftp 10.207.51.31 Connected to 10.207.51.31 (10.207.51.31). 220 (vsFTPd 3.0.2) Name (10.207.51.31:root): Allen 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> put all.csv local: all.csv remote: all.csv 227 Entering Passive Mode (10,207,51,31,67,156). 150 Ok to send data. 226 Transfer complete. 6406155 bytes sent in 0.483 secs (13255.36 Kbytes/sec) Barry用戶不能夠上傳文件,結果爲正確 [root@~]$ftp 10.207.51.31 Connected to 10.207.51.31 (10.207.51.31). 220 (vsFTPd 3.0.2) Name (10.207.51.31:root): Barry 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 (10,207,51,31,238,18). 150 Here comes the directory listing. -rw------- 1 1002 1002 6406155 Dec 20 14:38 all.csv 226 Directory send OK. ftp> put all2.csv local: all.csv remote: all2.csv 227 Entering Passive Mode (10,207,51,31,190,67). 550 Permission denied.