实验环境:
Linux centos-server 2.6.32-504.el6.x86_64 vsftpd-2.2.2-13.el6_6.1.x86_64 mysql-5.1.73-3.el6_5.x86_64
搭建所需环境:
yum install mysql mysql-server openssl pam-devel yum -y groupinstall "Development Tools" "Development Libraries"
安装pam_mysql:
wget http://ncu.dl.sourceforge.net/project/pam-mysql/pam-mysql/0.7RC1/pam_mysql-0.7RC1.tar.gz tar zxvf pam_mysql-0.7RC1.tar.gz cd pam_mysql-0.7RC1 ./configure --with-pam=/usr/ --with-mysql=/usr/bin/mysql_config --with-openssl make && make install 安装完成以后在/lib/security/下会多出个pam_mysql.la和pam_mysql.so [root@centos-server ~]# ls /lib/security/ pam_mysql.la pam_mysql.so
安装vsftp:
yum -y install vsftpd
在mysql中新建用户"vsftpd"用于vsftpd查询,并且新建vsftpd表并创建虚拟用户"down"和"up":
mysql> create database vsftpd; mysql> grant select on vsftpd.* to vsftpd@localhost identified by 'dragon'; mysql> create table user( -> id int AUTO_INCREMENT NOT NULL primary key, -> name char(20) binary NOT NULL, -> password char(48) binary NOT NULL, -> ); mysql> insert into user(name,password) values('down','qwe123'); Query OK, 1 row affected (0.00 sec) mysql> insert into user(name,password) values('up','qwe123'); Query OK, 1 row affected (0.00 sec)
配置pam通过mysql认证:
[root@centos-server ~]# cat >>/etc/pam.d/vsftpd.mysql<< end auth required /lib/security/pam_mysql.so user=vsftpd passwd=dragon host=localhost db=vsftpd table=user usercolumn=name passwdcolumn=password crypt=0 account required /lib/security/pam_mysql.so user=vsftpd passwd=dragon host=localhost db=vsftpd table=user usercolumn=name passwdcolumn=password crypt=0 end
添加虚拟用户宿主用户:
[root@centos-server ~]# useradd -s /sbin/nologin -d /var/ftp/v vuser1 [root@centos-server ~]# chmod o+rx /var/ftp/v/
创建虚拟用户“UP”的配置文件,浏览+下载+上传+创建目录+删除:
[root@centos-server ~]# cat >>/etc/vsftpd/vuser_dir/up <<end local_root /var/ftp/v/up anon_world_readable_only=no #可浏览 write_enable=yes#可写 anon_upload_enable=yes #可上传,需要write_enable=yes anon_other_write_enable=yes anon_mkdir_write_enable=yes #可创建目录 end
创建虚拟用户“down”的配置文件,可下载:
[root@centos-server ~]# cat >>/etc/vsftpd/vuser_dir/down<<end local_root=/var/ftp/v/down download_enable=YES write_enable=NO anon_upload_enable=NO end
创建虚拟用户的根目录
[root@centos-server ~]# mkdir /var/ftp/v/down [root@centos-server ~]# chown -R vuser1.vuser1 /var/ftp/v/down/ [root@centos-server ~]# mkdir /var/ftp/v/up [root@centos-server ~]# chown -R vuser1.vuser1 /var/ftp/v/up
在vsftpd.conf的添加以下配置
[root@centos-server ~]# tail -n 6 /etc/vsftpd/vsftpd.conf pam_service_name=vsftpd.mysql #pam认证文件 userlist_enable=YES #开启用户访问控制列表 tcp_wrappers=YES#开启tcp_wrappers访问控制 guest_enable=YES#开启虚拟用户功能 guest_username=vuser1#虚拟用户宿主用户 user_config_dir=/etc/vsftpd/vuser_dir #配置虚拟用户文件目录
暂时SELINUX:
setenforce 0
开启服务:
[root@centos-server ~]# service vsftpd restart
测试效果:
用户up可登录和上传:
2.用户“down”可登录,创建和上传操作被拒绝:
本文出自 “龙爱雪琪” 博客,转载请与作者联系!