Sub topic 11
File transfer: FTP, SFTP and TFTP
This chapter assumes you know what a port and a socket are, how TCP differs from UDP, and what an ephemeral client port is. It also assumes you can read short Python scripts. Three protocols move files across a network, and their names look alike. They differ in transport, security and port usage, and those differences decide how you configure firewalls.
The three protocols at a glance
| Protocol | Default port | Transport | Encrypted | Typical use |
|---|---|---|---|---|
| FTP | 21 (control), 20 or a negotiated port (data) | TCP | No | Legacy file servers, old website uploads |
| FTPS (explicit) | 21 | TCP | Yes, via TLS upgrade | FTP with TLS added |
| FTPS (implicit) | 990 (control), 989 (data) | TCP | Yes, TLS from the first byte | Older FTPS deployments |
| SFTP | 22 | TCP | Yes, via SSH | Secure file transfer and automation |
| TFTP | 69 | UDP | No | Network boot, firmware, device configs |
Despite the similar names, SFTP is not FTP with security added. It is a separate protocol that runs inside an SSH connection.
FTP: two connections for one session
FTP (File Transfer Protocol) is one of the oldest application protocols. A session uses two TCP connections:
- The control connection goes to server port 21. It carries commands such as
USER,PASS,LISTandRETR, and replies with numeric codes such as230(logged in). - A data connection is opened for each directory listing or file transfer. It carries only the payload.
The two connections exist because the protocol was designed to keep commands and data separate. That choice causes most of FTP's trouble with firewalls and NAT (network address translation).
Active mode
In active mode the client opens a listening port and sends it to the server with a PORT command. The server then connects back to the client, traditionally from server port 20. This breaks easily. A client behind NAT or a firewall usually blocks inbound connections, and the client may announce a private address the server cannot reach.
Passive mode
In passive mode the client sends PASV (or EPSV for IPv6 support). The server opens a port of its choosing and replies with it, and the client makes both connections outbound. Nearly every modern client defaults to passive mode.
The server picks the data port, so a firewall in front of it cannot simply allow port 21. The usual fix is to configure the FTP server to choose passive ports from a fixed range, such as 50000 to 50100, and to open that range as well. Some firewalls instead inspect the control connection and open the data port automatically. This is called an FTP application-layer helper. It stops working when the control channel is encrypted, because the firewall can no longer read the 227 reply.
Reading the PASV reply
The server encodes its address and port as six decimal numbers. The last two give the port as high * 256 + low. This script runs offline and decodes a sample reply:
import re
reply = "227 Entering Passive Mode (192,0,2,10,195,80)."
match = re.search(r"\((\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\)", reply)
if match is None:
raise ValueError("not a PASV reply")
h1, h2, h3, h4, p_high, p_low = (int(g) for g in match.groups())
host = f"{h1}.{h2}.{h3}.{h4}"
port = p_high * 256 + p_low
print(host, port)It prints 192.0.2.10 50000, because 195 × 256 + 80 = 50000.
The security problem
Plain FTP sends the username, password, commands and file contents unencrypted. Anyone on the network path can read them. Two fixes exist, and they are often confused:
- FTPS keeps the FTP design and wraps it in TLS. In explicit mode the client connects to port 21 and sends
AUTH TLSto upgrade. In implicit mode the client connects to port 990 and TLS starts immediately. Data connections are also protected, so the firewall problems above remain. - SFTP replaces FTP entirely.
SFTP: file transfer inside SSH
SFTP (SSH File Transfer Protocol) runs as a subsystem of an SSH session. It uses the same port as SSH, TCP 22, and the same authentication: passwords or public keys. Everything is encrypted, including the commands.
SFTP multiplexes all its traffic over one connection. There is no separate data connection, so firewall configuration is simple: allow TCP 22 and nothing else. For this reason SFTP is the usual recommendation for new deployments and for automated transfers.
Two related points often cause confusion:
- SCP (secure copy) also runs over SSH on port 22, but it is a different, older protocol. Recent OpenSSH versions run the
scpcommand over the SFTP protocol internally. - Opening port 22 for SFTP also opens SSH login unless the server is configured otherwise. Servers can restrict a user to file transfer only, for example with OpenSSH's
ForceCommand internal-sftpandChrootDirectorysettings.
TFTP: tiny and unauthenticated
TFTP (Trivial File Transfer Protocol) is deliberately minimal. It uses UDP port 69, has no login, no directory listing and no encryption, and it transfers files in small blocks. Each block must be acknowledged before the next is sent. The default block size is 512 bytes, though an extension (RFC 2348) lets clients and servers negotiate larger blocks.
TFTP survives because it is simple enough to fit in a device's boot firmware. Typical uses are:
- PXE network boot, where a machine fetches a boot loader.
- Loading firmware or configuration onto switches, routers and IP phones.
The server sends its replies from a new ephemeral port, not from 69. The client must therefore accept UDP packets from an unexpected source port. A stateless firewall that allows only port 69 breaks TFTP. Stateful firewalls normally track the exchange, but some need a TFTP helper.
The opening packet of a TFTP read is small. A read request is the opcode 1, the filename, a zero byte, the transfer mode and another zero byte:
import struct
def read_request(filename: str, mode: str = "octet") -> bytes:
return (
struct.pack("!H", 1)
+ filename.encode("ascii") + b"\x00"
+ mode.encode("ascii") + b"\x00"
)
packet = read_request("pxelinux.0")
print(packet)
print(len(packet), "bytes")This prints b'\x00\x01pxelinux.0\x00octet\x00' and then 20 bytes. Sending it in a UDP datagram to port 69 of a TFTP server starts a download.
Inspecting file transfer services
Use the commands from the earlier chapter on listing and testing ports to check what is running:
# Which file transfer services are listening locally?
ss -tulnp | grep -E ':(21|22|69|990)\b'
# Is the control port reachable from another machine? (use a host you own)
nc -vz 192.0.2.10 21An open port 21 tells you something is listening there, not that the FTP data channel will work. A passive transfer can still fail if the data port range is blocked, and the symptom is a successful login followed by a hang at the directory listing.
Choosing a protocol
- Use SFTP when you control both ends and want one port, strong authentication and encryption.
- Use FTPS when a partner requires FTP-style tooling and TLS. Fix the passive port range and plan for firewall work.
- Use plain FTP only on trusted, isolated networks, or for public downloads that carry no credentials. Even then, prefer HTTPS.
- Use TFTP only for device boot and firmware on a controlled network segment.
Exercise
A company puts an FTP server behind a firewall that allows only inbound TCP 21. Users can log in, but every directory listing hangs. In passive mode, what is the cause, and what two changes would fix it? Would moving to SFTP avoid the problem?
Show answer
Login uses the control connection on port 21, which the firewall allows. The directory listing needs a data connection to the port in the server's PASV reply, which the firewall blocks. Fix it by configuring the FTP server to use a fixed passive port range (for example 50000-50100) and allowing that range through the firewall. Alternatively, enable a firewall FTP helper, which fails if the control channel is encrypted. SFTP avoids the problem because it carries commands and data over a single connection on TCP 22.
Exercise
Decode the PASV reply "227 Entering Passive Mode (203,0,113,5,78,32)". What address and port should the client connect to?
Show answer
The address is 203.0.113.5. The port is 78 * 256 + 32 = 19968 + 32 = 20000, so the client connects to 203.0.113.5 on TCP port 20000.
Exercise
Why is "SFTP" not the same as "FTP over SSL", and which ports do SFTP, explicit FTPS and implicit FTPS use by default?
Show answer
SFTP is a separate protocol that runs as an SSH subsystem on TCP 22. FTPS is the original FTP protocol wrapped in TLS. Explicit FTPS starts on TCP 21 and upgrades with AUTH TLS. Implicit FTPS uses TCP 990 (data on 989) and starts TLS immediately.