File transfer: FTP, SFTP, SCP, TFTP and rsync
Different file jobs need different protocols: uploading website files, a nightly backup and 100 IP phones downloading their config at boot. This note covers FTP (active vs passive), FTPS, SFTP, SCP, TFTP and rsync, and which one fits which job.
135.1 Why is it used?
- Developer uploads website files to a server: SFTP/SCP (encrypted) or legacy FTP/FTPS.
- A big folder is backed up every night: rsync sends only what changed.
- IP phones and PXE clients download config/boot files at boot: TFTP (UDP 69, no login).
- Knowing active vs passive FTP explains a classic firewall/NAT troubleshooting question.
135.2 How FTP works: active vs passive
- FTP uses two TCP connections: a control connection to port 21 (USER, PASS, commands) and a separate data connection.
- Active mode: client sends PORT with its IP and port; the server connects back from port 20. A client firewall/NAT blocks it -> 425.
- Passive mode: client sends PASV (or EPSV); the server replies with a port and the client opens the data connection too: just an outgoing connection for the firewall.
- Port numbers in PORT/PASV: last two numbers p1,p2 -> port = p1 x 256 + p2 (117,56 -> 30008; 140,17 -> 35857).
- Step 1
1.Client 192.168.155.20 -> server 192.168.155.10:21 control: USER student, PASS lab123
- Step 2
2.Active: PORT 192,168,155,20,140,17 (client listens on 35857)
- Step 3
3.Server opens data from :20 to client:35857 -> client firewall drops the SYN -> 425
- Step 4
4.Passive: PASV -> 227 Entering Passive Mode (192,168,155,10,117,56) = port 30008
- Step 5
5.Client opens data to server:30008 (outgoing) -> transfer completes
Browsers and FTP clients use passive by default
curl -v --user student:lab123 \ ftp://192.168.155.10/report.txtcurl -v --disable-epsv \ --user student:lab123 \ ftp://192.168.155.10/report.txt< 220 n135 lab FTP server> USER student< 331 Please specify the password.> PASS lab123< 230 Login successful.> EPSV< 229 Entering Extended Passive Mode (|||30000|)* Connected 2nd connection to 192.168.155.10 port 30000> RETR report.txt< 226 Transfer complete. > PASV< 227 Entering Passive Mode (192,168,155,10,117,56).> RETR report.txt< 150 Opening BINARY mode data connection for report.txt (29 bytes).Q3 sales report: 1250 orders< 226 Transfer complete.135.3 Lab: a firewall breaks active mode
nft add table inet n135fwnft add chain inet n135fw input \ '{ type filter hook input priority 0; policy accept; }'nft add rule inet n135fw input \ ip saddr 192.168.155.10 \ tcp flags '&' '(syn|ack)' == syn dropcurl -v --ftp-port - --disable-eprt ...curl -s --disable-epsv ...> PORT 192,168,155,20,140,17< 200 PORT command successful. Consider using PASV.> RETR report.txtcurl: (19) RETR response: 425 $ curl -s --max-time 20 --disable-epsv ...Q3 sales report: 1250 orderscurl exit 0- The rule drops new TCP connections (SYN without ACK) coming from the server: exactly what active mode needs.
- Passive still works because the client opened both connections.
135.4 FTP is plain text, SFTP is encrypted
tshark -r /tmp/ftp.pcap -Y ftp.request \ -T fields -e ip.src \ -e ftp.request.command -e ftp.request.argtshark -r /tmp/ftp.pcap \ -Y 'tcp.flags.syn==1 && tcp.flags.ack==0' \ -T fields -e ip.src -e tcp.srcport \ -e ip.dst -e tcp.dstport192.168.155.20 USER student192.168.155.20 PASS lab123192.168.155.20 PASV192.168.155.20 RETR report.txt...192.168.155.20 PORT 192,168,155,20,205,207 192.168.155.20 35934 192.168.155.10 21192.168.155.20 51514 192.168.155.10 30009192.168.155.20 35940 192.168.155.10 21192.168.155.10 20 192.168.155.20 52687- The password lab123 is readable in the capture (a throwaway lab password).
- The last SYN comes from server port 20 to the client: that is the active-mode data connection.
| Protocol | Transport / port | Encryption | Notes |
|---|---|---|---|
| FTP | TCP 21 + data ports | None, all plain text | Two connections, active/passive |
| FTPS | TCP 21 (AUTH TLS) or 990 | TLS | Same old FTP + TLS, two-connection problem stays |
| SFTP | TCP 22 | SSH | SSH subsystem, not related to FTP |
| SCP | TCP 22 | SSH | Copy in one command; new OpenSSH uses SFTP underneath |
| TFTP | UDP 69 | None, no login | 512-byte blocks, ACK per block; phones, PXE |
| rsync | Over SSH (TCP 22) | SSH | Delta transfer: only changed blocks |
135.5 SFTP and SCP
ssh-keygen -q -t ed25519 -N '' \ -f /root/.ssh/id_ed25519 -C lab-clientprintf 'put /root/notes.txt\nls -l\nget report.txt /tmp/report.txt\n' | sftp -o StrictHostKeyChecking=accept-new \ -b - student@192.168.155.10scp -v /root/upload.txt \ student@192.168.155.10:/home/student/upload2.txtsftp> put /root/notes.txtsftp> ls -l-rw-r--r-- ? student student 18 Nov 9 03:57 notes.txt-rw-r--r-- ? student student 29 Nov 9 03:56 report.txtsftp> get report.txt /tmp/report.txt Executing: program /usr/bin/ssh host 192.168.155.10, user student, command sftpdebug1: Sending subsystem: sftptshark -r /tmp/ssh.pcap -Y ssh -T fields \ -e ip.src -e _ws.col.Info | sort | uniq -ctshark -r /tmp/ssh.pcap -x | grep -c lab123tcpdump -r /tmp/ssh.pcap -A | grep -c 'Q3 sales'tcpdump -r /tmp/ftp.pcap -A | grep -E 'PASS|Q3 sales' 59 192.168.155.20 Client: 56 192.168.155.10 Server: 3 192.168.155.20 Client: Protocol (SSH-2.0-OpenSSH_9.7) 3 192.168.155.20 Client: Key Exchange Init 3 192.168.155.10 Server: Diffie-Hellman Key Exchange Reply, New Keys 3 192.168.155.20 Client: New Keys00... length 13: FTP: PASS lab123PASS lab123.])WH.}.Q3 sales report: 1250 orders135.6 TFTP: UDP 69, no login
- Step 1
1.Phone boots and gets the TFTP server address from DHCP
- Step 2
2.Read Request (RRQ) phone-1001.cfg to 192.168.155.10 UDP 69
- Step 3
3.Server answers from a new port (39045): Data block 1, max 512 bytes
- Step 4
4.Client ACKs every block; next block follows
- Step 5
5.A PXE client fetches pxelinux.0 the same way
tftp -g -r pxelinux.0 \ -l /tmp/pxelinux.0 192.168.155.10tftp -g -r phone-1001.cfg \ -l /tmp/phone-1001.cfg 192.168.155.10tftp -p -l /tmp/boot.log \ -r boot.log 192.168.155.10pxelinux.0 100% |********************************| 20480 0:00:00 ETAsip_server=192.168.155.10 line1=1001put done 192.168.155.20 55073 69 Read Request, File: pxelinux.0, Transfer type: octet192.168.155.10 39045 55073 Option Acknowledgement, tsize=20480192.168.155.20 55073 39045 Acknowledgement, Block: 0192.168.155.10 39045 55073 Data Packet, Block: 1192.168.155.20 55073 39045 Acknowledgement, Block: 1192.168.155.10 39045 55073 Data Packet, Block: 2135.7 rsync: only the changed blocks
- Run 1: full copy, 939,451 bytes sent.
- Run 2, nothing changed: 132 bytes sent (speedup 6,520.28).
- Run 3, 7 bytes changed: the server sends block checksums, the laptop sends only Literal data 968 bytes; 937,927 bytes are Matched (rebuilt from the server's copy).
rsync -av /data/site/ \ student@192.168.155.10:/home/student/site/printf 'CHANGED' | dd of=/data/site/app.log \ bs=1 seek=500000 conv=notruncrsync -av --stats /data/site/ \ student@192.168.155.10:/home/student/site/sent 939,451 bytes received 136 bytes 626,391.33 bytes/sectotal size is 938,920 speedup is 1.00 sent 132 bytes received 12 bytes 96.00 bytes/sectotal size is 938,920 speedup is 6,520.28 Number of regular files transferred: 1Literal data: 968 bytesMatched data: 937,927 bytesTotal bytes sent: 5,031total size is 938,920 speedup is 86.25rsync -av --delete --dry-run /data/site/ \ student@192.168.155.10:/home/student/site/rsync -av --delete /data/site/ \ student@192.168.155.10:/home/student/site/deleting old.txttotal size is 938,916 speedup is 6,057.52 (DRY RUN) deleting old.txttotal size is 938,916 speedup is 6,057.52- --delete removes files on the destination that no longer exist on the source: always run --dry-run first in production.
- A trailing slash on /data/site/ copies the folder's contents, not the folder itself.
135.8 When to use which
| Job | Best choice | Why |
|---|---|---|
| Upload files to a server | SFTP / SCP | Encrypted, single port 22 |
| Nightly backup / mirror | rsync over SSH | Sends only changed blocks, --delete for mirrors |
| IP phone config, PXE boot | TFTP | Tiny client, no login, fits in firmware |
| Legacy partner needs FTP | FTPS, passive mode | At least encrypted; passive works through NAT |
135.9 Common mistakes
- Thinking SFTP = FTP over SSL: that is FTPS. SFTP runs inside SSH and needs only port 22.
- Using plain FTP over untrusted networks: USER/PASS and file content are readable.
- FTP login works but listing hangs behind NAT: client is in active mode; switch to passive.
- Running rsync --delete without --dry-run.
- Exposing TFTP to the internet: it has no authentication at all.
135.10 Interview tips
- Active vs passive: control is always client -> :21; active = server :20 -> client (blocked by client firewall/NAT); passive = client -> server data port. Passive is the default.
- FTP vs FTPS vs SFTP: plain / FTP+TLS / SSH subsystem on 22.
- TFTP: UDP 69, no login, 512-byte blocks with ACKs, used for PXE and IP phones.
- rsync delta transfer: checksums of blocks, only literal changed data crosses the network.