Set up an FTP server on a Linux instance

更新时间:
复制 MD 格式

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

  1. Update the system and install vsftpd.

    sudo yum update -y 
    sudo yum install vsftpd -y
  2. Start vsftpd and enable it on boot.

    sudo systemctl start vsftpd
    sudo systemctl enable vsftpd 
  3. Verify the service is running.

    netstat -antup | grep ftp

    The following output confirms vsftpd is running.

    [root@xxx ~]# systemctl start vsftpd
    [root@xxx ~]# netstat -antup | grep ftp
    tcp6       0      0 :::21                   :::*                    LISTEN      16387/vsftpd

    By default, vsftpd allows anonymous read-only access.

Step 2: Configure vsftpd

  1. 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 
  2. 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
  3. Edit the vsftpd configuration file.

    Note

    Use passive mode. Most clients are behind firewalls or NAT and cannot expose real IP addresses.

    1. Back up the vsftpd configuration file.

      sudo cp /etc/vsftpd/vsftpd.conf  /etc/vsftpd/vsftpd.conf.bak
    2. Edit the configuration file.

      sudo vim /etc/vsftpd/vsftpd.conf
    3. 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
    4. 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
  4. 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.

  1. Test the local connection.

    Run the following command on the server.

    ftp ftpuser@localhost 

    A Login successful message 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.
  2. 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

  1. Update the system and install vsftpd.

    sudo apt update && sudo apt upgrade -y
    sudo apt install vsftpd -y
  2. Start vsftpd and enable it on boot.

    sudo systemctl start vsftpd
    sudo systemctl enable vsftpd

Step 2: Configure vsftpd

  1. 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)
  2. 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
  3. Back up the original configuration file.

    sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.bak
  4. Edit the configuration file.

    sudo nano /etc/vsftpd.conf

    Apply 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 directory

    Append 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
  5. Restart vsftpd.

    sudo systemctl restart vsftpd
  6. vsftpd creates a default ftp user without a password. Set a password for this user.

     sudo passwd ftp

    Set a strong password and skip all other prompts.

  7. Add the user to the FTP user allowlist.

     echo "ftp" | sudo tee -a /etc/vsftpd.userlist
  8. Create an FTP directory and set permissions.

    1. Create an FTP folder.

       sudo mkdir /home/ftp
    2. 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.

  1. Test the local connection.

    Run the following command on the server.

    ftp ftpuser@localhost 

    A Login successful message 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.
  2. 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 227 Entering Passive Mode

Check the public IP address and firewall rules on both client and server.

550 Permission denied

Set directory permissions to 755.

Only empty directories are listed

Check the chroot_local_user configuration.

500 OOPS: vsftpd: refusing to run with writable root

Run chmod a-w /data/ftp.

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.