FTP Server with vsftpd on Ubuntu 22.04 LTS

vsftpd (Very Secure FTP Daemon) is a lightweight, stable and secure FTP server for Unix-like systems. It's designed with security in mind and is the default FTP server for many Linux distributions including Ubuntu.

Prerequisites

  • Ubuntu 22.04 LTS server
  • Minimum 1GB RAM (2GB recommended)
  • Sufficient disk space for your FTP storage needs
  • Root or sudo privileges
  • Static IP address configured
  • Basic knowledge of Linux command line

1 System Preparation

Update your system and set the hostname:

sudo apt update && sudo apt upgrade -y
sudo hostnamectl set-hostname ftp-server
exec bash

Install necessary utilities:

sudo apt install -y wget curl nano ufw

2 Install vsftpd

Install the vsftpd package:

sudo apt install -y vsftpd

Check if vsftpd is running:

sudo systemctl status vsftpd

Enable vsftpd to start on boot:

sudo systemctl enable vsftpd

3 Configure Firewall

Allow FTP through the firewall:

sudo ufw allow 20/tcp
sudo ufw allow 21/tcp
sudo ufw allow 990/tcp
sudo ufw allow 40000:50000/tcp
sudo ufw enable

Verify the firewall status:

sudo ufw status

4 Create FTP Directory and User

Create a dedicated FTP directory:

sudo mkdir -p /var/ftp/pub
sudo chown nobody:nogroup /var/ftp/pub
sudo chmod a-w /var/ftp/pub

Create a directory for authenticated users:

sudo mkdir -p /var/ftp/upload
sudo chmod 777 /var/ftp/upload

Create an FTP user:

sudo useradd -m ftpuser
sudo passwd ftpuser

Create a directory for the user and set permissions:

sudo mkdir -p /home/ftpuser/ftp
sudo chown nobody:nogroup /home/ftpuser/ftp
sudo chmod a-w /home/ftpuser/ftp
sudo mkdir /home/ftpuser/ftp/files
sudo chown ftpuser:ftpuser /home/ftpuser/ftp/files

5 Configure vsftpd

Back up the original configuration file:

sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.backup

Edit the vsftpd configuration file:

sudo nano /etc/vsftpd.conf

Update the configuration with these settings:

# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=NO

# Uncomment this to allow local users to log in.
local_enable=YES

# Uncomment this to enable any form of FTP write command.
write_enable=YES

# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022

# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
#anon_upload_enable=YES

# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES

# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES

# If enabled, vsftpd will display directory listings with the time
# in  your  local  time  zone.  The default is to display GMT. The
# times returned by the MDTM FTP command are also affected by this
# option.
use_localtime=YES

# Activate logging of uploads/downloads.
xferlog_enable=YES

# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES

# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever

# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/vsftpd.log

# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
xferlog_std_format=YES

# You may change the default value for timing out an idle session.
#idle_session_timeout=600

# You may change the default value for timing out a data connection.
#data_connection_timeout=120

# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure

# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES

# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
#ascii_upload_enable=YES
#ascii_download_enable=YES

# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.

# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
chroot_local_user=YES
allow_writeable_chroot=YES

# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option.
#ls_recurse_enable=YES

# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=YES

# This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6
# sockets, you must run two copies of vsftpd with two configuration files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YES

# Add these settings for passive mode
pasv_enable=YES
pasv_min_port=40000
pasv_max_port=50000
pasv_address=your_server_ip
Note: Replace your_server_ip with your server's actual IP address.

6 Configure User Access

Create a user list for vsftpd:

sudo nano /etc/vsftpd.user_list

Add users to allow or deny FTP access (one per line):

# Users to allow FTP access
ftpuser
# Users to deny FTP access
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody

Configure user access in the main configuration:

sudo nano /etc/vsftpd.conf

Add these lines to the configuration:

userlist_enable=YES
userlist_file=/etc/vsftpd.user_list
userlist_deny=NO

7 Enable SSL/TLS Encryption

Generate SSL certificates for secure connections:

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/ssl/private/vsftpd.pem -out /etc/ssl/private/vsftpd.pem

Configure vsftpd to use SSL:

sudo nano /etc/vsftpd.conf

Add these SSL configuration options:

# SSL Configuration
rsa_cert_file=/etc/ssl/private/vsftpd.pem
rsa_private_key_file=/etc/ssl/private/vsftpd.pem
ssl_enable=YES
allow_anon_ssl=NO
force_local_data_ssl=YES
force_local_logins_ssl=YES
ssl_tlsv1=YES
ssl_sslv2=NO
ssl_sslv3=NO
require_ssl_reuse=NO
ssl_ciphers=HIGH

8 Restart and Test vsftpd

Restart the vsftpd service to apply changes:

sudo systemctl restart vsftpd

Check the status to ensure it's running properly:

sudo systemctl status vsftpd

Test the FTP connection from the local machine:

ftp localhost

Test with a client like FileZilla or from the command line:

# Install FTP client if needed
sudo apt install -y ftp

# Connect to the FTP server
ftp your_server_ip

9 Advanced Configuration Options

Limit user upload/download rates:

sudo nano /etc/vsftpd.conf

Add these rate limiting options:

# Limit download bandwidth (bytes per second)
local_max_rate=1000000

# Limit upload bandwidth (bytes per second)
anon_max_rate=500000

# Maximum number of clients
max_clients=50

# Maximum number of connections per IP
max_per_ip=5

Configure logging options:

# Enable detailed logging
log_ftp_protocol=YES

# Custom log file location
vsftpd_log_file=/var/log/vsftpd.log

# Enable dual log format
dual_log_enable=YES

Set connection timeouts:

# Idle session timeout (seconds)
idle_session_timeout=600

# Data connection timeout (seconds)
data_connection_timeout=120

# Login timeout (seconds)
connect_timeout=60

10 Troubleshooting Common Issues

Check vsftpd service status:

sudo systemctl status vsftpd
sudo journalctl -u vsftpd -f

Check vsftpd logs:

sudo tail -f /var/log/vsftpd.log
sudo grep vsftpd /var/log/syslog

Test FTP connectivity:

# Test from local machine
ftp 127.0.0.1

# Test from remote machine
telnet your_server_ip 21

Common issues and solutions:

Issue Solution
Connection refused Check firewall settings and vsftpd status
Authentication failures Verify user exists and is in vsftpd.user_list
Passive mode not working Check passive port range and firewall rules
SSL/TLS connection errors Verify SSL certificate configuration
Command copied to clipboard!