安装配置步骤
配置环境:
关闭iptables
关闭selinux
[root@CentOS6 ~]# service iptables status
iptables: Firewall is not running.
[root@CentOS6 ~]# getenforce
Permissive
1、安装vsftpd、mysql-server:(yum方式)
程序包:vsftpd、mysql-server、mysql-devel、pan_mysql
步骤:
[root@CentOS6 ~]# yum -y install vsftpd mysql-server mysql-devel pam_mysql :yum会自动安装依赖环境;
Installed:
mysql-devel.x86_64 0:5.1.73-3.el6_5 mysql-server.x86_64 0:5.1.73-3.el6_5
vsftpd.x86_64 0:2.2.2-13.el6_6.1
安装完整之后,发现pam-mysql没有安装上,原因是yum源里面没有这个包,需要epel源才能进行安装(epel是一个第三方的yum源仓库)。
2、安装epel源仓库和pam-mysql:
程序包:el-release-6-8.noarch.rpm
pam-mysql
注意:epel下载地址:http://rpmfind.net/linux/rpm2html/search.php?query=epel-release (注意下载适合自己linux版本的)
[root@CentOS6 src]# rpm -ivh epel-release-6-8.noarch.rpm
[root@CentOS6 src]# yum -y install pam_mysql
[root@CentOS6 src]# rpm -ql pam_mysql
/lib64/security/pam_mysql.so :查看这个关键模块是否安装成功,注意位置是/lib64/下面;
3、配置mysql:
创建vsftpd库和授权一个能查询该库的sql用户:
mysql> create database vsftpd;
mysql> grant select on vsftpd.* to vsftpd@localhost identified by 'vsftpd';
mysql> grant select on vsftpd.* to [email protected] identified by 'vsftpd';
mysql> flush privileges;
创建存放虚拟账号的表:
mysql> create table users (
-> id int AUTO_INCREMENT NOT NULL,
-> name char(20) binary NOT NULL,
-> password char(48) binary NOT NULL,
-> primary key(id)
-> );
Query OK, 0 rows affected (0.03 sec)
添加ftp虚拟账号到表中:
mysql> insert into users (name,password) values ('tom',password('tom'));
Query OK, 1 row affected (0.02 sec)
mysql> insert into users (name,password) values ('test',password('test'));
Query OK, 1 row affected (0.00 sec)
mysql> select * from users;
+----+------+-------------------------------------------+
| id | name | password |
+----+------+-------------------------------------------+
| 1 | tom | *71FF744436C7EA1B954F6276121DB5D2BF68FC07 |
| 2 | test | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 |
+----+------+-------------------------------------------+
2 rows in set (0.04 sec)
4、配置vsftp,使它支持mysql:
1、自定义一个文件,“告知pam_mysql如何去找到数据库中表的字段”
vim /etc/pam.d/vsftpd.mysql
auth required /lib64/security/pam_mysql.so user=vsftpd passwd=vsftpd host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2
account required /lib64/security/pam_mysql.so user=vsftpd passwd=vsftpd host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2
:此处注意:crypt=2表示使用password()的方式对数据库中创建的ftp账号进行加密存放;
2、添加系统账号:
[root@CentOS6 ~]# useradd -s /sbin/nologin -d /var/ftproot vuser
[root@CentOS6 ~]# chmod -R go+rx /var/ftproot
3、启用vsftpd的mysql认证:
vim /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
local_enable=YES
write_enable=YES
anon_upload_enable=NO
anon_mkdir_write_enable=NO
chroot_local_user=YES
guest_enable=YES
guest_username=vuser
pam_service_name=vsftpd.mysql
xferlog_enable=YES
xferlog_file=/var/log/xferlog
dual_log_enable=YES
vsftpd_log_file=/var/log/vsftpd.log
use_localtime=YES :启用日志记录功能;
5、启动mysql、vsftpd服务:
[root@CentOS6 ~]# netstat -tnlp | grep :21
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 2339/vsftpd
[root@CentOS6 ~]# netstat -tnlp | grep mysqld
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2568/mysqld
6、安装lftp客户端:
[root@CentOS6 ~]# yum -y install lftp
[root@CentOS6 var]# lftp -u test 172.16.1.110
Password:
lftp [email protected]:~> pwd
ftp://[email protected]
[root@CentOS6 var]# lftp -u tom 172.16.1.110
Password:
lftp [email protected]:~> pwd
ftp://[email protected]
[root@CentOS6 log]# tail -f /var/log/vsftpd.log
Wed Mar 25 06:28:57 2015 [pid 31287] CONNECT: Client "172.16.1.110"
Wed Mar 25 06:28:57 2015 [pid 31286] [test] OK LOGIN: Client "172.16.1.110"
Wed Mar 25 06:31:01 2015 [pid 31308] CONNECT: Client "172.16.1.110"
Wed Mar 25 06:31:01 2015 [pid 31307] [tom] OK LOGIN: Client "172.16.1.110"
:登录成功!
7、虚拟用户账号访问ftp的限制:
1、实现让tom无权限删除,只有上传、下载权限:
vim /etc/vsftpd/vsftpd.conf
anonymous_enable=NO
anon_upload_enable=NO
anon_mkdir_write_enable=NO
user_config_dir=/etc/vsftpd/users_dir :为虚拟用户指定配置文件目录路径;
[root@CentOS6 ~]# mkdir /etc/vsftpd/users_dir/
[root@CentOS6 ~]# touch /etc/vsftpd/users_dir/tom
[root@CentOS6 ~]# touch /etc/vsftpd/users_dir/test
:为虚拟用户创建以其名字命令的配置文件;
[root@CentOS6 ~]# vim /etc/vsftpd/users_dir/tom
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=NO
2、实现test只有下载的权限:
[root@CentOS6 ~]# vim /etc/vsftpd/users_dir/test
anon_upload_enable=NO
anon_mkdir_write_enable=YES
anon_other_write_enable=NO
:注意,账号的最终权限是vsftpd和文件系统权限的交集;
3、测试一下即可;