Very secure FTP daemon (vsftpd) is a lightweight, safe, and easy-to-use FTP server software for Linux. This topic describes how to install and configure vsftpd on a Linux Elastic Compute Service (ECS) instance.
Prerequisites
An ECS instance is created and assigned a public IP address. For information about how to create an ECS instance, see Creation methods.
Background information
FTP is a protocol that is used to transfer files. FTP is based on a client-server architecture and supports the following work modes:
Active mode: The client sends port information to the FTP server, and the server establishes a connection to the port.
Passive mode: The FTP server enables a port and sends the port information to the client. The client initiates a connection to the port, and the server accepts the connection.
Most FTP clients are located in LANs, have no independent public IP addresses, and are protected by firewalls. As a result, FTP servers in active mode cannot easily establish connections to the clients. If you do not have special requirements, we recommend that you use passive mode for the FTP server.
FTP supports the following authentication modes:
Anonymous user mode: In this mode, users can log on to the FTP server without the need for a username or password. This is the least secure authentication mode. In most cases, this mode is used to save unimportant public files. We recommend that you do not use this mode to save files in a production environment
Local user mode: In this mode, users must have local Linux accounts. This mode is more secure than the anonymous user mode.
Virtual user mode: Virtual users are dedicated users of the FTP server. Virtual users can access only the FTP service that the Linux system provides and cannot access other resources of the system. This enhances the security of the FTP server.
In this topic, vsftpd is configured for local user access in passive mode. For information about how to configure an FTP server to allow anonymous users to access the FTP server and how to use third-party FTP client tools, see the FAQ section in the "Manually build an FTP site on a CentOS 7 instance" topic.
In this topic, the following resources are used in the procedure:
Instance type: ecs.g6.large
Operating system: CentOS 8.2 64-bit
vsftpd: 3.0.3
The commands and parameters may vary based on the versions of your software.
Step 1: Install vsftpd
Connect to a Linux instance.
For more information, see Connection methods.
Change the CentOS 8 repository address.
CentOS 8 reached end of life (EOL). In accordance with Linux community rules, all content was removed from the following CentOS 8 repository address: http://mirror.centos.org/centos/8/. If you continue to use the default CentOS 8 repository on Alibaba Cloud, an error is reported. To use specific installation packages of CentOS 8, change the CentOS 8 repository address. For more information, see Change CentOS 8 repository addresses. .
Run the following command to install vsftpd:
dnf install -y vsftpd
If the following command output is returned, vsftpd is installed.
Run the following command to enable the FTP service to automatically start on system startup:
systemctl enable vsftpd.service
Run the following command to start the FTP service:
systemctl start vsftpd.service
NoteIf the system returns the Job for vsftpd.service failed because the control process exited with error code error message when the preceding command is run, check whether the following issues occur and troubleshoot the issues.
If the network environment does not support IPv6 addresses, run the vim /etc/vsftpd/vsftpd.conf command to change the value of listen_ipv6 from
YES
toNO
.If the MAC address that is specified for a network interface controller (NIC) in the /etc/sysconfig/network-scripts/ifcfg-xxx configuration file is not the actual MAC address of the NIC, run the ifconfig command to query the MAC addresses of NICs. Then, add
HWADDR=<Actual MAC address of the NIC>
to the file or change HWADDR in the file to the actual MAC address of the NIC.
Run the following command to query the listening port of the FTP service:
netstat -antup | grep ftp
If the following command output is returned, the FTP service is started and listens on port 21.
By default, the local user mode is enabled. To use the FTP service, configure other parameters.
Step 2: Configure vsftpd
In this example, vsftpd is configured in passive and local user modes to ensure data security.
Run the following command to create a Linux user for the FTP service.
In this example, the
ftptest
user is created.adduser ftptest
Run the following command to modify the password of the
ftptest
user:passwd ftptest
Follow the command line instructions to modify the password of the user.
Run the following command to create a file directory for the FTP service:
mkdir /var/ftp/test
Run the following command to create a test file.
The test file is used when the FTP client accesses the FTP server.
touch /var/ftp/test/testfile.txt
Run the following command to change the owner of the
/var/ftp/test
directory toftptest
:chown -R ftptest:ftptest /var/ftp/test
Modify the
vsftpd.conf
configuration file.Run the following command to open the configuration file of vsftpd.
If vsftpd is installed by running the
apt install vsftpd
command, the path of the configuration file is/etc/vsftpd.conf
.vim /etc/vsftpd/vsftpd.conf
Press the I key to enter the Insert mode.
Enable passive mode for the FTP server.
Configure the following parameters:
ImportantWhen you modify or add information in the configuration file, take note of the format. For example, an extra space may cause the service to fail to restart.
#Use the default values for parameters other than the following parameters: #Modify the values of the following parameters: #Disable anonymous users from logging on to the FTP server. anonymous_enable=NO #Allow local users to log on to the FTP server. local_enable=YES #Listen on IPv4 sockets. listen=YES #Add a number sign (#) to the beginning of the line to comment out the following parameter: #Disable listening on IPv6 sockets. #listen_ipv6=YES #Add the following parameters to the end of the configuration file: #Specify the directory of a local user after the local user logs on. local_root=/var/ftp/test #Limit all users to the home directory after the users log on. chroot_local_user=YES #Use a list to specify exception users. Exception users are not limited to the home directory after they log on. chroot_list_enable=YES #Specify a file to store the list of exception users. chroot_list_file=/etc/vsftpd/chroot_list #Enable passive mode. pasv_enable=YES allow_writeable_chroot=YES #Enter the public IP address of the Linux instance that is used in this topic. pasv_address=<Public IP address of the FTP server> #Specify the lowest possible port that is sent to the FTP clients for passive mode connections. We recommend that you use ports in a high range, such as 50000 to 50010. These ports provide more secure access to the FTP server. pasv_min_port=<port number> #Specify the highest possible port that is sent to the FTP clients for passive mode connections. pasv_max_port=<port number>
For information about more parameters, see the vsftpd configuration file and parameters section in this topic.
Press the
Esc
key, enter:wq
, and then press theEnter
key to save and close the configuration file.
Create the
chroot_list
file and write the list of exception users to the file.Run the following command to create the
chroot_list
file:vim /etc/vsftpd/chroot_list
Press the I key to enter Insert mode.
Enter the list of exception users. Exception users are not limited to the home directory and can access other directories.
Press the
Esc
key, enter:wq
, and then press theEnter
key to save and close the configuration file.
ImportantIf exception users do not exist, you must still create the
chroot_list
file. The file can be empty.Run the following command to restart vsftpd:
systemctl restart vsftpd.service
Step 3: Configure security groups
After you build the FTP site, add inbound rules to the security groups of the instance to allow traffic on the following FTP ports. For more information, see Add a security group rule.
Most clients are located in LANs and can map private IP addresses to public IP addresses to communicate with external resources. Therefore, the IP addresses that are returned by the ipconfig or ifconfig command may not be the actual public IP addresses of the clients. If you cannot log on to the FTP server from a client, check the public IP address of the client.
In passive mode, you must allow traffic on port 21 and all ports in the port range that is specified by pasv_min_port and pasv_max_port in the /etc/vsftpd/vsftpd.conf
configuration file. The following table describes how to configure inbound security group rules.
Rule direction | Action | Protocol type | Port range | Authorization object |
Inbound | Allow | Custom TCP | 21/21 | The public IP addresses of all clients that want to access the FTP server. Separate the IP addresses with commas (,). To allow all clients to access the FTP server, specify 0.0.0.0/0 as an authorization object. |
Inbound | Allow | Custom TCP | pasv_min_port/pasv_max_port. Example: 50000/50010. | The public IP addresses of all clients that want to access the FTP server. Separate the IP addresses with commas (,). To allow all clients to access the FTP server, specify 0.0.0.0/0 as an authorization object. |
Step 4: Check whether you can access the FTP server from the client
You can use FTP clients, Windows command-line tools, or browsers to check whether the FTP server is reachable. In this example, an on-premises host that runs a Windows Server 2012 R2 64-bit operating system is used as an FTP client to show how to access the FTP server.
On the on-premises host, open This Computer.
In the address bar, enter
ftp://<Public IP address of the FTP server>:<FTP port>
. In this example, the public IP address of the Linux instance is used. Example:ftp://121.43.XX.XX:21
.In the Log on as dialog box, enter the FTP username and password that you configured and then click Logon.
After you log on to the FTP server, you can view the files in the specified directory on the FTP server. For example, you can view the test file named testfile.txt.
vsftpd configuration file and parameters
The following section describes the files in the /etc/vsftpd
directory:
/etc/vsftpd/vsftpd.conf
is the core configuration file of vsftpd./etc/vsftpd/ftpusers
is the blacklist file. Users specified in this file are not allowed to access the FTP server./etc/vsftpd/user_list
is the whitelist file. Users specified in this file are allowed to access the FTP server.
The following tables describe the parameters that are used in the vsftpd.conf
configuration file.
The following table describes the parameters for logon control.
Parameter
Description
anonymous_enable=YES
Accepts anonymous users.
no_anon_password=YES
Anonymous users do not need a password to log on to the FTP server.
anon_root= (none)
Specifies the home directory of anonymous users.
local_enable=YES
Accepts local users.
local_root= (none)
Specifies the home directory of local users.
The following table describes the parameters that are used to manage the permissions of users.
Parameter
Description
write_enable=YES
Allows all users to upload files.
local_umask=022
Grants local users permissions to upload files.
file_open_mode=0666
Uses umask to grant permissions to upload files.
anon_upload_enable=NO
Allows anonymous users to upload files.
anon_mkdir_write_enable=NO
Allows anonymous users to create directories.
anon_other_write_enable=NO
Allows anonymous users to modify and delete files.
chown_username=lightwiter
Specifies the ownership of files that are uploaded by anonymous users.