Adding users to your virtual VSFTPD setup

Adding a new user

  1. Add new username and password in /etc/vsftpd/virtualusers.db
  2. Add new username to /etc/vsftpd.allowed_users
  3. Create /etc/vsftpd_user_conf/username file
  4. Edit /etc/vsftpd_user_conf/username to set the chroot correctly

Putting it all together with a script

Holy cow there is a lot to know here, but honestly there is no replacement for a little bit of knowledge. That is why I started this blog. Documenting how to do stuff is a great way to exercise what you think you know.

Ok so after everything is said and done this is usually what you want to happen when you add a user to your server

  1. Add new username and password in /etc/vsftpd/virtualusers.db
  2. Add new username to /etc/vsftpd.allowed_users
  3. Create /etc/vsftpd_user_conf/username file
  4. Edit /etc/vsftpd_user_conf/username to set the chroot correctly
Add ftp-user

I take no responsibility for any logic bugs in this script, I threw it together pretty quick. It could still use a lot more error checking, but this gets the point across

#!/bin/sh
# 3 arguments
# username password websitedir

if [ -z $1 ]; then
  echo "Expected username to be provided as first parameter"
  exit 1
fi

if [ -z $2 ]; then
  echo "Expected password to be provided as second parameter"
  exit 1
fi

if [ -z $3 ]; then
  echo "Expected website directory name as third parameter"
  exit 1
fi

username=$1
password=$2
website=$3

#Create the new user details to be merged into the database
echo $username > newuserdetails.txt
echo $password >> newuserdetails.txt

#Merge the new user into the db
echo "Adding $username to /etc/vsftpd/virtualusers.db"
sudo db_load -T -t hash -f newuserdetails.txt /etc/vsftpd/virtualusers.db
sudo chmod 600 /etc/vsftpd/virtualusers.db

#Remove our temp file
rm newuserdetails.txt

userlocalroot="/var/www/$website"

if ! [ -d $userlocalroot ]; then
  echo "Website directory specified doesn't exist, please set all that up before proceeding."
  exit 1;
fi

echo "Setting local root to $userlocalroot in vsftpd_conf_users file"

sudo echo local_root=$userlocalroot > /etc/vsftpd_user_conf/$username
echo "Adding $username to /etc/vsftpd.allowed_users"
sudo echo $username >> /etc/vsftpd.allowed_users

Next up Removing Users

Adding users to your virtual VSFTPD setup
Share this