vsftpd+pam+mysql

 

一、安装所需要程序

  1. 1、事先安装好开发环境和mysql数据库; 
  2. # yum -y install mysql-server mysql-devel 
  3. # yum -y groupinstall "Development Tools" "Development Libraries" 
  4.  
  5. 2.安装pam_mysql-0.7RC1 
  6. [root@localhost ~]# tar xf pam_mysql-0.7RC1.tar.gz  
  7. [root@localhost ~]# cd pam_mysql-0.7RC1 
  8. [root@localhost pam_mysql-0.7RC1]# ./configure --with-mysql=/usr --with-openssl 
  9. [root@localhost pam_mysql-0.7RC1]# make 
  10. [root@localhost pam_mysql-0.7RC1]# make install 
  11.  
  12. 编译选项解释: 
  13. --with-mysql=/usr            #mysql的安装路径 
  14. --with-openssl               #表示连接mysql数据库时,可以实现加密的方式通信(可用可不用) 
  15.  
  16. 3.安装vsftpd 
  17. # yum -y install vsftpd 
 

二、创建虚拟用户账号

1.准备数据库及相关表

首先请确保mysql服务已经正常启动。而后,按需要建立存储虚拟用户的数据库即可,这里将其创建为vsftpd数据库。

mysql> create database vsftpd;

mysql> grant select on vsftpd.* to vsftpd@localhost identified by 'vsftpd';

mysql> grant select on vsftpd.* to vsftpd@127.0.0.1 identified by 'vsftpd';

mysql> flush privileges;

mysql> use vsftpd;

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)

    -> );

2、添加测试的虚拟用户

根据需要添加所需要的用户,需要说明的是,这里将其密码采用明文格式存储,原因是pam_mysql的password()函数与MySQL的password()函数可能会有所不同。

mysql> insert into users(name,password) values('tom','redhat');

mysql> insert into users(name,password) values('jerry','redhat');

