Run an FTP server
The easiest way to run an FTP server is to run a Python program as an FTP server.
Install required packages
SSH into your server as root
and install the Python
pyftpdlib
library.
sudo apt-get install python3-pyftpdlib
sudo apt-get install python3-pyftpdlib
sudo apt-get install python3-pyftpdlib
sudo apt-get install python-pyftpdlib
sudo apt-get install python-pyftpdlib
sudo apt-get install python-pyftpdlib
After installing the required package,
log out of the server as root
.
The rest of the steps in this guide should be performed
while logged in as an app’s system user.
Create and start the FTP server
Now, SSH into your server as your app’s system user and create a file
named ftpserver.py
in the system user’s home directory.
In the file, copy the script shown below.
Change the FTP_USER
, FTP_PASSWORD
, and FTP_DIRECTORY
defined near the top of the file.
from pyftpdlib.authorizers import DummyAuthorizerfrom pyftpdlib.handlers import FTPHandlerfrom pyftpdlib.servers import FTPServer
# The port the FTP server will listen on.# This must be greater than 1023 unless you run this script as root.FTP_PORT = 2121
# The name of the FTP user that can log in.FTP_USER = "myuser"
# The FTP user's password.FTP_PASSWORD = "change_this_password"
# The directory the FTP user will have full read/write access to.FTP_DIRECTORY = "/srv/users/SYSUSER/apps/APPNAME/public/"
def main(): authorizer = DummyAuthorizer()
# Define a new user having full r/w permissions. authorizer.add_user(FTP_USER, FTP_PASSWORD, FTP_DIRECTORY, perm='elradfmw')
handler = FTPHandler handler.authorizer = authorizer
# Define a customized banner (string returned when client connects) handler.banner = "pyftpdlib based ftpd ready."
# Optionally specify a range of ports to use for passive FTP connections. #handler.passive_ports = range(60000, 65535)
address = ('', FTP_PORT) server = FTPServer(address, handler)
server.max_cons = 256 server.max_cons_per_ip = 5
server.serve_forever()
if __name__ == '__main__': main()
You can now start the FTP server.
python3 /srv/users/SYSUSER/ftpserver.py >>/srv/users/SYSUSER/ftpserver.log 2>&1 &
python3 /srv/users/SYSUSER/ftpserver.py >>/srv/users/SYSUSER/ftpserver.log 2>&1 &
python3 /srv/users/SYSUSER/ftpserver.py >>/srv/users/SYSUSER/ftpserver.log 2>&1 &
python /srv/users/SYSUSER/ftpserver.py >>/srv/users/SYSUSER/ftpserver.log 2>&1 &
python /srv/users/SYSUSER/ftpserver.py >>/srv/users/SYSUSER/ftpserver.log 2>&1 &
python /srv/users/SYSUSER/ftpserver.py >>/srv/users/SYSUSER/ftpserver.log 2>&1 &
The above command will start the FTP server
and will log to a file named ftpserver.log
in the system user’s home directory.
For more information on customizing the FTP server, see the pyftpdlib documentation.
You can verify your FTP server is listening for connections with the following command.
netstat -npl --inet | grep 2121
The output will look like the following.
tcp 0 0 0.0.0.0:2121 0.0.0.0:* LISTEN 29972/python
To stop the FTP server, first get the PID of the FTP server.
ps -ef | grep ftpserver | grep -v grep | awk '{print $2}'
Then use the kill
command to kill the process.
kill PID_FROM_OUTPUT_OF_ABOVE_COMMAND
Open the FTP port in the firewall
Open the FTP port you configured in the above script in your server’s firewall. By default, the above script is using port 2121. Additionally, if you uncommented the passive port range line in the script to enable passive FTP, you also need to open ports 60000-65535 in your server’s firewall.
Learn more about customizing a server’s firewall.
You may also need to open the ports in your cloud provider’s firewall.
Start the FTP server on boot
To start your FTP server automatically when your server is rebooted, SSH into the server as your app’s system user and create the following cron job.
@reboot python3 /srv/users/SYSUSER/ftpserver.py >>/srv/users/SYSUSER/ftpserver.log 2>&1
@reboot python3 /srv/users/SYSUSER/ftpserver.py >>/srv/users/SYSUSER/ftpserver.log 2>&1
@reboot python3 /srv/users/SYSUSER/ftpserver.py >>/srv/users/SYSUSER/ftpserver.log 2>&1
@reboot python /srv/users/SYSUSER/ftpserver.py >>/srv/users/SYSUSER/ftpserver.log 2>&1
@reboot python /srv/users/SYSUSER/ftpserver.py >>/srv/users/SYSUSER/ftpserver.log 2>&1
@reboot python /srv/users/SYSUSER/ftpserver.py >>/srv/users/SYSUSER/ftpserver.log 2>&1
Learn more about how to use cron.