Set up an FTP server on a Linux instance
Install and configure vsftpd on a Linux ECS instance to enable secure FTP file transfers.
Quick deployment
Click Run now to open Terraform Explorer and automatically build an FTP site on an ECS instance.
Prerequisites
An ECS instance that meets the following requirements is created. Create an instance using the wizard.
-
Operating system: Alibaba Cloud Linux 3/2, CentOS 7.x 64-bit, Ubuntu, or Debian.
-
IP address: The instance has a static public IP address or an Elastic IP Address.
Overview
vsftpd (Very Secure FTP Daemon) is an open-source FTP server for UNIX and Linux systems. Key features:
-
High security: strict security audits and multiple mechanisms to prevent common attacks.
-
High performance: handles many concurrent connections with efficient file transfers.
-
Simple configuration: flexible, easy-to-understand options for various needs.
-
IPv6 support: native support for IPv6 networks.
Build the vsftpd service
Alibaba Cloud Linux 3 and 2/CentOS 7.x
Step 1: Install vsftpd
-
Update the system and install
vsftpd.sudo yum update -y sudo yum install vsftpd -y -
Start vsftpd and enable it on boot.
sudo systemctl start vsftpd sudo systemctl enable vsftpd -
Verify the service is running.
netstat -antup | grep ftpThe following output confirms vsftpd is running.
[root@xxx ~]# systemctl start vsftpd [root@xxx ~]# netstat -antup | grep ftp tcp6 0 0 :::21 :::* LISTEN 16387/vsftpdBy default, vsftpd allows anonymous read-only access.
Step 2: Configure vsftpd
-
Create an FTP user. This example uses
ftpuser.sudo useradd -d /data/ftp -s /sbin/nologin ftpuser # Specify the home directory and disable shell access sudo passwd ftpuser -
Create the FTP directory and set permissions.
sudo mkdir -p /data/ftp # Create a custom storage directory sudo chown ftpuser:ftpuser /data/ftp sudo chmod 750 /data/ftp # Permissions must be 755 or 750 -
Edit the
vsftpdconfiguration file.NoteUse passive mode. Most clients are behind firewalls or NAT and cannot expose real IP addresses.
-
Back up the
vsftpdconfiguration file.sudo cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak -
Edit the configuration file.
sudo vim /etc/vsftpd/vsftpd.conf -
Set the basic security configuration.
listen=YES # Enable IPv4 listener anonymous_enable=NO # Disable anonymous access local_enable=YES # Enable local user logon write_enable=YES # Allow file uploads chroot_local_user=YES # Lock users to their home directory allow_writeable_chroot=YES # Resolve chroot write errors -
Append the passive mode configuration.
pasv_enable=YES # Enable passive mode pasv_min_port=40000 # Lower limit of the passive port range pasv_max_port=40100 # Upper limit of the passive port range pasv_address=public_ip_address # Must be set to the server's public IP address
-
-
Restart
vsftpd.sudo systemctl restart vsftpd
Step 3: Set security group rules
Add inbound security group rules for the FTP mode you configured. Add a security group rule.
In active mode, clients behind NAT must expose real IP addresses, which may cause connection failures.
-
Active mode: Allow traffic on port 21.
-
Passive mode: Allow traffic on port 21 and ports 40000–40100 (pasv_min_port to pasv_max_port in /etc/vsftpd/vsftpd.conf). Configuring FTP passive mode ports.
Step 4: Verify the FTP service
Verify the FTP service using an FTP client, browser, or file explorer. This example uses the file explorer.
-
Test the local connection.
Run the following command on the server.
ftp ftpuser@localhostA
Login successfulmessage confirms the connection.# ftp ftpuser@localhost Connected to localhost. 220 (vsFTPd 3.0.5) 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. -
Test the client connection.
On your client machine, open the file explorer and enter the FTP address in the address bar.
The address format is
ftp://<FTP_server_public_IP_address>:21. After a successful connection, the file explorer will display the files in the FTP server's directory.Enter the FTP username and password to log on. You can then upload and download files.
Ubuntu and Debian
Step 1: Install vsftpd
-
Update the system and install
vsftpd.sudo apt update && sudo apt upgrade -y sudo apt install vsftpd -y -
Start
vsftpdand enable it on boot.sudo systemctl start vsftpd sudo systemctl enable vsftpd
Step 2: Configure vsftpd
-
Create a dedicated FTP user.
sudo useradd -m -s /bin/bash ftpuser # Create a user and automatically generate a home directory sudo passwd ftpuser # Set the user password (a strong password is recommended) -
Create the file storage directory and set permissions.
sudo mkdir /home/ftpuser/ftp-files sudo chown ftpuser:ftpuser /home/ftpuser/ftp-files sudo chmod 755 /home/ftpuser/ftp-files -
Back up the original configuration file.
sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.bak -
Edit the configuration file.
sudo nano /etc/vsftpd.confApply the following settings:
# Basic configuration listen=YES anonymous_enable=NO # Disable anonymous access local_enable=YES # Allow local user logon write_enable=YES # Enable write permissions chroot_local_user=YES # Lock users in their home directoryAppend the following:
allow_writeable_chroot=YES # Allow writing to the chroot directory local_root=/home/ftpuser/ftp-files # Specify the root directory for the ftp user # Passive mode configuration (to resolve external network connection issues) pasv_enable=YES pasv_address=# Replace with your public IP address pasv_min_port=40000 pasv_max_port=40100 -
Restart vsftpd.
sudo systemctl restart vsftpd -
vsftpd creates a default ftp user without a password. Set a password for this user.
sudo passwd ftpSet a strong password and skip all other prompts.
-
Add the user to the FTP user allowlist.
echo "ftp" | sudo tee -a /etc/vsftpd.userlist -
Create an FTP directory and set permissions.
-
Create an FTP folder.
sudo mkdir /home/ftp -
Set folder permissions.
This grants full permissions (777). Adjust as needed.
sudo chmod 777 /home/ftp
-
Step 3: Set security group rules
Add inbound security group rules for the FTP mode you configured. Add a security group rule.
In active mode, clients behind NAT must expose real IP addresses, which may cause connection failures.
-
Active mode: Allow traffic on port 21.
-
Passive mode: Allow traffic on port 21 and ports 40000–40100 (pasv_min_port to pasv_max_port in /etc/vsftpd/vsftpd.conf). Set up an FTP site on Windows.
Step 4: Verify the FTP service
Verify the FTP service using an FTP client, browser, or file explorer. This example uses the file explorer.
-
Test the local connection.
Run the following command on the server.
ftp ftpuser@localhostA
Login successfulmessage confirms the connection.# ftp ftpuser@localhost Connected to localhost. 220 (vsFTPd 3.0.5) 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. -
Test the client connection.
On your client machine, open the file explorer and enter the FTP address in the address bar.
The address format is
ftp://<FTP_server_public_IP_address>:21. After a successful connection, the file explorer will display the files in the FTP server's directory.Enter the FTP username and password to log on. You can then upload and download files.
Troubleshooting
|
Issue |
Solution |
|
Timeout after |
Check the public IP address and firewall rules on both client and server. |
|
|
Set directory permissions to 755. |
|
Only empty directories are listed |
Check the |
|
|
Run |
|
Passive mode connection timeout |
Check firewall rules and the pasv_address setting. |
|
Cannot upload files |
Verify directory permissions are 755 or 750. |
Appendix
vsftpd configuration file and parameters
Files in the /etc/vsftpd directory:
-
/etc/vsftpd/vsftpd.conf: Core vsftpd configuration file. -
/etc/vsftpd/ftpusers: Blocklist. Users listed here are denied FTP access. -
/etc/vsftpd/user_list: Allowlist. Users listed here are allowed FTP access.
Parameters in the vsftpd.conf configuration file:
-
Logon control parameters:
Parameter
Description
anonymous_enable=YES
Accepts anonymous users.
no_anon_password=YES
Anonymous users can log on without a password.
anon_root=(none)
Home directory for anonymous users.
local_enable=YES
Accepts local users.
local_root=(none)
Home directory for local users.
-
User permission control parameters:
Parameter
Description
write_enable=YES
Allows file uploads (global).
local_umask=022
File permissions for local user uploads.
file_open_mode=0666
File permissions for uploads. Works with local_umask.
anon_upload_enable=NO
Allows anonymous file uploads.
anon_mkdir_write_enable=NO
Allows anonymous directory creation.
anon_other_write_enable=NO
Allows anonymous edit and delete operations.
chown_username=lightwiter
Owner assigned to files uploaded by anonymous users.