Add mysql authentication for ftp

Keywords: Linux vsftpd MySQL yum vim

1. Install vsftpd. You can install it from yum or source;

2. Install mysql, skip, and view the previous documents;

3. Install PAM MySQL and rely on PAM devel package;

[root@WebA-136 ~]#tar xf pam_mysql-0.7RC1.tar.gz
[root@WebA-136 ~]#yum install pam-devel
[root@WebA-136 ~]#./configure --with-mysql=/usr/local/mysql --with-openssl
[root@WebA-136 ~]#make && make install
[root@WebA-136 ~]#less README
[root@WebA-136 ~]#vim /etc/pam.d/vsftpd.mysql
[root@WebA-136 ~]#cat /etc/pam.d/vsftpd.mysql
[root@WebA-136 ~]#auth required /lib/security/pam_mysql.so user=vsftpd passwd=123456 host=192.168.146.136 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=3 md5=yes
[root@WebA-136 ~]#account required /lib/security/pam_mysql.so user=vsftpd passwd=123456 host=192.168.146.136 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=3 md5=yes

Reconfigure database

create database vsftp;
grant selcet on vsftp.* to vsftpd@192.168.146.136 identified by '123456';
Create table
create table users(
id smallint auto_increment not null,
name char(20) binary not null,
password char(48) binary not null,
primary key(id))
;
View table structure
desc users;
You also need to insert a virtual user here
INSERT INTO users(name,password)VALUES('tom',md5(123456)),('jack',md5(123456));

4. Modify the vsftpd.conf configuration file to make it suitable for mysql authentication

Create user first

useradd -s /sbin/nologin -d /var/ftp-test vsftp
chmod go+rx /var/ftp-test

Make sure the following options are turned on in vsftpd.conf

[root@WebA-136 ~]#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
Add the following options
guest_enable=YES
guest_username=vsftpd######All virtual users must be mapped to a specified user.
listen=YES
pam_service_name=vsftpd.mysql

Note: different access rights can be set according to different virtual users

New in vsftpd.conf

user_config_dir=/etc/vsftpd/test
Add a file with the same name as the virtual user in the / etc/vsftpd/test directory
cd /etc/vsftpd/test
vim tom
anon_upload_enable=YES
anon_mkdir_write_enable=YES
vim jack
anon_upload_enable=NO
anon_mkdir_write_enable=NO


Posted by spaddict on Sat, 02 Nov 2019 11:51:39 -0700