Moving Files With scp and sftp
Goal
With scp and sftp you upload and download files, preserve timestamps, and confirm integrity with checksums. At the end, you take an incident in which three nightly upload jobs failed for different reasons, tell them apart with their error lines and the actually applied configuration, and fix them.
Why it matters
A transfer being "finished" and it having "arrived correctly" are different things. In practice you always confirm with checksums. The fact that scp's port option is uppercase -P looks trivial, but because in ssh lowercase is the port, people really do get it wrong often. Lowercase -p is preserve timestamps, so it quietly does something different and moves on. And scp, sftp, and rsync all run on top of ssh, which is why the reason a transfer fails usually lies not in the transfer tool but in the layer beneath it (a line the remote shell prints, a changed host key, a configuration block that matched first). If you erase the symptom by adding options, the cause is left to the next person.
In this lab, think of 127.0.0.1:2222 as a different server. You use the /root/.ssh/id_labhub key you made in the previous lab as it is (if it is not there, the step 1 answer key creates it).
Steps
- Create the
/root/xfer/srcdirectory and create three files inside it:a.txt(at least 1KB),b.txt(at least 1KB), andc.bin(at least 10KB). - Create the remote directory
/root/xfer/dst/and uploada.txtthere. - Recursively copy the whole
/root/xfer/srcdirectory under the remote/root/xfer/tree/. As a result,/root/xfer/tree/src/b.txtmust exist. - Download the remote
/root/xfer/dst/a.txtto the local/root/xfer/back/. - Write sftp batch commands in
/root/xfer/batch.txtand run it. The batch must upload/root/xfer/src/c.binto the remote/root/xfer/dst/. Save the execution log to/root/xfer/sftp.log. - Copy
b.txtto the remote/root/xfer/dst/with timestamps preserved, and write the mtime (in seconds) of the original and the copy, one per line, in/root/xfer/mtime.txt. The two values must be the same. - Make a sha256 manifest of the three files in
/root/xfer/src, verify that the hashes of the files that arrived in/root/xfer/dstand/root/xfer/tree/srcmatch, and save the result to/root/xfer/verify.txt. - Incident:
incident ss-upload-fail. All three upload jobs of the batch account uploader (reports, images, backup) fail. Leave the job scripts and the server configuration as they are, and without turning off host key checking, get all three succeeding again, then for each job write a cause code and one line of evidence to/root/incident/ss-upload-fail/report.txt.
Notes
- Remote notation is
사용자@호스트:경로(user@host:path). If you leave out the colon, scp quietly makes a local copy. - If you append a command, ssh runs only that command remotely without opening a shell; use this to create the remote directory beforehand.
- In an sftp batch file, write sftp's own commands (cd, put, get, bye, and so on), not shell commands, one per line. If even one line fails, it stops there.
- Common mistake 1: using lowercase
-pas the port in step 2. The port goes to 22, and 2222 is read as a file name. - Common mistake 2: in step 3, if the destination path does not exist, the directory depth changes. Check the required final path.
- The job in step 8 runs not as root but as uploader. Use
runuser -u uploader -- <명령>(command) to see that account's configuration and known_hosts. - Incident tools:
incident listshows the incidents you can raise, andincident show <이름>(name) shows the ticket again. Raising the same incident again returns it to its original broken state. - Report:
/root/incident/<이름>/report.txt(name is the incident name): one line이름: 원인코드(name: cause code) per target, plusevidence: <관찰한 출력 한 줄>(one line of output you observed). The evidence must be a line you actually saw, not a format example.
Preparing the files to transfer
Create the /root/xfer/src directory and create three files inside it: a.txt (at least 1KB), b.txt (at least 1KB), and c.bin (at least 10KB).
Three files of different sizes are enough. You may make them with head or dd, or fill them with base64.
Uploading a file
Create the remote directory /root/xfer/dst/ and upload a.txt there.
Specifying the port is the uppercase option. Be careful, because the lowercase one means something completely different.
Uploading a whole directory
Recursively copy the whole /root/xfer/src directory under the remote /root/xfer/tree/. As a result, /root/xfer/tree/src/b.txt must exist.
To send a directory you need the recursive option. If the destination path does not exist, create it beforehand.
Downloading
Download the remote /root/xfer/dst/a.txt to the local /root/xfer/back/.
You write the remote path first and the local path after. The remote notation is in the user@host:path format.
Running an sftp batch
Write sftp batch commands in /root/xfer/batch.txt and run it. The batch must upload /root/xfer/src/c.bin to the remote /root/xfer/dst/. Save the execution log to /root/xfer/sftp.log.
sftp has an option that reads a list of commands from a file. In the batch file you write one command per line.
Preserving timestamps
Copy b.txt to the remote /root/xfer/dst/ with timestamps preserved, and write the mtime (in seconds) of the original and the copy, one per line, in /root/xfer/mtime.txt. The two values must be the same.
The preserve option is lowercase. Check by comparing the mtime with stat.
Integrity verification
Make a sha256 manifest of the three files in /root/xfer/src, verify that the hashes of the files that arrived in /root/xfer/dst and /root/xfer/tree/src match, and save the result to /root/xfer/verify.txt.
Make the manifest with sha256sum and check with -c. It is convenient to match the paths so that the original and the copy have the same name.
Incident: all three nightly uploads fail
Running incident ss-upload-fail shows a ticket. The three jobs (reports, images, backup) in which the batch account uploader uploads files to three servers all fail. Fix it so all three succeed again. You must not change the job scripts or sftp batch files (/srv/ss-upload/jobs/) or the sshd configuration of the three servers, and turning off host key checking is not allowed either. In /root/incident/ss-upload-fail/report.txt, write one line per job, such as reports: <코드> (code), plus evidence: <관찰한 출력 한 줄> (one line of output you observed). Candidate codes: config-precedence (a value from an earlier block wins in the ssh configuration), host-key-changed (the host key in known_hosts differs from the server's current key), key-permissions (the private key's permissions are loose, so the client discards the key), rc-output (the remote shell's startup file prints output and breaks the transfer protocol), wrong-port (it connects to the wrong port).
Direction: all three are "failures", but the error lines differ, and so do the broken layers: the receiving server's account, the client's grounds for trusting the server, and the client's configuration. The jobs run not as root but as uploader, so you must reproduce as the same account to see the same configuration and the same known_hosts. Tools: the job logs, ssh run as the same account (runuser), ssh -G, which shows the actually applied values, the two key tools that fetch the key a server presents and turn it into a fingerprint, and the per-server logs. Concretely: look at what sftp adds that you should check when it says "message too long", what is printed in a non-interactive session that sends only one command, the fingerprint in the host key warning and the one in the infrastructure team's list, and the user name that actually applies to backup and the block that set that value first.