The idea: one connection to talk, another to move bytes
FTP, the File Transfer Protocol, uses two TCP connections. The control connection goes to port 21 and stays open for the whole session. Over it the client sends text commands (USER, LIST, RETR) and the server answers with reply lines that start with a three-digit code. The bytes of a listing or a file never travel on it. Each transfer gets a new data connection, which closes when the transfer ends. The question is who opens that data connection. In active mode the server connects to the client; in passive mode the client connects to the server.
The control connection and reply codes
The control connection is a plain text conversation, one line per command or reply, each ending in CR LF. The server speaks first, with 220. The client logs in with USER and PASS (Demo: log in on the control connection). The canvas writes every line into the transcript, in the exact words vsftpd uses. The first digit of the code is what a client reads:
| First digit | Meaning | Examples on this page |
|---|---|---|
| 1 | started, another reply will follow | 150 here comes the data |
| 2 | done | 220 ready, 230 logged in, 200 PORT ok, 227 / 229 passive port, 226 transfer complete, 221 goodbye |
| 3 | so far so good, send more | 331 send the password |
| 4 | failed, may work if tried again | 425 could not open the data connection |
| 5 | refused, do not repeat as is | 500 illegal PORT command |
Active mode: PORT, and the server connects back
In active mode the client first opens a listening socket, say on port 51001. It tells the server where it is with PORT h1,h2,h3,h4,p1,p2: four numbers for the IP and two for the port, where port = p1 × 256 + p2. For 203.0.113.7 port 51001 that is PORT 203,0,113,7,199,57, because 199 × 256 + 57 = 51001. After LIST or RETR, the server opens the data connection itself, from its port 20 to that address (vsftpd does this with connect_from_port_20=YES, as in its sample config). When the client has a public address and nothing filters incoming connections, this works (Demo: active mode, public IP).
Why active mode breaks behind a firewall or NAT
Most clients sit behind a home router that is a stateful firewall. It lets out every connection the client opens and records it in a table (conntrack), so the replies can come back in. A packet that starts a new connection from outside matches no row and is dropped. The server's SYN from port 20 is exactly that: a new incoming connection. The server's TCP retries for a while, then vsftpd gives up after connect_timeout (60 s) and replies 425 Failed to establish connection. on the control connection (Demo: active mode behind a firewall: 425).
With NAT it fails even earlier. The client only knows its private address, 192.168.1.10, so it sends PORT 192,168,1,10,199,57. The router rewrites the IP header of the packet (source 192.168.1.10:51000 becomes 203.0.113.7:40001) but not the text inside it. The server sees a PORT address that is not the address the command came from. vsftpd refuses that by default (port_promiscuous=NO, which also stops a client from aiming the server at a third machine) and replies 500 Illegal PORT command. (Demo: active mode behind NAT: 500 Illegal PORT). A private address could not be reached from the Internet anyway.
Some routers carry an FTP helper (an application-level gateway, such as Linux's nf_conntrack_ftp). It reads the control connection, rewrites the address inside PORT and opens a hole for the expected data connection. It only works while the control connection is plain text, and since Linux 4.7 it is not applied unless a firewall rule asks for it.
Passive mode: PASV and EPSV
In passive mode the roles of the data connection are reversed. The client sends PASV. The server opens a listening port from its passive range (vsftpd's pasv_min_port to pasv_max_port) and answers 227 Entering Passive Mode (198,51,100,20,195,92)., which is 198.51.100.20 port 195 × 256 + 92 = 50012. The client then connects to it. For the client's router this is an ordinary outgoing connection: it gets a NAT row and the replies come back in (Demo: passive mode (PASV) behind NAT: works). vsftpd checks that the data connection comes from the same IP as the control connection (pasv_promiscuous=NO), so nobody else can grab the file.
EPSV (RFC 2428) is the same idea with no address in the reply: 229 Entering Extended Passive Mode (|||50012|). The client uses the IP it already talks to, so EPSV works with IPv6 and does not care about NAT on either side (Demo: download a file with EPSV and RETR).
| Active (PORT) | Passive (PASV / EPSV) | |
|---|---|---|
| Who listens for data | the client | the server, on a port from its passive range |
| Who opens the data connection | the server, from port 20 | the client |
| Client behind a firewall or NAT | ✗ fails (425 or 500) | ✓ works |
| What the server's firewall must allow | outgoing from port 20 | incoming to the passive range |
Passive mode moves the problem to the server side: the server's firewall must let in the whole passive port range. That is easier, because the server's admin controls it, so clients use passive mode by default.
Plain text, and why FTPS and SFTP exist
Everything on the control connection is plain text, the password included, and so is the data. The router on the canvas reads PASS guest@ as it passes, and so can anyone else on the path. Two protocols fix this:
| FTP | FTPS (FTP over TLS) | SFTP | |
|---|---|---|---|
| What it is | RFC 959 | FTP with TLS on both connections (RFC 4217) | a different protocol, run inside SSH |
| Port | 21 + data ports | 21 (AUTH TLS) or 990, + data ports | 22, one connection |
| Password and data | ✗ plain text | ✓ encrypted | ✓ encrypted |
| Firewalls and NAT | needs passive mode or a helper | harder: a helper cannot read the encrypted PORT/PASV | ✓ easy: a single outgoing connection |
FTPS keeps FTP's two connections and adds TLS to them. SFTP has nothing to do with FTP apart from the name: it sends file commands as messages inside one SSH connection, so there is no data connection to open and nothing to break.
See also NAT for how the router's translation table works, Stateful Firewall for conntrack rows and why new incoming connections are dropped, and TCP Handshake for the SYN, SYN/ACK and ACK behind every connection here.
What the page leaves out
The final ACK of each handshake as its own arrow, the server's SYN retries one by one, TYPE I / TYPE A, EPRT (the extended PORT), REST, STOR and other commands, the server's own NAT (where pasv_address must be set), FTP helpers in detail, conntrack timeouts (a closed row lingers about 2 minutes), and the TLS steps of FTPS.