HowTo: FTP service using MySQL database

Despite some uncertainty that may surround MySQL, personally I still prefer working with this DB to using some other. I have nothing against Postgre, I have simply used MySQL my whole life, and until now I have had no reason to rethink its use.

This time I will teach you how to install an FTP server, but not only that, I will teach you in a not so complex way, how to make users, passwords and other user data be stored in a MySQL database, and not in accounts local.

Why do this like this?

Simple, because when making a backup, reinstalling a server or any other significant change, moving the service would be as simple as copying a configuration file, and exporting the MySQL database to FTP.

To achieve this we will use Pure-FTPd, well ... let's get started 🙂

Installing the FTP service with Pure-FTPd

1. The first thing to do is install the package: pure-ftpd-mysql

In distros like Debian or derivatives: aptitude install pure-ftpd-mysql

2. Once installed, we started the service but we must stop it, to stop it on systems such as Debian or derivatives it is enough with:

/etc/init.d/pure-ftpd-mysql stop

However, I leave you a line that will stop the service regardless of the distro you use:

ps ax | grep pure | grep -v grep | awk '{print $1}' | xargs kill

If you want to understand this line in detail, read This article

Preparing conditions on the MySQL server

I already explained not long ago how to create a database, a user and give that user permissions in the database: Users and permissions in MySQL

Let's move on to what will we do here? ...

1. We will create a database yes, but first we access MySQL:

mysql -u root -p

Here they put the root password and they will access the MySQL terminal.

2. Once inside MySQL we go on to create the database myftpdb:

CREATE DATABASE myftpdb;

Notice the semicolon «;»At the end of the line.

3. Now we will create the user myftpuser and we will give permissions to use user on the database that we just created, this user will have as password myftppassword:

CREATE USER 'myftpuser'@'localhost' IDENTIFIED BY 'myftppassword';
GRANT ALL PRIVILEGES ON myftpdb.* TO 'myftpuser'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES ;

4. Ready, we have created the database, the user and set the permissions. Now we must import the default (or clean) database for this to be complete. To do this, let's first exit MySQL:

exit;

Now let's download the default database that I offer you:

Download DB by default

Or on the server use the following line:

wget http://ftp.desdelinux.net/myftpdb.sql

Ready, we already have it on our server, now it only remains to import your data:

mysql -u root -p myftpdb < myftpdb.sql

And ready!

They can also use some web application like Adminer o PHPMyAdmin to import the database, I leave it to taste.

5. And this is all to have the conditions of our MySQL ready.

Joining FTP with MySQL

Well, we already have the FTP service installed, the MySQL service installed and with our database set ... now we just need, join FTP service with MySQL.

1. First we must download the configuration file that we will use for the aforementioned. In the server terminal let's put the following line:

cd /etc/pure-ftpd/ && wget http://ftp.desdelinux.net/pure-ftpd-mysql.conf

2. Now we start the FTP service telling it to use to authenticate MySQL users, and we will also indicate which configuration file to use to connect to MySQL:

pure-ftpd-mysql -l mysql:/etc/pure-ftpd/pure-ftpd-mysql.conf

And voila 😀

This is enough to install our own FTP server authenticating with a MySQL database.

If you want that whenever the server starts automatically it starts the FTP service, you must put in the file /etc/rc.local the line that we use to execute the FTP, that is, we put in /etc/rc.local this:

pure-ftpd-mysql -l mysql:/etc/pure-ftpd/pure-ftpd-mysql.conf

By the way, you can access FTP using any browser, as well as FTP clients like Filezilla ... and not only that, using file browsers like Nautilus, Dolphin or PCManFM you can also download and upload files 😀

Test user who is in the database

User: testuser

Password: testpassword

How to manage FTP users?

Well, considering that it is a MySQL database, as I said above ... using PHPMyAdmin or Adminer will suffice. Just use your favorite application to manage the database, which contains a single table: users ... and in it are the users, here is a screenshot:

If you want to create a new user, you can duplicate or clone the existing line and change the data that would be different between both users, here I show you a screenshot:

Well ... nothing more to add 🙂

I hope this is useful to you and you know, any questions or suggestions let me know.

regards

