Table of Contents

Setting up VSFTPD with MySQL

Install PAM MySQL module

sudo apt-get install libpam-mysql

Create a database to store usernames and passwords

CREATE DATABASE ftp;
GRANT ALL PRIVILEGES ON ftp.* TO 'ftp-admin'@'localhost' IDENTIFIED BY 'f7p@dm1n';
FLUSH PRIVILEGES;

Edit /etc/vsftpd.conf

listen=YES
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
nopriv_user=ftp
chroot_local_user=YES
secure_chroot_dir=/var/run/vsftpd
pam_service_name=vsftpd
rsa_cert_file=/etc/ssl/certs/vsftpd.pem
guest_enable=YES
guest_username=ftp
local_root=/srv/ftp/$USER
user_sub_token=$USER
virtual_use_local_privs=YES
user_config_dir=/etc/vsftpd_user_conf

Configure the PAM module

sudo vi /etc/pam.d/vsftpd

The contents of the file should be

auth required pam_mysql.so user=ftp passwd=f7p@dm1n host=localhost db=ftp table=accounts usercolumn=username passwdcolumn=password crypt=2
account required pam_mysql.so user=ftp passwd=f7p@dm1n host=localhost db=ftp table=accounts usercolumn=username passwdcolumn=password crypt=2

Create users

insert into accounts(username, password) values('hari', PASSWORD('haripriya'));

Create user-specific directories

sudo mkdir /srv/ftp/hari
sudo chown ftp:ftp /srv/ftp/hari

Restart VSFPTD

sudo systemctl restart vsftpd