Debian Tutorials

Debian Tutorials


Step by step tutorials showing you how to install and configure various applications and services on Debian based Linux distros.

October 2024
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
28293031  

Categories


Installing vsftpd with MySql backend

Ástþór IPÁstþór IP

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

Comments 4
  • Matt
    Posted on

    Matt Matt

    Author

    I get a:

    Disconnected from server
    Connection failed.
    OOPS: priv_sock_get_result

    When trying to connect, if I try to change credentials it denies me access (so its connecting to the database at some point to check username/password).


  • aip
    Posted on

    aip aip

    Author

    Hey Matt

    Do you see any messages from pam in /var/log/auth.log?

    – aip


  • tomehb
    Posted on

    tomehb tomehb

    Author

    Little Script to create user automatic….

    #!/usr/bin/perl
    # Script to Add New Virtual FTP Users & Create a FTP Dir
    # # Version 0.1 – Thomas Stewart Buchanan – 15/02/2010
    use strict;
    use warnings;
    use DBI;

    # MYSQL VARIABLES
    my $database = “DBI:mysql:vsftpd”;
    my $tablename = “accounts”;
    my $user = “vsftpd”;
    my $pw = “ftpuserpass”;

    # GATHER USER DETAILS
    print “Enter the name of the new FTP user:\n”;
    chomp(my $inUser = );
    print “Please now enter a password for user $inUser:\n”;
    chomp(my $inPw = );
    print “Thankyou \n”;

    # PERL MYSQL CONNECT
    my $dbh = DBI->connect($database, $user, $pw) || die “Could not connect to database: $DBI::errstr”;

    # MYSQL QUERY TO INSERT User
    my $queryInsertUser = $dbh->do(“INSERT INTO $tablename (username, pass) VALUES(‘$inUser’, PASSWORD(‘$inPw’))”);

    # DISCONNECTS FROM DATABASE
    $dbh->disconnect || warn “Disconnection failed: $DBI::errstr”;

    # CREATES USER DIR
    mkdir (“/home/vsftpd/$inUser”) || print $!;

    # Sets ownership of the ftp dirs
    my $chown = system(“chown -R vsftpd:nogroup /home/vsftpd”);

    exit;


  • florinfs
    Posted on

    florinfs florinfs

    Author

    Thank you Tomehb

    chomp(my $inUser = ); chomp(my $inPw = );