01.06.2025
Setting Up an FTP Server (vsftpd) with Virtual Users
If a separate drive is used for storage, mount it to a dedicated directory beforehand (for example, /ftpsd). In this guide, the main FTP storage root directory is /ftpsd/FTP.
Installing Required Components
Switch to the superuser (root) mode:
user@COMPXX:~\$ su -
Update the package list and install vsftpd, the PAM module for password file authentication, and the htpasswd utility:
root@COMPXX:~# apt-get update && apt-get install -y vsftpd libpam-pwdfile apache2-utils
Stop the vsftpd service while configuring it:
root@COMPXX:~# systemctl stop vsftpd
Configuring Authentication and Virtual Users
Create a backup of the default PAM configuration for vsftpd:
root@COMPXX:~# cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak
Open /etc/pam.d/vsftpd, delete all existing content, and insert only the following two lines:
root@COMPXX:~# nano /etc/pam.d/vsftpd
auth required pam_pwdfile.so pwdfile /etc/vsftpwd
account required pam_permit.so
Creating Users
Create the /etc/vsftpwd password file and add the first virtual user (the -c flag creates a new file — use it only once):
root@COMPXX:~# htpasswd -c -d /etc/vsftpwd username1
To add any subsequent users, run the command without the -c flag:
root@COMPXX:~# htpasswd -d /etc/vsftpwd username2
Restrict access to the password hash file so other system users cannot read it:
root@COMPXX:~# chmod 600 /etc/vsftpwd
Note: The
cryptalgorithm (-dflag) only evaluates the first 8 characters of a password. If you need longer passwords, generate a SHA-512 hash usingecho "username:$(openssl passwd -6)" >> /etc/vsftpwd.
Create the allowed users list /etc/vsftpd.userlist and enter the created usernames (one per line):
root@COMPXX:~# nano /etc/vsftpd.userlist
username1
username2
Configuring Directories and Permissions
Create the shared /ftpsd/FTP root folder and remove write permissions from it (a vsftpd security requirement when using chroot):
root@COMPXX:~# mkdir -p /ftpsd/FTP
root@COMPXX:~# chown nobody:nogroup /ftpsd/FTP
root@COMPXX:~# chmod a-w /ftpsd/FTP
Create personal home directories for each user and set their owner to www-data:
root@COMPXX:~# mkdir -p /ftpsd/FTP/username1 /ftpsd/FTP/username2
root@COMPXX:~# chown -R www-data:www-data /ftpsd/FTP/*
root@COMPXX:~# chmod -R 755 /ftpsd/FTP/*
Removing Users
To completely remove a virtual user and their home directory, run:
root@COMPXX:~# htpasswd -D /etc/vsftpwd username1
root@COMPXX:~# rm -rf /ftpsd/FTP/username1
Also remember to remove the corresponding username line from /etc/vsftpd.userlist.
Configuring the vsftpd Service
Back up the original /etc/vsftpd.conf file and clear it before adding your configuration:
root@COMPXX:~# cp /etc/vsftpd.conf /etc/vsftpd.conf.bak
root@COMPXX:~# nano /etc/vsftpd.conf
Update /etc/vsftpd.conf to match the following configuration:
# Enable IPv4 and disable IPv6
listen=YES
listen_ipv6=NO
# Hide real file owners and configure PAM
hide_ids=YES
seccomp_sandbox=NO
pam_service_name=vsftpd
# Isolate each user within their own directory
user_sub_token=\$USER
local_root=/ftpsd/FTP/\$USER
secure_chroot_dir=/var/run/vsftpd/empty
chroot_local_user=YES
allow_writeable_chroot=YES
# Session and data transfer timeouts
idle_session_timeout=600
data_connection_timeout=120
# Passive mode and port range
pasv_enable=YES
pasv_min_port=62000
pasv_max_port=62999
# If the server is behind NAT (a router or cloud provider), uncomment and set your public IP:
# pasv_address=YOUR_PUBLIC_IP
# Port, time, and new file permission settings
use_localtime=YES
connect_from_port_20=YES
local_umask=022
# UTF-8 support and disable ASCII mode (prevents corruption of archives and media files)
utf8_filesystem=YES
ascii_upload_enable=NO
ascii_download_enable=NO
# Login modes and virtual users
anonymous_enable=NO
local_enable=YES
write_enable=YES
guest_enable=YES
guest_username=www-data
virtual_use_local_privs=YES
# Whitelist of users allowed to log in
userlist_enable=YES
userlist_file=/etc/vsftpd.userlist
userlist_deny=NO
dirmessage_enable=YES
# Logging
xferlog_enable=YES
xferlog_file=/var/log/vsftpd.log
xferlog_std_format=YES
# SSL certificates (disabled by default)
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO
Configuring the Firewall (UFW)
If the UFW firewall is used on the server, install it (if needed) and open the ports for SSH as well as active and passive FTP modes:
root@COMPXX:~# apt-get install -y ufw
root@COMPXX:~# ufw allow OpenSSH
root@COMPXX:~# ufw allow 20/tcp
root@COMPXX:~# ufw allow 21/tcp
root@COMPXX:~# ufw allow 62000:62999/tcp
Enable the firewall and check the active rules:
root@COMPXX:~# ufw enable
root@COMPXX:~# ufw status
Starting and Verifying the Service
Start vsftpd and enable it at boot:
root@COMPXX:~# systemctl enable --now vsftpd
root@COMPXX:~# systemctl status vsftpd
Security Recommendation: Standard FTP transmits usernames, passwords, and data in plain text. If the server is accessible over the internet, it is strongly recommended to enable FTPS encryption (
ssl_enable=YES) or use SFTP over SSH.