File Transfer Protocol (FTP)
1. Overview and Design Philosophy
The File Transfer Protocol (FTP) (RFC 959) is an Application Layer protocol designed to transfer files reliably, securely, and efficiently between two computers across a TCP/IP network.
Unlike most client-server protocols (which use a single communication channel), FTP is famous for its Dual-Connection Architecture (Out-of-Band Control): it uses two completely separate parallel TCP connections for a single user session:
FTP CLIENT FTP SERVER
+-------------------+ +-------------------+
| User Interface | | Server Process |
+-------------------+ +-------------------+
| Control Process | <==== 1. Control Connection (TCP 21) =====> | Control Process |
| | (Commands: USER, PASS, RETR, QUIT) | (PI - Protocol Int)
+-------------------+ +-------------------+
| Data Process | <==== 2. Data Connection (TCP 20/Eph) ====> | Data Process |
| (DTP) | (Bulk File Bytes & Directory Lists) | (DTP) |
+-------------------+ +-------------------+
2. The Two Connections: Control vs. Data
+--------------------------+-----------------------+-----------------------+
| FEATURE | CONTROL CONNECTION | DATA CONNECTION |
+--------------------------+-----------------------+-----------------------+
| Default Server Port | **TCP Port 21** | **TCP Port 20** (Active)|
| Lifetime | Entire session length | Ephemeral (Per file) |
| Data Format | 7-bit ASCII commands | Raw bytes / binary |
| Purpose | Authentication & Cmds | File content & listings|
+--------------------------+-----------------------+-----------------------+
Why Use Two Separate Connections?
- Out-of-Band Signaling: Control commands travel on a clean, dedicated channel uninterrupted by massive multi-gigabyte file payloads.
- Interactive Abort: If a user initiates a 10 GB file download and wants to cancel it mid-transfer, they can immediately send an
ABORcommand across the Control Connection. In single-connection protocols (like HTTP/1.0), the client must download or terminate the entire socket!
3. Active Mode vs. Passive Mode (The Firewall Problem)
FTP operates in two distinct operational modes, differing in which entity initiates the Data Connection:
A. ACTIVE FTP (PORT Mode) - Broken by Client Firewalls:
Client (Port 1050) ─── Connects to ───────────────────> Server (Port 21) [Control]
Client (Port 1050) ─── "I'm listening on Port 1051" ──> Server (Port 21)
Client (Port 1051) <── INBOUND CONNECTION INITIATED <── Server (Port 20) [Data]
(Client firewall blocks inbound connection from server ===> TRANSFER FAILS!)
B. PASSIVE FTP (PASV Mode) - Modern Firewall-Friendly Standard:
Client (Port 1050) ─── Connects to ───────────────────> Server (Port 21) [Control]
Client (Port 1050) ─── Sends "PASV" ──────────────────> Server (Port 21)
Client (Port 1050) <── "Connect to my Port 32000" ───── Server (Port 21)
Client (Port 1051) ─── OUTBOUND CONNECTION INITIATED ─> Server (Port 32000) [Data]
(Both connections are outbound from client ===> Firewall allows traffic!)
1. Active Mode (Default):
- The client tells the server its IP address and an arbitrary local port () using the
PORTcommand. - The server initiates the data connection from its own TCP Port 20 back to the client's port .
- Problem: Modern client NAT routers and personal firewalls block unsolicited inbound connection attempts from the outside Internet!
2. Passive Mode (PASV):
- The client sends a
PASVcommand across the control connection. - The server opens an unprivileged ephemeral port (e.g., ) and returns this port number to the client.
- The client initiates the data connection outbound to the server's ephemeral port.
- Advantage: Because both the Control and Data connections originate outbound from the client, client firewalls allow the connections without interference.
4. Common FTP Commands and Status Codes
Standard Commands:
USER <username>: Submits user identity.PASS <password>: Submits authentication password.LIST: Requests a list of files in the current remote directory.RETR <filename>: Retrieve (download) a file from server to client.STOR <filename>: Store (upload) a file from client to server.QUIT: Terminates the session.
3-Digit Status Response Codes:
1xx: Positive Preliminary reply (e.g.,150 Opening BINARY mode data connection).2xx: Positive Completion reply (e.g.,220 Service ready,230 User logged in,226 Transfer complete).3xx: Positive Intermediate reply (e.g.,331 Password required).4xx: Transient Negative reply (e.g.,425 Can't open data connection).5xx: Permanent Negative error (e.g.,530 Login incorrect,550 File not found).
5. Security Limitations and Modern Replacements
Standard FTP transmits all credentials (usernames and passwords) and file contents in unencrypted cleartext.
- FTPS (FTP Secure): Adds TLS encryption to standard FTP (RFC 4217).
- SFTP (SSH File Transfer Protocol): A completely different, modern protocol running over SSH (TCP Port 22) that provides encrypted file transfers over a single multiplexed connection.
6. Key Exam Questions & Takeaways
- Why does FTP use two separate TCP connections?
- It separates control commands (
PORT 21) from data transfer (PORT 20 / PASV), allowing control commands (such as directory changes or transfer aborts) to be processed immediately without being blocked by large transferring files.
- It separates control commands (
- What is the difference between Active FTP and Passive FTP?
- In Active FTP, the server initiates the data connection back to the client from Port 20 (often blocked by client firewalls). In Passive FTP, the client initiates both the control and data connections to the server, bypassing client firewall restrictions.
- What port does the FTP control connection use?
- TCP Port 21.