PD: In this tutorial we use passwords saved in the database in plain text, if you want more security I recommend you try md5 😉


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   LiGNUxer said

    VERY GOOD!!! A couple of weeks ago I was installing this but with vsftpd and I am not convinced so I'm going to try this to see how it is. Thank you

    1.    KZKG ^ Gaara said

      Thank you friend.
      vsftpd I don't remember when was the last time I used it ... it will be a few years now, ... if I ever used it HAHA. At the moment with PureFTPd I'm more than happy 😀

  2.   proper said

    Very good contribution!

    1.    KZKG ^ Gaara said

      Thanks mate 😀
      You do what you can lol….

  3.   Computer Guardian said

    Uuumm, interesting ... pass me the IP of the DB that I want to have that collection of users and passwords at hand 😉

    Don't be evil, man

    1.    KZKG ^ Gaara said

      I didn't understand hehehe… what IP and DB are you talking about?
      If you mean that this data that I put in the tutorial could also be on a server, yes there you are right ... they are in an FTP service on a virtual PC on my laptop, it has a VERY good firewall (iptables) so … HAHAHAHAHA of course they don't be evil LOL !!!

  4.   Computer Guardian said

    Joroña what a joroña…. it will be more complicated than it seems to take advantage of some vulnerability and capture that data 😉

  5.   Algave said

    Very interesting!! 🙂

    1.    KZKG ^ Gaara said

      Thank you
      This is what I try to differentiate us from the rest of the sites ... that we try to put not so much news but as technical articles 😀

  6.   giskard said

    How fast is this compared to Samba? (local network only)

    1.    LiGNUxer said

      that samba and ftp are 2 different things, ftp is a serious protocol and smb just to facilitate the sharing between win and linux.
      If you are looking for performance on the network, use an FTP service, otherwise use samba just without problems

      1.    KZKG ^ Gaara said

        Exact.
        Let's say that FTP is a bit more serious than Samba, at least in my opinion it is.

        I haven't done any benchmarks at all, but maybe FTP is a bit faster.

        1.    giskard said

          Thank you. I use Samba so that from my Wii console (using wiimc) I can watch movies and series that I download to my computer. But wiimc can also connect to an ftp server. I used Samba because it was the easiest, but I was always intrigued if it would be faster with ftp. I will have to try.

          1.    KZKG ^ Gaara said

            Well, you could also simply mount Apache on your PC, and so the Wii would connect, this must be faster than samba ... and much simpler to configure than FTP 😀

          2.    giskard said

            The wiimc (a Wii Media Player) only accepts Samba and FTP connections.

  7.   Max Steel said

    Excellent. You just need something to these types of articles (and the site in general) to make everything perfect; a CSS template to be able to print the articles to PDF or on paper.

  8.   LiGNUxer said

    I don't know if it will be my own, but the big problem with this is that users can go through all the directories even if I give them a specific directory like "/ var / www / user_site" if they connect by ftp they have access to anywhere from my pc ¬¬
    that is not very safe haha

  9.   LiGNUxer said

    HERE IT IS!!!
    To prevent the users we create from being able to navigate through our entire system, we must add the parameter "-A" when launching the pure ...

    So what we add to the /etc/rc.local that you put in the tutorial is this
    pure-ftpd-mysql -l mysql: /etc/pure-ftpd/pure-ftpd-mysql.conf

    and you have to replace it with this other:
    pure-ftpd-mysql -A -l mysql: /etc/pure-ftpd/pure-ftpd-mysql.conf

    Is it appreciated? ... This new line carries the -A parameter to restrict the directory only to which we assign it and nothing else, it can create but it cannot level up.

    D: this cute pure-fptd

    1.    KZKG ^ Gaara said

      Thanks for the tip 😀

  10.   Roberto said

    Hello, it is possible to implement quotas in this MySQL and FTP implementation, I currently have an ftp server with vsftpd and I have no problem with the quotas, but being a virtual user (created in mysql) will the quotas be valid? and the second is where the files uploaded by users are stored, that is, which are the directories of each user.

    1.    KZKG ^ Gaara said

      In theory you can implement quotas, in fact the database has the fields created for it, and the configuration file of the FTP service has the queries set for this, which in reality I have not tested this

      About where the users would put the files, you define it in the 5th field, see the screenshot: https://blog.desdelinux.net/wp-content/uploads/2012/09/phpmyadmin-screenshot-nuevo-usuario.jpg

  11.   Roberto said

    Thank you very much for your comment, I am going to test this system on a test server and comment on the results, hopefully I can because it is a great method to have everything in order, and with a RAID you have a stable backup system: D.

    1.    KZKG ^ Gaara said

      Thanks to you for the comment 🙂

  12.   Roberto said

    I have a question, I have already managed to install pure-ftp with mysql and quotas, the issue now is how can I suspend an account from the mysql table itself, without modifying its user password or uploaded files.

    1.    KZKG ^ Gaara said

      I can think of two ways, the simplest is to change the value of Status from 1 to 0, in theory if it is at 0 the account is deactivated, try this and tell me 🙂

  13.   birkhoff said

    Roberto, how did you manage to set the fees using this facility? Please share the information.
    Very good entry !!

    1.    Roberto Sotelo said

      Birkhoff, just in my personal blog I created a topic about that, I leave the link for you to review:

      http://aprendelinux.net/instalar-servidor-ftp-pure-ftp-con-cuentas-virtuales-en-mysql/

  14.   klaus said

    Greetings:

    I am trying to follow everything but I get error 501 and most of all to refer to the fact that the password is incorrect when I know that it is fine