File transfer¶
Practical 7.4 file-transfer-annex.pdf · PDF
You need to put the whole documentation of the Tramuntana office server on the server: dozens of files, one of them 5 MB. Tomorrow you will change two of them and send it all again. Next week, again. Do you attach everything to an email? The attachment has a size limit, and the email travels as a postcard.
Moving files between machines is one of the oldest jobs of a network, older even than email. Every developer does it daily: uploading a website, sending a build to a server, keeping a backup copy up to date. Today you will learn the three tools that do it safely, and the one old tool you must refuse.
What you will learn
- Explain why plain FTP is not used any more
- Move files to and from a server in an interactive SFTP session
- Copy a file or a folder in one line with SCP
- Keep a folder on the server equal to your local one with rsync, and why the second run sends almost nothing
- Prove that a copy is identical with a checksum
- Why SFTP is not "FTP with security added"
FTP: the protocol that trusted everybody¶
April 1971: Abhay Bhushan, a student at MIT, writes RFC 114, "A File Transfer Protocol". Moving files was one of the first things anybody wanted from a network, months before the first email with an @. The version still called FTP (File Transfer Protocol) today is RFC 959, from October 1985.
It was designed for a network of researchers who trusted each other. It encrypts nothing. Watch what travels when someone logs in to an FTP server (captured with tcpdump, a program that shows the traffic on a network interface):
# tcpdump -i lo -l -A -n "tcp dst port 21" | grep -a -o -E "(USER|PASS) .*"
USER marta
PASS Tramuntana-2027
The user name and the password, readable by anyone on the path: the Wi-Fi, a switch, every router on the way. The files travel the same way. That is why Firefox 90 and Chrome 95 removed FTP support in 2021.
Careful: FTP inside the office is not fine either
The password is readable by anyone on that network, and people reuse passwords. If a supplier gives you an FTP address and a password "to upload the invoices", ask for SFTP instead.
The armoured van: everything inside SSH¶
The analogy for this page: SSH is an armoured van. You already have an encrypted tunnel to the server, the one you use with ssh. Instead of inventing a new protocol, put the files inside that van. Three tools ride in the same van, on the same port 22, with the same keys:
flowchart LR
subgraph PC ["Your PC"]
SFTP["<b>sftp</b><br>walk around and pick"]
SCP["<b>scp</b><br>one line, one copy"]
RSYNC["<b>rsync</b><br>send only what changed"]
end
SFTP --> VAN
SCP --> VAN
RSYNC --> VAN
VAN["<b>SSH tunnel, port 22</b><br>the armoured van:<br>encrypted, same keys"] --> SRV["<b>Server</b><br>ubu-srv or the online VM"]
| Tool | What it is | Use it when |
|---|---|---|
sftp |
An interactive session: you walk around the server (ls, cd) and move files (put, get) |
You do not know yet what is there, or you move a few files by hand |
scp |
One line, copies and exits. Same shape as cp |
One file or folder, now, or inside a script |
rsync |
Compares both sides and sends only what changed | A folder you will send again and again: documentation, backups, a website |
Two facts for the curious: since OpenSSH 9.0 (April 2022), scp speaks the SFTP protocol inside; and rsync was first announced in June 1996 by Andrew Tridgell and Paul Mackerras, in Australia.
Careful: SFTP and FTPS are not the same
SFTP (SSH File Transfer Protocol) is a different protocol that lives inside SSH, on port 22. FTPS is the old FTP wrapped in TLS (RFC 4217, 2005). They only share three letters.
Getting ready¶
In the practical, a variable keeps the address of your server so you do not type it every time. export makes it live only in that terminal:
mkdir -p ~/transfer-lab/docs && cd ~/transfer-lab
echo "# Tramuntana office server" > README.md
for n in 01-overview 02-shares 03-containers; do echo "# $n" > docs/$n.md; done
head -c 5M /dev/urandom > manual.pdf # a 5 MB file that plays the role of a PDF
export SRV=user@host # write YOUR user and host
ssh $SRV hostname # if it asks for a password: ssh-copy-id $SRV
When ssh $SRV hostname prints the server's name without asking for a password, your key is in place and the van is ready.
sftp: walk around and move files¶
sftp $SRV
sftp> pwd (where am I on the server?)
sftp> lpwd (where am I on my PC?)
sftp> mkdir incoming
sftp> cd incoming
sftp> put README.md (my PC -> server)
sftp> ls -l
sftp> get /etc/hostname server-name.txt (server -> my PC)
sftp> bye
pwd answers Remote working directory: /home/marta; lpwd answers Local working directory: /home/marta/transfer-lab. Two rules make sftp easy:
putandgetare named from your side:putsends to the server,getbrings from it.- An
lin front means local:lpwd,lls,lcdact on your PC.
scp: one line, one copy¶
scp manual.pdf $SRV:incoming/ # one file, to the server
scp -r docs $SRV:incoming/ # a whole folder (-r = recursive)
scp $SRV:/etc/os-release . # from the server to here (the dot = this folder)
ssh $SRV ls -l incoming incoming/docs
The shape is scp source destination, just like cp. A path with $SRV: in front is on the server, relative to your home there. Each file prints a progress line:
scp always sends the whole file, every time. Fine once; wasteful when you send the same folder every evening.
rsync: send only what changed¶
-a (archive) copies folders recursively and keeps dates and permissions; -v (verbose) lists what it sends. Now run it three times and read the last lines.
About 5.2 MB, then 249 bytes, then 326 bytes. rsync compares both sides first and sends only the differences. The speedup is the total size divided by what really travelled. Byte counts change a little from machine to machine, but the shape does not.
It even works inside a file. Add one line at the end of the 5 MB file and ask for statistics:
Literal data: 1,086 bytes # sent as new data
Matched data: 5,241,808 bytes # already on the server: not sent
Careful: the slash at the end matters
~/transfer-lab/ (with a final /) copies the content of the folder. Without the slash, rsync copies the folder itself and you end up with mirror/transfer-lab/....
--delete, but dry-run first¶
If you delete a file locally, rsync leaves it on the server unless you add --delete. That option deletes on the destination whatever no longer exists in the source. With the two sides swapped by mistake, it deletes your work. So the habit is: --dry-run first, read the list, then run it for real.
The output prints deleting docs/03-containers.md and ends with (DRY RUN): nothing has happened yet. Run the same line without --dry-run to do it.
Careful: a mirror is not a backup
rsync makes a mirror: an exact copy of the source, so a mistake is mirrored too. A backup keeps old versions.
Prove the copy is identical¶
"The copy finished" is not proof. A checksum (or hash) is a short fingerprint calculated from every byte of a file: one different byte gives a completely different result. Compute it on both sides:
SHA-256 gives 64 hexadecimal characters. The same 64 characters on both sides means identical content.
Which tool?¶
| Job | Tool | Why |
|---|---|---|
| Every evening, send the documentation folder (300 files, 3 changed) | rsync |
Repeated; only the changes travel |
| Find an old report on a server you do not know, and bring it | sftp |
Look around first |
| A script copies one log file to the server each time it runs | scp |
One file, one line |
| A supplier gives you an FTP address and a password | None | FTP sends the password in clear text; ask for SFTP |
Key takeaways¶
- FTP (RFC 959, 1985) encrypts nothing: user, password and files travel in clear text. Browsers dropped it in 2021.
- SFTP, SCP and rsync all travel inside SSH, on port 22, with the same keys: the armoured van.
sftpis interactive (putsends,getbrings,lmeans local);scpcopies in one line and always sends the whole file.- rsync compares both sides and sends only what changed;
--deletemirrors deletions, so use--dry-runfirst. - A checksum such as
sha256sumproves the copy is identical. SFTP and FTPS are different protocols.
Quick check¶
1. Why is plain FTP not used any more?
- It is too slow for big files
- It only works between Windows machines
- The password and the files travel in clear text
- It cannot copy whole folders
FTP was designed in a network that trusted itself and encrypts nothing: tcpdump shows USER and PASS in clear. That is why browsers removed it in 2021.
2. How do sftp, scp and rsync travel when you use them as on this page?
- Inside TLS, on port 443
- Inside SSH, on port 22
- In clear text, on port 21
- Inside SMTP, on port 25
All three ride in the same armoured van: the SSH tunnel on port 22, with the same keys you use for ssh. Port 21 is the old FTP.
3. You run exactly the same rsync -av command twice. What does the second run do?
- It sends almost nothing, because only differences travel
- It copies every file again from the start
- It deletes the folder on the destination
- It fails because the files already exist
rsync compares both sides first. Nothing changed, so it sent only 249 bytes of bookkeeping instead of 5.2 MB.
4. How do you prove that the file on the server is identical to yours?
- The size of both files looks the same
- Both files have the same name
- scp printed 100% at the end
- sha256sum gives the same result on both sides
A checksum depends on every byte: one different byte changes the result completely. The same SHA-256 on both ends means identical content; size and name prove nothing.
5. Inside an sftp session, what does put README.md do?
- Downloads README.md from the server to your PC
- Sends README.md from your PC to the server
- Shows the content of README.md on the screen
- Deletes README.md from the server
put and get are named from your side: put sends to the server, get brings from it. Commands with an l in front (lpwd, lls) act on your PC.
6. Which statement about SFTP and FTPS is true?
- SFTP is FTP with encryption added on top
- They are two names for the same protocol
- SFTP lives inside SSH; FTPS is the old FTP wrapped in TLS
- FTPS lives inside SSH; SFTP is the old FTP wrapped in TLS
SFTP is a different protocol that runs inside SSH on port 22. FTPS is FTP wrapped in TLS. They only share letters.
7. You want the server's copy of your docs folder to lose the file you just deleted locally. What is the safe way?
- Run scp -r again, so it replaces the folder
- Run rsync with --delete --dry-run, read the list, then run it without --dry-run
- Run rsync with --delete straight away, since it only deletes on your PC
- Delete the whole folder on the server and send it again with sftp
--delete removes on the destination what no longer exists in the source. A dry run shows deleting ... and (DRY RUN) without touching anything, so you catch a mistake before it happens.
8. Every evening a script must send the documentation folder (300 files, 3 changed) to the server. Which tool fits best?
- rsync
- scp
- sftp
- ftp
rsync is made for a folder you send again and again: it sends only the 3 changed files. scp would resend all 300, sftp is interactive, and FTP sends the password in clear text.
That was the last stop: from the bits inside a CPU to documenting, sharing and moving the work of a real office. Look back at the whole journey on the course home.