skip to content

In FTP, what is the difference between active and passive mode, and which side opens the data connection in each?

level: middleimportance: must knowfreq 42%

answer

  1. who calls whom for data
  2. PORT: client listens
  3. server's source port 20
  4. PASV, 227, then the client dials
  5. inbound calls to a laptop

basics

~20 s

In active FTP the client listens, announces its address with PORT, and the server connects from port 20. In passive FTP the client sends PASV, the server replies 227 with an address and port it listens on, and the client connects.

solid answer

~40 s

Both modes share the control connection to port 21; they differ in who opens the **data connection**. In **active** mode the client opens a listening port, names it with `PORT h1,h2,h3,h4,p1,p2`, and after `RETR` or `LIST` the server connects from its port 20 to that address. In **passive** mode the client sends `PASV`, the server opens a listening port and answers `227 Entering Passive Mode (h1,h2,h3,h4,p1,p2)`, and the client makes the connection. That is why passive works from a laptop on a home network: every connection is outbound from the laptop, which home routers and host firewalls allow, while active mode needs the server to reach *into* the laptop — usually dropped, and hopeless when the `PORT` address is a private one. `EPRT` and `EPSV` are the extended forms of the same two modes.

go deeper

for a junior

Recall who dials whom: in active mode the server connects to the client from port 20; in passive mode the client connects to a port the server named in its 227 reply.

for a middle

Walk through both exchanges command by command — PORT, 200, LIST, connect from 20 versus PASV, 227, connect, LIST — and say why the 200 to PORT proves nothing about reachability.

for a senior

Turn the direction into firewall facts: active mode needs inbound access to every client, passive needs inbound access to a server port range, which is why passive is the sensible default.

for a principal

Judge the cost of a protocol that opens connections in both directions: passive mode moves the inbound requirement from many clients to one server you operate, usually the right trade.

## Same control connection, two ways to open the data connection Every FTP session (RFC 959) starts the same way: the client opens a **control connection** to the server's TCP port **21** and logs in. Each file transfer or directory listing then needs its own **data connection**, and the two modes differ only in *who opens it*: - **Active mode** — the client listens; the **server connects**. - **Passive mode** — the server listens; the **client connects**. The names describe the server's role. RFC 959 defines `PASV` as a request that the server "listen" on a data port and wait for a connection "rather than initiate one", so in passive mode the server does a passive TCP open, and in active mode it does an active one. ## Active mode step by step 1. The client opens a listening socket on a port of its choosing, say 50000 on 192.0.2.15. 2. It announces that endpoint with `PORT 192,0,2,15,195,80` (the port is 195 × 256 + 80 = 50000). 3. The server records it and answers `200 Command okay`. Nothing has been connected yet. 4. The client sends the transfer command, for example `LIST`. 5. The server answers `150 File status okay; about to open data connection` and connects **from its own port 20** to 192.0.2.15 port 50000. 6. The listing flows; the server closes the data connection and sends `226 Closing data connection`. `EPRT` is the extended form of `PORT` (RFC 2428): same mode, but it names an address family and can carry an IPv6 address. ## Passive mode step by step 1. The client sends `PASV`. 2. The server opens a listening socket on a port it picks and answers `227 Entering Passive Mode (198,51,100,20,195,149)` — address 198.51.100.20, port 195 × 256 + 149 = 50069. 3. The client connects to 198.51.100.20 port 50069. 4. The client sends `LIST`; the server answers `150`, sends the listing over the connection the client opened, closes it and replies `226`. `EPSV` is the extended form of `PASV`: its reply, `229 Entering Extended Passive Mode (|||50069|)`, carries only the port, and the client reuses the control connection's address. ## The two modes side by side | | Active (`PORT` / `EPRT`) | Passive (`PASV` / `EPSV`) | |---|---|---| | Who listens | client | server | | Who connects | server, **from port 20** | client, from any local port | | Setup reply | `200 Command okay` | `227` or `229` with the server's port | | Direction of the data connection, seen from the client | **inbound** | **outbound** | | Who needs an inbound firewall opening | every client | the server, for its data ports | The direction of the *bytes* is independent of the mode: `RETR` downloads and `STOR` uploads in either one. ## The laptop on a home network Picture a laptop on a home network listing a directory on a public FTP server. Login works — the control connection is an ordinary outbound connection to port 21. Then: - **Active mode hangs after `PORT`.** The `PORT` gets `200`, because the server only noted the address. When `LIST` arrives the server tries to connect *into* the laptop. A home router or the laptop's own host firewall drops unsolicited inbound connections, and the address inside `PORT` is the laptop's private address, which the server could not route to anyway. The client waits; eventually the server gives up with `425 Can't open data connection` or the client times out. - **Passive mode works.** After `PASV` the laptop makes the data connection itself, outbound, exactly like the control connection — which home routers and host firewalls normally allow. That is the short answer to the interview question "why does only passive work behind NAT?": passive keeps every connection outbound from the client. What a NAT device rewrites, and how an FTP helper in a firewall tries to repair active mode, is the subject of the material on FTP's NAT and firewall issues; RFC 1579 ("Firewall-Friendly FTP") already recommended in 1994 that clients use `PASV` for exactly this reason. ## Misconceptions to avoid - "Active means the client is active." The client sends commands in both modes; *active* describes the server opening the data connection. - "Passive mode uses port 20." Port 20 is only the server's source port in active mode; in passive mode the server listens on a port it reports in `227` or `229`. - "Passive mode needs no inbound opening at all." It needs one on the **server** side, for the ports it advertises — one server you control instead of every client. - "A `200` to `PORT` proves the server can reach the client." It proves nothing; the connection is only attempted after the transfer command.

  • Why does an FTP server answer PORT with 200 even when it will never reach that address?
    `PORT` only records where the server should connect later; RFC 959 answers it with `200 Command okay` without opening anything. The connection is attempted only after a transfer command such as `LIST` or `RETR`, so the failure surfaces there — as a hang, then `425 Can't open data connection` or a client timeout.
  • In FTP passive mode, which side's firewall must allow an inbound connection?
    The server's. The client connects outward to the port named in the `227` or `229` reply, so the server side must accept inbound connections on the ports it hands out. That moves the burden from every client to one server you control; sizing and advertising those ports belongs with FTP's NAT and firewall material.
  • Does an FTP server use port 20 in passive mode as well?
    No. Port 20 is the source port the server uses when it opens the data connection in active mode — RFC 959's L-1, one below the control port. In passive mode the server listens on a port it chooses and reports in `227` or `229`; the client connects there, and port 20 plays no part.

Passive mode is the server handing the caller a direct-line extension to ring: the caller places every call, so it works even from a phone that cannot take incoming calls. Active mode is the caller leaving its own number and waiting for a call back, which never arrives if its switchboard rejects incoming calls.

  • In FTP active mode the client opens the data connection to the server
  • Passive mode means the client waits for the server to connect
  • Passive-mode data still goes to the server's port 20
  • A 200 reply to PORT proves the server can reach the client
  • Passive mode needs no inbound firewall opening on either side