Setup FTP on Ubuntu
Most of the contents comes from How to Setup FTP Server with VSFTPD on Ubuntu 20.04. Much thanks. 🙂
Prologue
Why we need this? We have plenty of tools to transfer files to a remote server, such as Xshell & Xftp, Tabby. I recently comes to this problem as I trying to develop an ASP.NET Core Web API project for Linux, when it is more convenient to publish the product via FTP. So here’s how to set up FTP in Linux.
Setup FTP Service
Step 1. Install vsftpd
Well, everything starts with this tool.
1 | sudo apt update |
Then, it should automatically start. You can verify its status with this.
1 | sudo systemctl status vsftpd |
Step 2. Configure vsftpd
The configuration file for vsftpd is located /etc/vsftpd.conf
, you can refer to VSFTPD.CONF for all available options.
You should have sudo
permission to edit it. And you can use /
in Vim to search for options more efficiently.
Enable FTP Access
For security reason, we should only allow local users to access FTP.
1 | anonymous_enable=NO |
Enable Uploads
Locate and uncomment the write_enable
directive to allow filesystem changes, such as uploading and removing files.
1 | write_enable=YES |
chroot jail
To prevent local FTP users to access files outside of their home directories, uncomment the line starting with chroot_local_user
. It is called a “jail” because users are restricted to their own directory.
1 | chroot_local_user=YES |
It is not enough, though. By default, for security reasons, when chroot is enabled, vsftpd will refuse to upload files if the directory that the users are locked in is writable. There are two solutions for this.
Method 1
This is the recommended one, that is to specify a writeable root. Add the following to entries below chroot_local_user
.
1 | user_sub_token=$USER |
You can set local_root
to /home/$USER
to allow writing for the entire user directory.
Method 2
Or, you can simply add this line. Use this option only if you must grant writable access to your user to its home directory.
1 | allow_writeable_chroot=YES |
Passive FTP Connections
By default, vsftpd uses active mode. To use passive mode, set the minimum and maximum range of ports. Add these two lines to the configuration file.
1 | pasv_min_port=30000 |
Step 3. Restart vsftpd
Save your configurations, and restart vsftpd service.
1 | sudo systemctl restart vsftpd |
Step 4. Opening the Firewall
If you are running a UFW firewall , you’ll need to allow FTP traffic.
1 | sudo ufw allow 20:21/tcp |
Also, open the firewall in your server’s provider’s control panel.
Now, you’re all set! 🥳 You can access FTP with any local user.
Epilogue
There are a few steps ahead, actually, but these are enough for basic use. See How to Setup FTP Server with VSFTPD on Ubuntu 20.04 for more information.
This only enables FTP service, and is not any FTP site. So you cannot visit it in your browser, or even File Explorer. 😞