FTP Server

November 19, 2022

File Transfer Protocol (FTP) is a network protocol for transferring copies of files from one computer to another.  FTP (File Transfer Protocol) is used to communicate and transfer files between computers on a TCP/IP (Transmission Control Protocol/Internet Protocol) network, also known as the internet. Users who have been granted access, can receive and transfer files in the File Transfer Protocol server (also known as FTP host/site).  FTP connection needs two parties to establish and communicate on the network. To do that, users need to have permission by providing credentials to the FTP server. Some public FTP servers may not require credentials to access their files. The practice is common in a so-called anonymous FTP.

The connection is a simple relationship acquired between Two points. In FTP, we have two basic connections, to begin with, Control Connection and a Data Connection.  The Control Connection is an Initial Connection which is established as soon as Login Credentials are sent and one of the TCP Port 20 or 21 is opened.  The Data Connection is a connection established for a specific purpose of transferring data.  Along with Two Types of Connections, FTP works in Two different modes, Active and Passive FTP. These modes depend on the device that initiates the Data Connection, Client or Server.  In Active Mode, the Server Initiates the Data Connection, and the Client listens to a random port for incoming data connections from the server.  In Passive Mode, the client initiates the Data connection by sending the data, and the server listens.

FTP was developed during the 1970s and 1980s to support file sharing on TCP/IP and older networks. The protocol follows the client-server model of communication. To transfer files with FTP, a user runs an FTP client program and initiates a connection to a remote computer running FTP server software. After the connection is established, the client can choose to send and/or receive copies of files. An FTP server listens on TCP port 21 for incoming connection requests from FTP clients. When a request is received, the server uses this port to control the connection and opens a separate port for transferring file data.

Basically, the FTP server uses a Client-Server Architecture to transfer files. FTP Server is a Software Application that uses the File Transfer Protocol to share files over the Internet between Client and Host machine. Here we have two machines, the end-user, which is a local host machine and a Destination, which is a remote host. Both machines have the same FTP Server Application Software running.

FTP server Type Files Transmission has many of its Advantages like It allows you to transfer multiple amounts of Files and Directories/Folders.  In the case of Interrupted Connection, your file will not be lost, instead, you’ll be able to resume the transfer where it dropped.  FTP is faster than HTTP.  And finally, you can schedule a File transfer.  FTP does have a few Disadvantages. Transferring multiple files at an instance, simultaneously, will result in a queue and not uploading every file at the same moment.  Few security points like upon using a simple Brute Force Attack, any outsider can gain access.

Your login credentials like Username and Passwords are sent in clear text format.  With easy access, an inexperienced person can accidentally wipe out the FTP or any other major action easily.

The original FTP clients were command-line programs for Unix operating systems. A variation of FTP called Trivial File Transfer Protocol (TFTP) was also developed to support low-end computer systems. Microsoft later released the Windows FTP client with a graphical interface. There are many FTP clients available for different operating systems. A lot of them are free, but there are also premium FTP clients that have extra features, such as the option to automatically transfer files on a set schedule.

An FTP client is a program that allows you to move files between computers. For example, you can create web pages on your PC and use an FTP client to upload the website to the server where it will be hosted. The are are three ways to establish an FTP connection. A  very simple method is using a command-line FTP, such as using Command prompt for Windows or Terminal in Mac/Linux. Developers still use it today for transferring files using FTP.  A user also can use a web browser to communicate with the FTP server. A web browser is more convenient when users want to access large directories on the server. Yet, it’s often less reliable and slower than using a dedicated FTP program.  The most common practice to use FTP, especially for a web developer, is by using an FTP client.  An FTP client provides more freedom compared to the command line and web browser. It is also easier to manage and more powerful compared to the other methods.  There are also more features available whilst using such a client. For example, it allows users to transfer a large file and use the synchronizing utility.

To connect to an FTP server, you need a username and password as set by the server administrator; however, some servers follow a special convention that accepts any client using “anonymous” as its username. Clients identify the FTP server either by its IP address (such as 192.168.1.1) or by its hostname.  You must also select a mode for the FTP transfer. FTP supports two modes of data transfer: plain text (ASCII), and binary. A common error when using FTP is attempting to transfer a binary file (such as an image, program, or music file) while in text mode, causing the transferred file to be unusable.

To set up our FTP server, we need an Operating System that will run on our Raspberry Pi.  We will be using Rasbian in this article.  Installing Raspbian on the Raspberry Pi is pretty straightforward. We’ll be downloading Raspbian and writing the disc image to a microSD card, then booting the Raspberry Pi to that microSD card. For this project, you’ll need a microSD card (go with at least 8 GB), a computer with a slot for it, and, of course, a Raspberry Pi and basic peripherals (a mouse, keyboard, screen, and power source). First, download the Raspbian disc image. You can find the latest version of Raspbian on the Raspberry Pi Foundations website <a href=”https://www.raspberrypi.com/software/operating-systems/” target=”_blank”>here</a>.  The Raspbian disc image is compressed, so you’ll need to unzip it. The file uses the ZIP64 format, so depending on how current your built-in utilities are, you need to use certain programs to unzip it like <a href=”https://www.7-zip.org” target=”_blank”>7-zip</a>.  Unzip the image file that was downloaded and note the location of the unzipped *.img file.

