Installing vsftpd with MySql backend

Posted: June 11, 2011 by Hacking & Relax in Mysql
Tags:

vsftpd is a secure, fast and stable FTP server. In this tutorial we’ll install the server and make create a user database in MySql for virtual users.

1. Install required packages (make sure you have installed MySql)

apt-get install vsftpd libpam-mysql

2. Create database and insert the first user (mysql -u root -p)

CREATE DATABASE ftpd;
USE ftpd;
CREATE TABLE users (username varchar (30) NOT NULL, password varchar(50) NOT NULL, PRIMARY KEY (username)) TYPE=MyISAM;
INSERT INTO users (username, password) VALUES ('user1', PASSWORD('password1'));
GRANT SELECT ON ftpd.users to vsftpd@localhost identified by 'yourpassword';
exit;

Replace yourpassword with a strong password used later by vsftpd to authenticate

3. Configure vsftpd (pico /etc/vsftpd.conf)

Edit or add these variables in the config file and leave everything else with the default values.

anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
nopriv_user=vsftpd
virtual_use_local_privs=YES
guest_enable=YES
user_sub_token=$USER
local_root=/var/www/$USER
chroot_local_user=YES
hide_ids=YES
guest_username=vsftpd

Set the local_root to the parent directory where the user’s home directories are located

4. Configure PAM to check the MySql database for users (pico /etc/pam.d/vsftpd)

auth required pam_mysql.so user=vsftpd passwd=yourpassword host=localhost db=ftpd table=users usercolumn=username passwdcolumn=password crypt=2
account required pam_mysql.so user=vsftpd passwd=yourpassword host=localhost db=ftpd table=users usercolumn=username passwdcolumn=password crypt=2

Make sure you remove everything else from the file

5. Create a local user that’s used by the virtual users to authenticate

useradd --home /home/vsftpd --gid nogroup -m --shell /bin/false vsftpd

6. Restart vsftpd

/etc/init.d/vsftpd restart

7. Create user’s home directory since vsftpd doesn’t do it automatically

mkdir /var/www/user1
chown vsftpd:nogroup /var/www/user1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s