实例:Vsftpd基于MySQL实现用户认证;
工作原理:
实验环境:
ftp服务器,IP:192.168.1.10,操作系统:CentOS6.7 x86_64
MySQL服务器,IP:192.168.1.11,操作系统:CentOS6.7 x86_64
客户端,IP:192.168.1.12,操作系统:CentOS6.7x86_64
实验过程:
MySQL服务器安装配置:
# yum install mysql mysql-server mysql-devel
新建逻辑卷;
# pvcreate /dev/sdb
# vgcreate -s 8M data /dev/sdb
# lvcreate -L 49G -n mydata data
# mke2fs -text4 –b 4096 /dev/data/mydata
# mkdir /data
# vim /etc/fstab
> /dev/data/mydata /data ext4 defaults 0 0
# mount –a
# mkdir -p /data/mydata
# chown -R mysql:mysql /data/mydata/
# chmod -R o-rx /data/mydata/
编辑配置文件:
# vim /etc/my.cnf
> [mysqld]
> datadir=/data/mydata
启动服务:
# chkconfig mysqld on
# service mysqld start
创建虚拟用户:
# mysql
mysql> CREATE DATABASE ftpdb;
mysql> GRANT SELECT ON ftpdb.* TO ftpuser@'192.168.1.10' IDENTIFIED BY 'ftppass';
mysql> FLUSHPRIVILEGES;
mysql> USE ftpdb;
mysql> CREATE TABLE users (
-> id INT AUTO_INCREMENT NOT NULL,
-> name CHAR(50) BINARY NOT NULL,
-> password VARCHAR(48) BINARY NOT NULL,
-> PRIMARY KEY(id) );
mysql> INSERT INTO users(name,password) VALUES('tom',password('tomtom'));
mysql> INSERT INTO users(name,password) VALUES('jim',password('jimjim'));
vsftpd服务器配置:
# yum install vsftpd pam_mysql
创建虚拟用户映射的本地用户:
# mkdir /data
# useradd -s /sbin/nologin -d /data/ftpdir ftpuser
# chmod -R go+rx /data/ftpdir
编辑配置文件:
# vim /etc/vsftpd/vsftpd.conf
> anonymous_enable=YES
> local_enable=YES
> write_enable=YES
> chroot_local_user=YES
>
> pam_service_name=vsftpd.mysql :指定pam认证文件;
添加以下选项:
> guest_enable=YES :允许来宾账户;
> guest_username=ftpuser :映射为系统用户;
创建pam认证文件:
# vim /etc/pam.d/vsftpd.mysql
> auth required /lib64/security/pam_mysql.so user=ftpuser passwd=ftppass host=192.168.1.11 db=ftpdb table=users usercolumn=name passwdcolumn=password crypt=2
> account required /lib64/security/pam_mysql.so user=ftpuser passwd=ftppass host=192.168.1.11 db=ftpdb table=users usercolumn=name passwdcolumn=password crypt=2
为虚拟用户创建单独的配置文件;
# vim /etc/vsftpd/vsftpd.conf
> user_config_dir=/etc/vsftpd/users_config
为虚拟用户提供配置文件:
# mkdir /etc/vsftpd/vusers_config
# touch /etc/vsftpd/vusers_config/{tom,jim}
# vim /etc/vsftpd/vusers_config/tom
> anon_upload_enable=YES
> anon_mkdir_write_enable=YES
> anon_other_write_enable=YES
# vim /etc/vsftpd/vusers_config/jim
> anon_upload_enable=YES
> anon_mkdir_write_enable=YES
> anon_other_write_enable=YES
启动服务:
# chkconfig vsftpd on
# service vsftpd start
客户端测试:
安装客户端:
# yum install lftp
登陆测试:
结语:
Vsftpd的服务就介绍到这里,试验中如有遗漏和失误,欢迎各路大神指正,谢谢。