Linux, especially Ubuntu, has many FTP server applications. And PureFTPd is one of the most stable and good FTP server applications. We can configure PureFTPd to use virtual users stored on a MySQL database rather than using system users.
This is much more performant and allows having thousands of FTP users on a single machine. In addition to that I will show the use of quota and upload/download bandwidth limits with this setup. Passwords will be stored encrypted as MD5 strings in the database.
Please follow these steps to install PureFTPd, MySQL and PhpMyAdmin:
1. Open your terminal and log in as root.
su
2. Install MySQL, PhpMyAdmin and Apache; if you already have these, skip this step.
apt-get install mysql-server mysql-client phpmyadmin apache2
Please follow the wizard; you will be asked for the MySQL root password and configuration.
3. Install PureFTPd.
apt-get install pure-ftpd-mysql
4. Create ftpgroup and ftpuser.
groupadd -g 2001 ftpgroup
useradd -u 2001 -s /bin/false -d /bin/null -c "pureftpd user" -g ftpgroup ftpuser
5. Log in to your MySQL with this command:
mysql -u root -p
You will be asked for the root password.
6. Create the pureftpd database and pureftpd user; run this script:
CREATE DATABASE pureftpd;
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON pureftpd.* TO 'pureftpd'@'localhost' IDENTIFIED BY 'ftpdpass';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON pureftpd.* TO 'pureftpd'@'localhost.localdomain' IDENTIFIED BY 'ftpdpass';
FLUSH PRIVILEGES;
Please change ftpdpass to a password you like.
7. Change your active database to pureftpd:
USE pureftpd;
8. Create the ftpd table:
CREATE TABLE ftpd (
User varchar(16) NOT NULL default '',
status enum('0','1') NOT NULL default '0',
Password varchar(64) NOT NULL default '',
Uid varchar(11) NOT NULL default '-1',
Gid varchar(11) NOT NULL default '-1',
Dir varchar(128) NOT NULL default '',
ULBandwidth smallint(5) NOT NULL default '0',
DLBandwidth smallint(5) NOT NULL default '0',
comment tinytext NOT NULL,
ipaccess varchar(15) NOT NULL default '*',
QuotaSize smallint(5) NOT NULL default '0',
QuotaFiles int(11) NOT NULL default 0,
PRIMARY KEY (User),
UNIQUE KEY User (User)
) TYPE=MyISAM;
9. Quit from MySQL by running this command:
quit;
10. Configure PureFTPd. First create mysql.conf under pureftpd. Run this command:
cp /etc/pure-ftpd/db/mysql.conf /etc/pure-ftpd/db/mysql.conf_orig
cat /dev/null > /etc/pure-ftpd/db/mysql.conf
vim /etc/pure-ftpd/db/mysql.conf
11. Open /etc/pure-ftpd/db/mysql.conf; it should look like this:
MYSQLSocket /var/run/mysqld/mysqld.sock
#MYSQLServer localhost
#MYSQLPort 3306
MYSQLUser pureftpd
MYSQLPassword ftpdpass
MYSQLDatabase pureftpd
#MYSQLCrypt md5, cleartext, crypt() or password() - md5 is VERY RECOMMENDABLE uppon cleartext
MYSQLCrypt md5
MYSQLGetPW SELECT Password FROM ftpd WHERE User="L" AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "R")
MYSQLGetUID SELECT Uid FROM ftpd WHERE User="L" AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "R")
MYSQLGetGID SELECT Gid FROM ftpd WHERE User="L"AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "R")
MYSQLGetDir SELECT Dir FROM ftpd WHERE User="L"AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "R")
MySQLGetBandwidthUL SELECT ULBandwidth FROM ftpd WHERE User="L"AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "R")
MySQLGetBandwidthDL SELECT DLBandwidth FROM ftpd WHERE User="L"AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "R")
MySQLGetQTASZ SELECT QuotaSize FROM ftpd WHERE User="L"AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "R")
MySQLGetQTAFS SELECT QuotaFiles FROM ftpd WHERE User="L"AND status="1" AND (ipaccess = "*" OR ipaccess LIKE "R")
Make sure that you replace the string ftpdpass with the real password for the MySQL user pureftpd in the line MYSQLPassword! Please note that we use md5 as MYSQLCrypt method, which means we will store the users’ passwords as an MD5 string in the database which is far more secure than using plain text passwords!
12. Create the file /etc/pure-ftpd/conf/ChrootEveryone which simply contains the string yes:
echo "yes" > /etc/pure-ftpd/conf/ChrootEveryone
13. Create the file /etc/pure-ftpd/conf/CreateHomeDir which again simply contains the string yes:
echo "yes" > /etc/pure-ftpd/conf/CreateHomeDir
14. Create the file /etc/pure-ftpd/conf/DontResolve which again simply contains the string yes:
echo "yes" > /etc/pure-ftpd/conf/DontResolve
15. Restart PureFTPd:
/etc/init.d/pure-ftpd-mysql restart
16. Open your PhpMyAdmin by opening your browser and typing: http://localhost/phpmyadmin
17. Select the pureftpd database, and add a new user to the ftpd table. Run this query:
INSERT INTO `ftpd` (`User`, `status`, `Password`, `Uid`, `Gid`, `Dir`, `ULBandwidth`, `DLBandwidth`, `comment`, `ipaccess`, `QuotaSize`, `QuotaFiles`) VALUES ('ivan', '1', MD5('ivan123'), '2001', '2001', '/home/ivan', '100', '100', '', '*', '50', '0');
18. Now open your FTP client program on your workstation (e.g. FileZilla) and try to connect. As hostname use localhost (or the IP address of the system); the username is ivan, and the password is ivan123.
19. If it is connected, congratulations! Please check all the steps again if you have something wrong.
You can add, edit, or delete users by going to PhpMyAdmin, or make a simple web application to do user management for your FTP server.
Read more about PureFTPd.
