FTP服务器支持基于Mysql的用户认证方式
默认情况下vsftpd是通过pam的认证机制来验证用户的,但是对于用户很多的情况下,这个是完善的,接下来的配置希望实现vsftpd基于mysql的用户认证
首先来安装需要的两个软件包
[root@localhost ~]# yum install mysql-server mysql-devel -y
[root@localhost ~]# yum groupinstall "Development tools" -y
然后需要下载pam_mysql,并且进行解压安装
[root@localhost mnt]# tar -zxf pam_mysql-0.7RC1.tar.gz
然后进行编译安装
[root@localhost pam_mysql-0.7RC1]# ./configure --with-mysql=/usr/ --with-open~
[root@localhost pam_mysql-0.7RC1]# make
[root@localhost pam_mysql-0.7RC1]# make install
可能出现的错误
configure: error: Cannot find pam headers. Please check if your system is ready for pam module development.
这个错误需要安装
[root@localhost pam_mysql-0.7RC1]# yum install pam-devel -y
如果出现这个错误
configure: error: Cannot locate mysql client library. Please check your mysql installation.
需要启动mysql服务,并且指定mysql路径为/usr;
对于安装成功之后的库文件在/lib/security/
[root@localhost pam_mysql-0.7RC1]# ll /lib/security/
total 140
-rwxr-xr-x. 1 root root 871 4月 16 17:38 pam_mysql.la
-rwxr-xr-x. 1 root root 135862 4月 16 17:38 pam_mysql.so
接下来需要配置mysql,创建相应的表文件
mysql> create database vsftpd;
Query OK, 1 row affected (0.00 sec)
mysql> use vsftpd;
Database changed
mysql> CREATE TABLE users (
-> id SMALLINT AUTO_INCREMENT NOT NULL,
-> name CHAR(20) NOT NULL,
-> password CHAR(48) NOT NULL,
-> PRIMARY key(id))
-> ;
Query OK, 0 rows affected (0.10 sec)
mysql> GRANT SELECT ON vsftpd.* TO vsftpd@localhost IDENTIFIED BY 'vsftpd';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT SELECT ON vsftpd.* TO vsftpd@127.0.0.1 IDENTIFIED BY 'vsftpd';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
插入两个用户
mysql> INSERT INTO users (name,password) VALUE('tom',password('redhat')),('jerry',password('redhat'));
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
接下来需要建立的是pam认证需要的配置文件
auth required /lib/security/pam_mysql.so user=vsftpd passwd=vsftpd \
host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=0
account required /lib/security/pam_mysql.so user=vsftpd passwd=vsftpd \
host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=0 //表示使用明文进行存储
建立虚拟用户映射的系统用户及对应的目录
[root@localhost ~]# useradd -s /sbin/nologin -d /var/ftproot vuser
[root@localhost ~]# chmod go+rx /var/ftproot/
接下来修改vsftpd的配置文件
pam_service_name=vsftpd.mysql
userlist_enable=YES
tcp_wrappers=YES
# ssl or tls
ssl_enable=NO
ssl_sslv3=YES
ssl_tlsv1=YES
allow_anon_ssl=NO
force_local_data_ssl=NO
force_local_logins_ssl=NO
rsa_cert_file=/etc/vsftpd/ssl/vsftpd.crt
rsa_private_key_file=/etc/vsftpd/ssl/vsftpd.key
guest_enable=YES
guest_username=vuser
接下来重新启动服务
[root@localhost ~]# /etc/init.d/vsftpd restart
Shutting down vsftpd: [ OK ]
Starting vsftpd for vsftpd: [ OK ]
然后需要测试连接用户tom
[root@my Desktop]# lftp 172.25.23.23 -utom
Password: //输入错误的密码
lftp tom@172.25.23.23:~> ls
ls: Login failed: 530 Login incorrect.
[root@my Desktop]# lftp 172.25.23.23 -utom
Password: //输入正确的密码
lftp tom@172.25.23.23:~> ls
lftp tom@172.25.23.23:~>
首先查看mysql里面存储的用户情况
测试使用mysql里面的用户是可以直接进行登陆
对于用户来说都会被映射成为vuser这个系统用户,并且家目录是/var/ftproot,对于这个目录里面的文件是可以进行下载的
[root@my Desktop]# lftp 172.25.23.23 -utom
Password:
lftp tom@172.25.23.23:~> ls
-rw-r--r-- 1 0 0 779 Apr 16 14:30 fstab
lftp tom@172.25.23.23:/> get fstab
779 bytes transferred
同样也是可以上传文件的
lftp tom@172.25.23.23:/> lcd /etc
lcd ok, local cwd=/etc
lftp tom@172.25.23.23:/> put issue
23 bytes transferred
lftp tom@172.25.23.23:/> ls
-rw-r--r-- 1 0 0 779 Apr 16 14:30 fstab
-rw------- 1 502 502 23 Apr 16 14:32 issue
对于上面定义的虚拟用户的指令都是和匿名用户的指令进行匹配,上面的这些指令同时对于所有的虚拟用户生效,因为所有的虚拟用户都被映射成为一个匿名用户vuser;
实现虚拟用户权限的精准定义
首先创建需要的几个文件
[root@localhost ~]# mkdir /etc/vsftpd/vusers_dir
[root@localhost ~]# cd /etc/vsftpd/vusers_dir
[root@localhost vusers_dir]# ls
[root@localhost vusers_dir]# touch tom
[root@localhost vusers_dir]# touch jerry
[root@localhost vusers_dir]# vim tom
[root@localhost vusers_dir]# cat tom > jerry
[root@localhost vusers_dir]# vim jerry
[root@localhost vusers_dir]# cat tom
anon_upload_enable=YES
anon_mkdir_write_enable=YES
[root@localhost vusers_dir]# cat jerry
anon_upload_enable=YES
anon_mkdir_write_enable=NO
其次更改配置文件,添加指令
[root@localhost vusers_dir]# vim /etc/vsftpd/vsftpd.conf
user_config_dir=/etc/vsftpd/vusers_dir
对于两个用户进行了不同的权限定义,接下来进程验证