TodayTools

File Transfer Protocol

How FTP works

The old way to move files, two connections

Donate
L7ApplicationPort 21TCP
What brings you here?

Watch it happen

How much detail

Lucas downloads a report: commands go over port 21, the file over a second connection the server opens a port for.

Lucas's laptop
Home router (NAT)
Internet
ftp.example.com
Wi-FiInternetSetupResponseRequestDataClose

5 steps, 19 packets

First up: Control connection. Press Play to watch it happen, one step at a time.

0 of 50/5

What is FTP?

FTP moves files between a computer and a server. It uses two connections: one on port 21 for commands like log in, list and download, and a separate one for each file or folder listing. That split made sense in 1971 and still confuses firewalls today.

Plain FTP sends everything, passwords included, as readable text. It is still found on old websites, printers and cameras, but for anything private use SFTP, which runs inside SSH, or FTPS, which adds TLS.

Think of it like this. FTP is like ordering by phone and having the goods delivered by a separate truck. You place the order on the phone line (port 21), and the store tells you which loading bay to drive to (passive mode). In active mode the truck tries to come to your house instead, and your gate guard turns it away.

Step by step

Passive mode

5 steps

Lucas downloads a report: commands go over port 21, the file over a second connection the server opens a port for.

  1. 1

    Control connection

    TCP to port 21

    FTP uses two connections. The first, to port 21, carries only commands and replies and stays open for the whole session.

  2. 2

    Log in

    USER, PASS, 230

    The app sends the username and password one after the other. In plain FTP both are readable by anyone on the path.

  3. 3

    Ask for passive mode

    PASV, 227 with an address and port

    PASV asks the server to open a fresh port for the file. The 227 reply packs the address and port as six numbers: the last two give the port, 195 times 256 plus 149, which is 50069. Newer apps use EPSV, which gives just the port.

  4. 4

    Data connection

    Laptop connects to port 50069

    The laptop opens a second TCP connection to that port. Because the laptop starts it, the home router's NAT lets it through without trouble.

  5. 5

    Transfer and close

    150, file data, 226

    RETR asks for the file. The server says 150, sends the bytes on the data connection, closes it to mark the end, and says 226 on the control connection.

Active mode fails

4 steps

In active mode the server must call the laptop back, and the home router's NAT stops it.

  1. 1

    Logged in

    Control connection on 21

    Lucas uses an old FTP app that defaults to active mode.

  2. 2

    PORT command

    Call me back at 192.168.1.10

    In active mode the client tells the server where to connect for the data. The laptop only knows its private address, 192.168.1.10, which means nothing on the internet.

  3. 3

    Callback blocked

    SYN from port 20 goes nowhere

    The server tries to open a connection from its port 20 to the laptop. Even with the right public address, the home router's NAT and firewall drop a connection that no one inside asked for.

  4. 4

    425 and fall back

    Switch to passive

    The server gives up with 425. The fix is passive mode, where the client makes both connections. That is why every modern FTP app uses PASV or EPSV by default.

Password in clear

3 steps

On café Wi-Fi a stranger captures the FTP login, because plain FTP encrypts nothing.

  1. 1

    Login over plain FTP

    USER and PASS as text

    Lucas uploads a file from a café. His app uses plain FTP on port 21.

  2. 2

    Captured

    Anyone in range can read it

    On a shared network a capture tool shows every FTP command. The stranger now has the username and password for the server.

  3. 3

    The fix

    Use SFTP or FTPS

    SFTP runs inside SSH, and FTPS adds TLS to FTP with AUTH TLS. Either one hides the password and the files.

Myths and mix-ups

  • SFTP is not FTP with security added; it is a separate protocol that runs inside SSH.
  • Plain FTP does not hide your password; it travels as readable text.
  • A blocked FTP transfer is rarely a server fault; active mode callbacks are usually stopped by NAT or a firewall.
  • FTP does not use only port 21; every file and listing travels on a second connection.

Try it yourself

Run these on your own computer to see FTP at work.

  • Watch an FTP session

    curl -v --list-only ftp://ftp.example.com/

    Replace the host with a real FTP server. Lines starting with > are commands, < are replies. You will see EPSV or PASV.

  • Download a file

    curl -O ftp://ftp.example.com/pub/readme.txt

    curl uses passive mode by default. Add --ftp-port - to try active mode.

  • Prefer the safe version

    sftp lucas@files.example.com

    If the server also runs SSH, SFTP gives the same file access with everything encrypted.

FTP at a glance

Layer
Application (7)
Ports
21 for commands; data on a port chosen per transfer (20 in active mode)
Transport
TCP, two connections
Modes
Passive (PASV, EPSV) and active (PORT, EPRT)
Encryption
None; FTPS adds TLS, SFTP is a different protocol
Reply codes
3 digits, like 220, 331, 227, 226, 425
Standard
RFC 959 (1985), first version 1971

Defined inRFC 959RFC 2428RFC 4217

Header fields

What FTP puts in its messages, with example values from the stories above.

FTP

Reply code
220FTP reply codes use the same idea as SMTP: 2 done, 3 need more, 4 try later, 5 failed.
Text
Welcome to ftp.example.com
Command
PASSPlain FTP has no encryption. Anyone on the path can read this.
Argument
lucas

FTP-DATA

Content
%PDF-1.7 ... (raw file bytes)No headers here, just the file. The end is marked by closing the connection.

Test yourself

Question 1 of 5Score 0

Which port carries FTP commands?

Questions people ask

What is the difference between FTP and SFTP?

FTP is the old file transfer protocol on port 21 with no encryption. SFTP is a different protocol that runs inside SSH on port 22 and encrypts everything, using one connection.

What is the difference between active and passive FTP?

In active mode the server connects back to the client for each transfer. In passive mode the client makes that connection to a port the server names. Passive works behind NAT and firewalls, so it is the default today.

Is FTP secure?

No. Plain FTP sends usernames, passwords and files as readable text. Use SFTP or FTPS for anything private.

Which ports does FTP use?

Port 21 for commands. In active mode data comes from port 20. In passive mode the server picks a high port for each transfer, so firewalls need a range opened.

What does FTP error 425 mean?

The data connection could not be opened. It usually means active mode is blocked by a firewall or NAT. Switching the client to passive mode usually fixes it.

Donate