FTP (vi) Implementing a MySQL-authenticated FTP virtual user

Source: Internet
Author: User
Tags auth crypt

Experiment Preparation: This experiment is implemented on two CentOS hosts, one as FTP server and one for database server .

    • A CentOS-7 when MySQL server
    • One CentOS-6 when FTP server
      Note that here who when the FTP server is critical, because there is a PAM module package is not on the CentOS-7, need source code compilation, Pam_mysql, below I first demonstrate CentOS-6 when the FTP server example
CENTOS-6 when FTP server one, configure Database server 1, install the Mariadb-server package on the database server side
    yum –y install  mariadb-server    systemctl  start  mariadb     # 设为开机自动启动    systemctl  enable mariadb
2, you can run the MARIADB security script for security reasons.
"mysql_secure_installation"    第一项问你:输入root密码  回车即可,因为没有    第二项问你:需要设置root密码么,当然要  敲Y    第三项问你:需要删除空账号用户么,当然要  敲Y    第四项问你:禁止root用户远程登入么,根据你们公司的需要    第五项问你:需要删除test测试数据哭么,我不需要    第六项问你:现在重新加载权限表吗 ,当然
3, establish virtual user account on MARIADB server side
 1,创建存储虚拟用户数据库和连接的数据库用户(带有mysql>的就表示需要连接到数据库执行操作)     #创建数据库     mysql> CREATE DATABASE vsftpd;       #查看数据库是否创建成功     mysql> SHOW DATABASES; 2,创建管理vsftpd数据库的用户。     mysql> GRANT all ON vsftpd.* TO   [email protected]‘192.168.136.6‘ IDENTIFIED BY ‘centos‘;      命令解析:        GRANT:                 创建授权用户关键字        all  :                  表示拥有对vsftpd这个数据库的所有权限        vsftpd.*:              表示指定vsftpd的所有表        [email protected]‘192.168.136.6‘: @ 前的vsftpd表示用户名,@ 后面的表示该用户只能在192.168.136.6这个主机登入,如果想表示一个网段可以加百分号:192.168.136.%        IDENTIFIED BY ‘centos‘: 作用设置密码,centos就是该用户的密码。

Prepare the user's related tables

    #切表        mysql> USE vsftpd;     #查看表        Mysql> SHOW TABLES;      #创建users表        mysql>create table users (id int auto_increment not null primary key,name varchar(30) binary not null, password varchar(50) binary not null);    命令解析:        create table users:创建表名为users子句        口号里的就表结构,用逗号分开的表示字段例如:第一个字段为id,第二个字段为name,第三个字段为password        修饰符:        int:           表示该字段为数字,        auto_increment:表示该字段是整数自动增长        not null:      表示该字段不能为空        primary key:   表示该字段为主键        varchar(30):   表示该字段可以是任意字符长度为30个        binary:        作用是让字段能够用于登入验证

Add a virtual user to the user table

Add the required user as needed, and store the password for security should be encrypted using the password function

    #查看表结构    mysql>DESC users;      #插入内容    mysql> INSERT INTO users(name,password)    values(‘wang‘,password(‘wang‘));      #插入内容    mysql> INSERT INTO users(name,password)          values(‘li‘,password(‘li‘));      #查看表内容    mysql> SELECT * FROM users;
Second, configure FTP server 1, install the VSFTPD and Pam_mysql packages on the FTP server
 centos6:pam_mysql由epel6的源中提供    配置epel源:    [epel]    name=centos-epel    baseurl=http://mirrors.aliyun.com/epel/6/x86_64/    gpgcheck=0    enable=1 #安装模块,以及FTP服务器端包    yum install vsftpd pam_mysql
2, create the required PAM Module authentication file for FTP

Because you need to connect to the database, the previous Pam block is no longer available, you need to configure

在/etc/pam.d/目录下创建一个名为vsftpd.mysql存放PAM模块的配置文件     cd /etc/pam.d/     touch  vsftpd.mysql      vim vsftpd.mysql     添加如下几行:     auth required pam_mysql.so user=vsftpd passwd=centos host=数据库IP地址  db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2      account required pam_mysql.so user=vsftpd passwd=centos host=数据库IP地址 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2 配置字段说明     ? auth 表示认证     ? account 验证账号密码正常使用     ? required 表示认证要通过     ? pam_mysql.so模块是默认的相对路径,是相对/lib64/security/路 径而言,也可以写绝对路径;后面为给此模块传递的参数     ? user=vsftpd为登录mysql的用户     ? passwd=magedu 登录mysql的的密码     ? host=mysqlserver  mysql服务器的主机名或ip地址     ? db=vsftpd  指定连接msyql的数据库名称     ? table=users 指定连接数据库中的表名     ? usercolumn=name 当做用户名的字段     ? passwdcolumn=password 当做用户名字段的密码     ? crypt=2 密码的加密方式为mysql password()函数加密     
3, set up the normal user of the system, used as a user for virtual user mapping

