In this tutorial we’ll install the Debian Linux 9 (squeeze), Apache 2 with mpm-itk (to run each web as a isolated user),...
Installing vsftpd with MySql backend
Ástþór IPvsftpd 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
-
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;