SkillPilotAI

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).
Active FTP blocked, passive FTP works
  1. 1.Client 192.168.155.20 -> server 192.168.155.10:21 control: USER student, PASS lab123

  2. 2.Active: PORT 192,168,155,20,140,17 (client listens on 35857)

  3. 3.Server opens data from :20 to client:35857 -> client firewall drops the SYN -> 425

  4. 4.Passive: PASV -> 227 Entering Passive Mode (192,168,155,10,117,56) = port 30008

  5. 5.Client opens data to server:30008 (outgoing) -> transfer completes

Browsers and FTP clients use passive by default

Terminal
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
Output
< 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

Terminal
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 ...
Output
> 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

Terminal
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.dstport
Output
192.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.
ProtocolTransport / portEncryptionNotes
FTPTCP 21 + data portsNone, all plain textTwo connections, active/passive
FTPSTCP 21 (AUTH TLS) or 990TLSSame old FTP + TLS, two-connection problem stays
SFTPTCP 22SSHSSH subsystem, not related to FTP
SCPTCP 22SSHCopy in one command; new OpenSSH uses SFTP underneath
TFTPUDP 69None, no login512-byte blocks, ACK per block; phones, PXE
rsyncOver SSH (TCP 22)SSHDelta transfer: only changed blocks

135.5 SFTP and SCP

Terminal
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.txt
Output
sftp> 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: sftp
Terminal
tshark -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'
Output
  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 orders

135.6 TFTP: UDP 69, no login

IP phone / PXE boot with TFTP
  1. 1.Phone boots and gets the TFTP server address from DHCP

  2. 2.Read Request (RRQ) phone-1001.cfg to 192.168.155.10 UDP 69

  3. 3.Server answers from a new port (39045): Data block 1, max 512 bytes

  4. 4.Client ACKs every block; next block follows

  5. 5.A PXE client fetches pxelinux.0 the same way

Terminal
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.10
Output
pxelinux.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: 2

135.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).
Terminal
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/
Output
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.25
Terminal
rsync -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/
Output
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

JobBest choiceWhy
Upload files to a serverSFTP / SCPEncrypted, single port 22
Nightly backup / mirrorrsync over SSHSends only changed blocks, --delete for mirrors
IP phone config, PXE bootTFTPTiny client, no login, fits in firmware
Legacy partner needs FTPFTPS, passive modeAt 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.