One, create user, share directory

    #创建虚拟用户映射的系统用户及对应的目录         useradd -s /sbin/nologin -d /var/ftproot vuser     #修改对应的目录权限,因为FTP共享根目录不能有写权限            chmod 555 /var/ftproot     #创建可以上传下载的共享目录        mkdir /var/ftproot/{upload,pub}    #并给vuser用户ACL权限        

Modifying the master configuration file

确认/etc/vsftpd.conf中是否已经启用了以下选项    #支持匿名用户登入        anonymous_enable=YES     #添加下面两项 ,作用是支持虚拟用户映射为某一个系统用户        guest_enable=YES         guest_username=vuser     #修改下面一项,原系统用户无法登录         
4. Start the FTP service
    #启动服务        service vsftpd start    #开机自动启动        chkconfig vsftpd on    #查看端口打开情况    
5,selinux Related Configurations
   1,restorecon  -R /lib64/security    2,setsebool -P ftpd_connect_db 1    3,setsebool -P ftp_home_dir 1    
Final Test
   1,li用户登入测试:成功        [[email protected] ~]# ftp 192.168.136.6        Connected to 192.168.136.6 (192.168.136.6).        220 (vsFTPd 2.2.2)        Name (192.168.136.6:root): li        331 Please specify the password.        Password:        230 Login successful.        Remote system type is UNIX.        Using binary mode to transfer files.    2,wang用户登入测试:成功        [[email protected] ~]# ftp 192.168.136.6        Connected to 192.168.136.6 (192.168.136.6).        220 (vsFTPd 2.2.2)        Name (192.168.136.6:root): wang        331 Please specify the password.        Password:        230 Login successful.        Remote system type is UNIX.        Using binary mode to transfer files.
Third, the implementation of each virtual user has their own different permissions

1. Modify the master configuration file

    vim /etc/vsftpd/vsftpd.conf      添加如下选项,作用是让虚拟用户支持独立权限配置文件,可以自定义存放目录。    

2. Create a permissions profile for each virtual user in the/etc/vsftpd/vusers_config/directory, with the file name corresponding to the user name

    mkdir /etc/vsftpd/vusers_config/     cd  /etc/vsftpd/vusers_config/     touch wang     touch li    "注意:虚拟用户对vsftpd服务的访问权限是通过匿名用户的相关 指令进行的。   "    下面的权限都可以添加至文件中,需要什么权限添加什么权限            #是否支持上传功能            anon_upload_enable={YES|NO}            #是否支持创建文件功能            anon_mkdir_write_enable={YES|NO}             #是否支持删除文件功能            anon_other_write_enable={YES|NO}             #指定虚拟账户登入的共享目录            local_root=/ftproot           例如:让wang用户支持上传,下载,删除文件的权限那么只需要在wang的权限配置文件中添加如下几行        "注意:需确保对应的映射用户对于文件系统有写权限"        vim  wang        anon_upload_enable=YES         anon_other_write_enable=YES        anon_mkdir_write_enable=YES
CENTOS-7 when the FTP server is only a different place, it is necessary to compile and install the Pam_mysql module, the others are similar.

Steps:

 # 安装开发包组    yum -y groupinstall "Development Tools"  # 安装相关依赖包    yum -y install mariadb-devel  pam-devel vsftpd # 去官网下载pam_mysql-0.7RC1.tar.gz源码包            https://sourceforge.net/projects/pam-mysql/ # 解压缩包    tar xvf pam_mysql-0.7RC1.tar.gz  # 进入pam_mysql-0.7RC1/目录开始编译       cd pam_mysql-0.7RC1/  # 运行configure脚本    ./configure     --with-mysql=/usr \     --with-pam=/usr     --with-pam-mods-dir=/lib64/security # 编译开始    make -j 4 # 安装程序    

FTP (vi) Implementing a MySQL-authenticated FTP virtual user

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.