How FTP works
FTP (File Transfer Protocol) is one among the early developed protocols of TCP/IP protocol suite. FTP (File Transfer Protocol) is used to transfer files between an FTP Client and FTP Server. An end user sits at FTP Client computer and upload/download files to/from the File Systems of an FTP Server/FTP Client.
To understand how FTP works, please refer below image.
Before continuing further, please visit following lessons to understand the basics of FTP (File Transfer Protocol).
- What is FTP (File Transfer Protocol)
- FTP Commands
- FTP Response Codes
- Basic concepts of FTP, Control Channel, Data Channel, FTP Active mode and Passive mode
There are two different connection channels for FTP, one is used as Control channel and other one is used as Data channel. Commands and return codes are exchanged using Control channel and actual file transfer happens via Data channel.
FTP File operations
Mainly there are three types of operations in FTP.
- File download from FTP Server to FTP Client : This is also called as retrieving a file. Internally RETR command is used to retrieve a file.
- File upload from FTP Client to FTP Server : This is also called storing a file. Internally STOR command is used to store a file.
- Remote directory listing : Listing the contents of the remote directory. Internally LIST command is used to list the contents of a remote directory.
FTP File structures
FTP STRU command is used to set file transfer structure. Following are the different file structures.
- F (FILE structure) : FILE structure is a continuous stream of bytes.
- R (RECORD structure) : Files divided into records, with fixed or variable length.
- P (PAGE structure) : The file is divided into pages, with each page having a page number and a page header.
FTP Transmission Modes
Following are the different FTP Transmission Modes.
- Stream mode : Transmission is made as continuous stream of bytes.
- Block mode : Transmission is made as blocks. Each block is preceded by a three-byte header.
- Compressed mode : Transmission with data compression.
FTP Transfer Types
Following are the different FTP Transfer Types.
- TYPE A : ASCII, the default format for transferring text files.
- TYPE E : EBCDIC.
- TYPE I : Image, the default format for transferring binary files.
- TYPE N : Nonprintable text files.
- TYPE T : TELNET.
How FTP works - An example with capture screenshots
Following steps describe with capture screenshots of different commands and response codes used in a connection between FTP Client and FTP Server. The IP address of FTP Client is this example is 192.168.10.100 and FTP Server is 192.168.10.10.
Step 1 - After the initial TCP three-way handshake, FTP Server sends a Service Ready message to FTP Client.
Step 2 - FTP Client sends AUTH (Authentication) command (with TLS protection) to FTP Server.
Step 3 - FTP Server refuses TLS based Authentication, because TLS is not configured for use in FTP Server.
Step 4 - FTP Cient sends AUTH (Authentication) command (with SSL protection) to FTP Server.
Step 5 - FTP Server refuses SSL based Authentication, because SSL is also not configured for use in FTP Server.
Step 6 - FTP Client sends username for authentication in plain-text.
Step 7 - Username is okay, but password of the user jajish is also required for authentication.
Step 8 - FTP Client sends password for user jajish in plain-text. Both username and password are sent as plain-text, which is a high security risk.
Step 9 - User logged in.
Step 10 - SYST command (System Type) from FTP Client to FTP Server.
Step 11 - FTP Server replies with System Type.
Step 12 - FEAT command (Get feature list) from FTP Client to FTP Server.
Step 13 - FTP Server replies with feature list.
Step 14 - PWD command (Get current directory) from FTP Client to FTP Server.
Step 15 - FTP Server replies with current directory.
Step 16 - TYPE command (transfer type) from FTP Client to FTP Server. Refer FTP Transfer Types section for more details.
Step 17 - Transfer type set to Image (I), in this case.
Step 18 - PASV command (FTP Passive mode) from FTP Client to FTP Server.
Step 19 - FTP Server agrees for Passive mode. You can see that the FTP Server is using response code 227 for entering to passive mode. The server is also sending the IP address and port number to FTP Client, for making a new TCP connection for Data channel.
Step 20 - MLSD command from FTP Client to FTP Server to list directory. Now another TCP three-way handshake happens to open new TCP connection for Data channel.
Step 21 - Response code 150 is sent from FTP Server to FTP Client, to confirm about new Data channel.
Step 22 - Directory listing is sent from FTP Server to FTP Client, over the new Data channel.