Sub topic 11
File transfer: FTP, SFTP and TFTP
This chapter assumes you know what a port is, the difference between TCP and UDP, and how SSH works at the level of the previous chapter. Three protocols move files, and their names look alike. They differ in transport, ports and security.
The three protocols
| Protocol | Port | Transport | Encrypted |
|---|---|---|---|
| FTP (control) | 21 | TCP | No |
| FTP (data, active mode) | 20 | TCP | No |
| SFTP | 22 | TCP | Yes, through SSH |
| TFTP | 69 | UDP | No |
FTP (File Transfer Protocol) is the oldest of the three. SFTP (SSH File Transfer Protocol) is not FTP over SSH. It is a separate protocol that runs inside an SSH session, so it uses the SSH port, 22. TFTP (Trivial File Transfer Protocol) is a minimal protocol with no login and no directory listing.
FTP uses two connections
FTP keeps commands and file data on separate TCP connections. The client opens the control connection to port 21 and sends commands such as USER, PASS, LIST and RETR. Each file or directory listing travels on a second, data connection.
FTP has two modes for setting up the data connection:
- Active mode: The client listens on a port and tells the server. The server connects from its port 20 to that client port. A client-side firewall or NAT device usually blocks this inbound connection.
- Passive mode: The server opens a high port and tells the client. The client connects to it. This works through most client firewalls and is the common default.
Passive mode has a firewall consequence. The server needs a range of high ports open for data connections. Most FTP servers let you fix that range so you can allow exactly those ports.
SFTP: one port, one connection
SFTP needs only the SSH port. Authentication, encryption and file operations all share one TCP connection. That makes firewall rules simple: allow TCP 22 and nothing else. It also supports SSH key authentication, which avoids sending passwords.
A typical session looks like this:
sftp [email protected]
sftp> get report.txt
sftp> put backup.tar.gzTFTP: small and unauthenticated
TFTP uses UDP port 69 for the first request. The server then replies from a different, randomly chosen UDP port, and the rest of the transfer uses that port. Stateful firewalls and NAT devices can therefore mishandle it unless they have TFTP support.
TFTP has no authentication and no encryption. It survives because it is easy to implement in tiny boot code. Network devices use it to load firmware and configuration, and PXE network boot uses it to fetch boot files. Keep it on an isolated management network and never expose it to the internet.
Exercise
A server must allow users to upload files with SFTP only. Which inbound port do you open, and why is no extra data-port range needed?
Show answer
Open TCP port 22. SFTP runs inside the SSH session, so commands and file data share the single SSH connection. Unlike FTP, it has no separate data connection.