FTP · File Transfer Protocol

The classic file transfer protocol with two connections.

13 · Application · TCP 21 (+ data port)

FTP

File Transfer Protocol

The classic file transfer protocol with two connections.

LearnProtocolTier 2 · MechanismsUnderstand10 minInfrastructure

You will be able to: trace a FTP exchange and decode it byte by byte.

Prove it: Ray Serve Distributed Actor Deployment

System design›Protocols

Study with AI
ClaudeChatGPTGeminiPerplexityMore

Part 1 of 5

Basics

What the protocol is for, and the ideas it is built on.

1.1What it does

The protocol in plain words, before any bytes.

FTP separates control from data. Commands and replies travel on one connection (port 21). Every file or directory listing opens a new data connection. This was elegant in 1971 and awkward for firewalls and NAT today.

In active mode the server connects back to the client. In passive mode the server opens a port and the client connects to it, which works better through NAT.

1.2Key ideas

The handful of ideas everything else follows from.

Control and data

Two sockets per session. It is why FTP needs firewall helpers.

Active and passive

Passive is almost always used now.

Reply codes

1xx preliminary, 2xx success, 3xx need more, 4xx temporary failure, 5xx failure.

Successors

SFTP (over SSH) and FTPS (FTP over TLS) add security. SFTP is a different protocol.

Part 2 of 5

Question

What this lesson prepares you to answer. Keep these in mind while you watch the packets.

2.1Target questions

  1. Why is FTP awkward with NAT?
  2. Is SFTP the same as FTP over TLS?

The answers are in part 4.

Part 3 of 5

Detailed answer

Watch a real exchange step by step, then take each message apart field by field.

3.1Follow the packets

Step through the exchange between the two sides.

Client
Server
USER / PASS
PASV
227 (…,195,80)
RETR file.txt
Data connection: file bytes
226 Transfer complete
Step 1 of 6

Over the control connection. Credentials are plain text.

← BackPlayNext →

3.2Decode it byte by byte

Click any byte or row. Each colour is one field.

Control channel

FTP uses two connections. Commands travel on port 21; files travel on a separate data connection.

000032323020465450207365727665722072220 FTP server r
0010656164790d0a5553455220616e6f6e79eady··USER anony
00206d6f75730d0a50415353206775657374mous··PASS guest
0030406578616d706c652e636f6d0d0a5041@example.com··PA
004053560d0a32323720456e746572696e67SV··227 Entering
00502050617373697665204d6f6465202839 Passive Mode (9
0060332c3138342c3231362c33342c3139353,184,216,34,195
00702c3830290d0a524554522066696c652e,80)··RETR file.
00807478740d0a323236205472616e736665txt··226 Transfe
00907220636f6d706c6574650d0ar complete··
FieldGreeting
Length22 bytes
Text220 FTP server ready␍␊

On connect.

FieldOffsetBytesHex
Greeting02232 32 30 20 46 54 50 20 …
USER221655 53 45 52 20 61 6e 6f …
PASS382450 41 53 53 20 67 75 65 …
PASV62650 41 53 56 0d 0a
PASV reply685032 32 37 20 45 6e 74 65 …
RETR1181552 45 54 52 20 66 69 6c …
Done1332332 32 36 20 54 72 61 6e …

3.3Decode it in code

A small decoder, tested against the exact bytes shown above.

Decoding FTP's passive reply

FTP uses a second connection for data, and the address travels as text.

ftp.tsTypeScriptCopy
1/** Reply 227 packs the data connection's address into six numbers. */2export function parsePasv(reply: string): { host: string; port: number } {3  const m = reply.match(/\((\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\)/);4  if (!m || !reply.startsWith("227")) throw new Error("not a PASV reply");5  const [h1, h2, h3, h4, p1, p2] = m.slice(1).map(Number);6  return { host: `${h1}.${h2}.${h3}.${h4}`, port: p1 * 256 + p2 }; // the port needs two bytes7}8 9/** The first digit says what happened. 1xx means the transfer has only started. */10export function replyClass(code: number): "wait" | "done" | "more" | "retry" | "failed" {11  return (["wait", "done", "more", "retry", "failed"] as const)[Math.floor(code / 100) - 1];12}
12 linesUTF-8TypeScript
p1 * 256 + p2
The port needs two bytes, so it's written as two numbers.
replyClass
The first digit: 1xx means the transfer has only started.

Tested by npm run test:modules.

Part 4 of 5

Q&A

Check your understanding. Answer out loud, then open.

4.1Check yourself

Why is FTP awkward with NAT?
The server sends IP and port numbers inside the control channel, which NAT does not rewrite, and the data connection uses new ports.
Is SFTP the same as FTP over TLS?
No. SFTP runs over SSH. FTPS is FTP wrapped in TLS.
Part 5 of 5

Extra depth

The mistakes people make with this protocol in real systems.

5.1Watch out for

  • Credentials and files are readable on the wire.
  • Firewalls must allow the passive port range.
  • ASCII versus binary mode once corrupted files by changing line endings.
← IMAPMark as understoodSSH →