详细操作过程:

  1. [root@localhost pam_mysql-0.7RC1]# mysql 
  2. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  3. Your MySQL connection id is 4 
  4. Server version: 5.0.77 Source distribution 
  5.  
  6. Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 
  7.  
  8. mysql> create database vsftpd; 
  9. Query OK, 1 row affected (0.12 sec) 
  10.  
  11. mysql> use vsftpd; 
  12. Database changed 
  13. 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)); 
  14. Query OK, 0 rows affected (0.08 sec) 
  15.  
  16. mysql> desc users; 
  17. +----------+----------+------+-----+---------+----------------+ 
  18. | Field    | Type     | Null | Key | Default | Extra          | 
  19. +----------+----------+------+-----+---------+----------------+ 
  20. | id       | int(11)  | NO   | PRI | NULL    | auto_increment |  
  21. name     | char(20) | NO   |     | NULL    |                |  
  22. password | char(48) | NO   |     | NULL    |                |  
  23. +----------+----------+------+-----+---------+----------------+ 
  24. rows in set (0.03 sec) 
  25.  
  26. mysql> grant select on vsftpd.* to vsftpd@localhost identified by 'vsftpd'
  27. Query OK, 0 rows affected (0.04 sec) 
  28.  
  29. mysql> grant select on vsftpd.* to vsftpd@127.0.0.1 identified by 'vsftpd'
  30. Query OK, 0 rows affected (0.00 sec) 
  31.  
  32. mysql> flush privileges
  33. Query OK, 0 rows affected (0.00 sec) 
  34.  
  35. mysql> insert into users(name,passwordvalues('tom','redhat'),('jerry','redhat'
  36.     -> ; 
  37. Query OK, 2 rows affected (0.01 sec) 
  38. Records: 2  Duplicates: 0  Warnings: 0 
  39.  
  40. mysql> select * from users; 
  41. +----+-------+----------+ 
  42. | id | name  | password | 
  43. +----+-------+----------+ 
  44. |  1 | tom   | redhat   |  
  45. |  2 | jerry | redhat   |  
  46. +----+-------+----------+ 
  47. rows in set (0.00 sec) 
  48.  
  49. mysql> \q 
  50. Bye 
  51. [root@localhost pam_mysql-0.7RC1]#  

三、配置vsftpd

  1. 1.建立pam认证所需文件 
  2.  
  3. #vi /etc/pam.d/vsftpd.mysql 
  4. 添加如下两行 
  5. auth required /lib/security/pam_mysql.so user=vsftpd passwd=www.magedu.com host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=0 
  6. account required /lib/security/pam_mysql.so user=vsftpd passwd=www.magedu.com host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=0 

FTP服务系列之vsftpd+pam+mysql实现虚拟用户认证_vsftpd

FTP服务系列之vsftpd+pam+mysql实现虚拟用户认证_vsftpd_02

  1. 2.修改vsftpd的配置文件,使其适应mysql认证 
  2.  
  3. 建立虚拟用户映射的系统用户及对应的目录 
  4. #useradd -s /sbin/nologin -d /var/ftproot vuser 
  5. #chmod go+rx /var/ftproot 
  6.  
  7. 请确保/etc/vsftpd/vsftpd.conf中已经启用了以下选项 
  8. anonymous_enable=YES 
  9. local_enable=YES 
  10. write_enable=YES 
  11. anon_upload_enable=NO 
  12. anon_mkdir_write_enable=NO 
  13. chroot_local_user=YES 
  14.  
  15. 而后添加以下选项 
  16. guest_enable=YES                #是否允许来宾账号访问 
  17. guest_username=vuser            #把来宾账号映射为vuser 
  18.  
  19. 并确保pam_service_name选项的值如下所示 
  20. pam_service_name=vsftpd.mysql 

FTP服务系列之vsftpd+pam+mysql实现虚拟用户认证_mysql_03

虚拟用户登录成功

FTP服务系列之vsftpd+pam+mysql实现虚拟用户认证_vsftpd_04

四、启动vsftpd服务

# service vsftpd start

# chkconfig vsftpd on

五、配置虚拟用户具有不同的访问权限

  1. vsftpd可以在配置文件目录中为每个用户提供单独的配置文件以定义其ftp服务访问权限,每个虚拟用户的配置文件名同虚拟用户的用户名。配置文件目录可以是任意未使用目录,只需要在vsftpd.conf指定其路径及名称即可。 
  2.  
  3. 1、配置vsftpd为虚拟用户使用配置文件目录 
  4.  
  5. # vim /etc/vsftpd/vsftpd.conf 
  6. 添加如下选项 
  7. user_config_dir=/etc/vsftpd/vusers  
  8.  
  9. 2、创建所需要目录,并为虚拟用户提供配置文件 
  10.  
  11. # mkdir /etc/vsftpd/vusers/ 
  12. # cd /etc/vsftpd/vusers/ 
  13. # touch tom jerry 
  14.  
  15. 3、配置虚拟用户的访问权限 
  16.  
  17. 虚拟用户对vsftpd服务的访问权限是通过匿名用户的相关指令进行的。比如,如果需要让tom用户具有上传文件的权限,可以修改/etc/vsftpd/vusers/tom文件,在里面添加如下选项即可。 
  18. anon_upload_enable=YES 
  19. anon_mkdir_write_enable=YES 
  20. anon_other_write_enable=YES 


在tom问件里配置如下内容,可以使tom用户上传下载和删除文件,jerry用户没配置

FTP服务系列之vsftpd+pam+mysql实现虚拟用户认证_vsftpd_05

tom用户可以上传和删除等权限

  1. [root@localhost vusers]# ftp 172.16.25.11 
  2. Connected to 172.16.25.11. 
  3. 220 (vsFTPd 2.0.5) 
  4. 530 Please login with USER and PASS. 
  5. 530 Please login with USER and PASS. 
  6. KERBEROS_V4 rejected as an authentication type 
  7. Name (172.16.25.11:root): tom 
  8. 331 Please specify the password
  9. Password
  10. 230 Login successful. 
  11. Remote system type is UNIX. 
  12. Using binary mode to transfer files. 
  13. ftp> ls 
  14. 227 Entering Passive Mode (172,16,25,11,78,168) 
  15. 150 Here comes the directory listing. 
  16. 226 Directory send OK. 
  17. ftp> lcd /etc 
  18. Local directory now /etc 
  19. ftp> put inittab 
  20. local: inittab remote: inittab 
  21. 227 Entering Passive Mode (172,16,25,11,96,175) 
  22. 150 Ok to send data. 
  23. 226 File receive OK. 
  24. 1666 bytes sent in 0.00068 seconds (2.4e+03 Kbytes/s) 
  25. ftp> ls 
  26. 227 Entering Passive Mode (172,16,25,11,193,191) 
  27. 150 Here comes the directory listing. 
  28. -rw-------    1 502      502          1666 Apr 14 11:00 inittab 
  29. 226 Directory send OK. 
  30. ftp> delete inittab 
  31. 250 Delete operation successful. 
  32. ftp> ls 
  33. 227 Entering Passive Mode (172,16,25,11,113,4) 
  34. 150 Here comes the directory listing.
  35. 226 Directory send OK. 
jerry用户没有权限:
  1. [root@localhost vusers]# ftp 172.16.25.11 
  2. Connected to 172.16.25.11. 
  3. 220 (vsFTPd 2.0.5) 
  4. 530 Please login with USER and PASS. 
  5. 530 Please login with USER and PASS. 
  6. KERBEROS_V4 rejected as an authentication type 
  7. Name (172.16.25.11:root): jerry 
  8. 331 Please specify the password
  9. Password
  10. 230 Login successful. 
  11. Remote system type is UNIX. 
  12. Using binary mode to transfer files. 
  13. ftp> lcd /etc 
  14. Local directory now /etc 
  15. ftp> put inittab   
  16. local: inittab remote: inittab 
  17. 227 Entering Passive Mode (172,16,25,11,78,3) 
  18. 550 Permission denied. 
  19. ftp>  
这就是基于pam的虚拟用户认证,大家都来试试吧!