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.

Two rows. Active mode: the client connects to server port 21 for control and sends PORT; the server then connects from its port 20 to the client's port 51001. Passive mode: the client connects to port 21, sends PASV, the server answers with port 50012, and the client connects to it. Arrows show who opens each connection.
The control connection always goes from the client to port 21; the mode decides who opens the data connection.

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 digitMeaningExamples on this page
1started, another reply will follow150 here comes the data
2done220 ready, 230 logged in, 200 PORT ok, 227 / 229 passive port, 226 transfer complete, 221 goodbye
3so far so good, send more331 send the password
4failed, may work if tried again425 could not open the data connection
5refused, do not repeat as is500 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.

A PORT packet before and after the NAT router. Before: header source 192.168.1.10:51000, text PORT 192,168,1,10,199,57. After: header source rewritten to 203.0.113.7:40001, text still PORT 192,168,1,10,199,57, marked as a mismatch.
NAT rewrites the header but not the address written inside the PORT command, so the two no longer agree.

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.

Passive mode after login. On the control connection the client sends PASV; the server listens on port 50012 and answers 227 (198,51,100,20,195,92), which is port 195 times 256 plus 92 = 50012. The client sends a SYN to port 50012; for its router this is a new outgoing connection, so it gets NAT row 2. The server answers SYN/ACK. The client sends LIST on the control connection; the server answers 150 Here comes the directory listing, sends the listing pub, readme.txt on the data connection, closes it with FIN, and confirms 226 Directory send OK on the control connection.
In passive mode the client opens both connections, so its NAT router sees only outgoing traffic; the listing travels on the data connection between 150 and 226.

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 datathe clientthe server, on a port from its passive range
Who opens the data connectionthe server, from port 20the client
Client behind a firewall or NAT✗ fails (425 or 500)✓ works
What the server's firewall must allowoutgoing from port 20incoming 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:

FTPFTPS (FTP over TLS)SFTP
What it isRFC 959FTP with TLS on both connections (RFC 4217)a different protocol, run inside SSH
Port21 + data ports21 (AUTH TLS) or 990, + data ports22, one connection
Password and data✗ plain text✓ encrypted✓ encrypted
Firewalls and NATneeds passive mode or a helperharder: 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.

On the mind map

Open the Networking mind mapNetworkingLayers & links: frames on one local networkLayers & linksIP & routing: packets hop between networksIP & routingCore services: address, name and timeCore servicesTCP, UDP & sockets: end to end between two portsTCP, UDP & socketsHTTP & TLS: what a browser does per requestHTTP & TLSAPIs & push: how apps talk to serversAPIs & pushProxies & edge: boxes in the middle of the pathProxies & edgeContainers: virtual networks from Linux partsContainersReal-time media: voice and video over UDPReal-time mediaSecurity at the edge: filter traffic at the borderSecurity at the edgeRouter forwardingRouter forwardinglongest prefix match picks the next hopMPLS label switchingMPLS label switchingpush at edge, swap in core, pop before lastVRRP / HSRP failoverVRRP / HSRP failoverBackup takes over the gateway in 3.6 sSDN and OpenFlowSDN and OpenFlowcontroller writes rules, switch looks upTracerouteTracerouteTTL 1, 2, 3: each hop sends Time ExceededNATNATmaps private IP:port to one public IPFTP active vs passivePORT breaks behind NAT; PASV connects outIPv4 vs IPv6IPv4 vs IPv632 vs 128 bits; SLAAC, no NAT neededIPv6 Neighbor DiscoveryIPv6 Neighbor DiscoveryNS/NA to solicited-node group, not broadcastAnycast and BGPAnycast and BGPsame prefix announced from many sitesOSPF and BGP policyOSPF and BGP policyPolicy first, then path, then IGP costDistance vector vs link stateDistance vector vs link stateRIP shares distances; OSPF floods the mapSubnets and CIDRSubnets and CIDR/24 = mask: same subnet? ARP, else routerVLSM PlanningVLSM PlanningLargest first: no gaps, one summary routeRouter queues and AQMRouter queues and AQMFull buffers add delay; drop earlyDelay, loss, throughputDelay, loss, throughputSlowest link sets pace; full buffer dropsOpen the Networking mind mapNetworkingpackets hop between networksIP & routingRouter forwardingRouter forwardinglongest prefix match picks the next hopMPLS label switchingMPLS label switchingpush at edge, swap in core, pop before lastVRRP / HSRP failoverVRRP / HSRP failoverBackup takes over the gateway in 3.6 sSDN and OpenFlowSDN and OpenFlowcontroller writes rules, switch looks upTracerouteTracerouteTTL 1, 2, 3: each hop sends Time ExceededNATNATmaps private IP:port to one public IPFTP active vs passivePORT breaks behind NAT; PASV connects outIPv4 vs IPv6IPv4 vs IPv632 vs 128 bits; SLAAC, no NAT neededIPv6 Neighbor DiscoveryIPv6 Neighbor DiscoveryNS/NA to solicited-node group, not broadcastAnycast and BGPAnycast and BGPsame prefix announced from many sitesOSPF and BGP policyOSPF and BGP policyPolicy first, then path, then IGP costDistance vector vs link stateDistance vector vs link stateRIP shares distances; OSPF floods the mapSubnets and CIDRSubnets and CIDR/24 = mask: same subnet? ARP, else routerVLSM PlanningVLSM PlanningLargest first: no gaps, one summary routeRouter queues and AQMRouter queues and AQMFull buffers add delay; drop earlyDelay, loss, throughputDelay, loss, throughputSlowest link sets pace; full buffer drops
This page on the Networking mind map · 83 pages in 10 branches · open it to see the whole map and test yourself in recall mode.