Once the file is unzipped,  we will need an application to write the file to a microSD card.  <a href=”https://ames-pc.com/geek/blogs/blog.html?issue=1949″ target=”_blank”>There are several different applications available for free use, which allows you to flash ISO images to SD cards</a>. In this tutorial, we will use Etcher.  Etcher is a free and open-source utility for flashing images to SD cards & USB drives and supports Windows, macOS, and Linux.  You can download the most recent Etcher for your operating system from <a href=”https://www.balena.io/etcher/#download” target=”_blank”>Etcher’s download page</a>.  Once downloaded and installed,  insert the SD card into your SD card drive and Launch Etcher.  Click on the Select image button and locate the Raspbian files that you noted from the unzipping program you used.  Etcher will auto select the SD card if only one drive is present. Otherwise, if more than one SD card or USB stick are attached, make sure you have selected the correct drive before flashing the image.  Be very careful in this step because Etcher will overwrite any files on the drive you select. Click on the <b>Flash</b> button, and the flashing process will start. Etcher will show a progress bar and ETA while flashing the image. The process may take several minutes, depending on the size of the ISO file and the card speed.  After the writing completes, safely remove the SD Card from the reader.

We need to set up our raspberry pi before we set up our print server. We can either do a normal setup by connecting the monitor, keyboard and mouse to the Raspberry Pi or a headless setup using ssh or vnc. For this article, we will perform a normal setup.  First,  we will connect the Raspberry Pi to a monitor, keyboard, mouse and network cable. Plug the SD card to the Pi.  Turn on the power supply and wait until the OS boots.  Once the Raspberry Pi is booted up,  we will need to make sure that our Raspberry Pi has all the latest updates to the OS. To do this, open a terminal window and type the following 2 commands one after another

<br>

<code>

  <pre>

    Sudo apt-get update

    sudo apt-get upgrade

  </pre>

</code>

<br><br>

After the upgrade is finished,  we will need to reboot the pi.  We can do it from the GUI window or from the terminal.  To reboot from the terminal,  type in <b>sudo reboot</b> in the terminal window.

<br><br>

We can now set up our FTP server on the Pi. Before setting up the FTP server,  we need to make a few system tweaks so that we can use the Pi as a server on the network. We need to set the DHCP client to use a static IP address.  The system is set by default to <b>DHCP</b> where the Raspberry Pi can pick any available network address, which would make it hard to know how to connect to the Raspberry Pi over the network.  We start by editing the DHCP config file by opening the terminal windows and typing in the following:

<br>

<code>

  <pre>

    Sudo nano /etc/dhcpcd.conf

  </pre>

</code>

<br><br>

Scroll to the bottom of the file and add one, or both of the following snippets, depending on whether you want to set a static IP address for a wired connection ( eth0 ) or a wireless connection ( wlan0 ).  In this tutorial,  we will be setting the wired connection ( eth0 ) for a static ip.  Enter the following under <b>eth0</b>

<br>

<code>

  <pre>

    static ip_address=192.168.0.10/24

    static routers=192.168.0.1

    static domain_name_servers=192.168.0.1

  </pre>

</code>

<br><br>

Change <b>static ip_address</b> to the IP address that you want to set your device to. Make sure you keep the <b>/24</b> for your subnet mask.  Change <b>staic routers</b> to the IP address of your gateway which is probably the IP address or your router. Change the <b>static domain_name_server</b> to  the IP address of your DNS which is probably the IP address of your router. You can add multiple IP addresses here separated with a single space.  Once the file has been edited,  save the file by entering <b> Ctrl + X</b> then press <b>Y</b> and <b>Enter</b>.  Again, reboot the pi by typing <b>sudo reboot</b> in the terminal or using the GUI.

We will first install the FTP Server on a machine with a Linux Operating System. Here, we will use <b>VSFTPD</b>, which is an FTP server for Unix-like systems, it is Licensed under GNU General Public License and supports IPv6 and SSL. It also supports FTPS, which is the extended community of FTP with security. VSFTPD stands for <b>V</b>ery <b>S</b>ecure <b>F</b>ile <b>T</b>ransfer <b>P</b>rotocol Daemon VSFTPD is a default FTP Server for Unix Based Operating Systems like Linux Ubuntu, Fedora, CentOS, and RHEL distributions.

Before beginning with the installation for any tool or software application in any Unix based system, it is recommended to run an update command using <b>sudo apt-get update</b>.  Based on the Type of Linux Distribution you are using, the command for installation might vary.For Linux Ubuntu/Debian based systems, you can install VSFTPD as simple as executing the following command:

<code>

  <pre>

    sudo apt-get install vsftpd

  </pre>

</code>

<br><br>

From here we will configure the file for Ubuntu. The configuration file in <b>/etc/vsftpd/</b> folder.  Before editing the default file,   we will make a copy of the original by running  <b>Sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.bak</b>.  Now that we have a copy of the original, we can make some configuration changes. Open the configuration file by executing the command, <b>Sudo nano /etc/vsftpd.conf</b>.  Make the following changes:

<br><

<code>

  <pre>

     anonymous_enable=NO ##Disable the default anonymous login

     local_enable=YES ##Allow local Logins.

     write_enable=YES ##Permission to allow FTP commands

     chroot_local_user=YES ##Allow local users to access files.

     userlist_enable=YES ##Will load the usernames

     userlist_file=/etc/vsftpd.userlist     @@Stores the usernames

     userlist_deny=NO  

     tcp_wrappers=YES         ##Turn on TCP wrappers

  </pre>

</code>

<br><br>

Now save the changes and close the file. Once the changes are saved and the file is closed, we will restart the VSFTPD services in order for the changes to take effect by running the command, <b>systemctl restart vsftpd</b>.