Virtual Hosting With vsftpd And MySQL On Debian Etch - Page 2
4 Configure vsftpd
First we create a non-privileged user called vsftpd (with the homedir /home/vsftpd) belonging to the group nogroup. We will run vsftpd under this user, and the FTP directories of our virtual users will be in the /home/vsftpd directory (e.g. /home/vsftpd/user1, /home/vsftpd/user2, etc.).
useradd --home /home/vsftpd --gid nogroup -m --shell /bin/false vsftpd
Then we make a backup of the original /etc/vsftpd.conf file and create our own:
cp /etc/vsftpd.conf /etc/vsftpd.conf_orig
The file should have the following contents:
The configuration options are explained on http://vsftpd.beasts.org/vsftpd_conf.html. The important options for our virtual setup are chroot_local_user, guest_enable, guest_username, user_sub_token, local_root, and virtual_use_local_privs.
With the user_config_dir option you can specify a directory for per-user configuration files that override parts of the global settings. This is totally optional and up to you if you want to use this feature. However, we should create that directory now:
Now we must configure PAM so that it uses the MySQL database to authenticate our virtual FTP users instead of /etc/passwd and /etc/shadow. The PAM configuration for vsftpd is in /etc/pam.d/vsftpd. We make a backup of the original file and create a new one like this:
cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd_orig
Please make sure that you replace the MySQL password with your own one!
Afterwards, we restart vsftpd:
5 Create The First Virtual User
To populate the database you can use the MySQL shell:
mysql -u root -p
Now we create the virtual user testuser with the password secret (which will be stored encrypted using MySQL's PASSWORD function):
INSERT INTO accounts (username, pass) VALUES('testuser', PASSWORD('secret'));
testuser's homedir is /home/vsftpd/testuser; unfortunately vsftpd doesn't create that directory automatically if it doesn't exist. Therefore we create it manually now and make it owned by the vsftpd user and the nogroup group:
Now open your FTP client program on your work station (something like WS_FTP or SmartFTP if you are on a Windows system or gFTP on a Linux desktop) and try to connect. As hostname you use server1.example.com (or the IP address of the system), the username is testuser, and the password is secret.
If you are able to connect - congratulations! If not, something went wrong.
6 Database Administration
For most people it is easier if they have a graphical front-end to MySQL; therefore you can also use phpMyAdmin (in this example under http://server1.example.com/phpmyadmin/) to administrate the vsftpd database.
Whenever you create or modify a user, make sure that you use MySQL's PASSWORD function to encrypt that user's password. Also, when you create a new virtual user, please don't forget to create that user's homedir on the shell, as shown at the end of the previous chapter.