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:
2. The Two Connections: Control vs. Data
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:
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.
Frequently Asked Questions
QWhy 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.
QWhat 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.
QWhat port does the FTP control connection use?
TCP Port 21.
QHow do FTPS and SFTP differ?
FTPS is standard FTP with TLS encryption added (still two connections). SFTP is a different protocol that runs over SSH on TCP port 22 and multiplexes everything over a single encrypted connection.