Transferring files with SFTP
On this page
Your note is on the local machine, but someone needs it on the lab server. Copying a pathname into the remote shell will not make the file appear there. You need a transfer, and you need to know which machine each pathname belongs to.
By the end, you can upload a local file with SFTP, download a copy under another name, verify both destinations, and reset both filesystems.
Before you start
Section titled “Before you start”Finish First SSH connection, including its reset. Use ~/lab from the B04 setup, and the prepared lab server. Reserve local ~/lab/b14-transfer and remote ~/b14-transfer; both must be absent initially.
The capture uses a private Docker bridge between two containers, not a home network. The lab hostname labserver resolves only in the prepared environment. Your addresses, fingerprints, transfer speeds, and login banner will differ. The SSH banner’s WSL2 kernel string is the lab container’s shared kernel; your banner names your own system and kernel.
SFTP transfers files over SSH’s encrypted transport. It uses the server identity and account authentication from the previous lesson. Its prompt, sftp>, is an interactive file-transfer prompt, not a Bash shell.
This SFTP recording uses a recreated server, so its host key differs from the SSH lesson’s. Before connecting, follow the console check and lab trust-reset guidance if a saved labserver entry remains.
Prepare a small local note
Section titled “Prepare a small local note”At the local shell, learner@rootvolt:~/lab$, create the directory:
mkdir ~/lab/b14-transfercd ~/lab/b14-transferUse your editor to create note.txt in this local directory with these contents:
Connectivity practicePrepared on rootvolt.Save the file, exit the editor, and read it with cat note.txt. The recording creates the same text with printf so that the transfer demonstration stays focused on files rather than editor keystrokes:
learner@rootvolt:~/lab/b14-transfer$ printf 'Connectivity practice\nPrepared on rootvolt.\n' > note.txtlearner@rootvolt:~/lab/b14-transfer$ cat note.txtConnectivity practice
Prepared on rootvolt.learner@rootvolt:~/lab/b14-transfer$ Steps (5)
- Type
printf 'Connectivity practice\nPrepared on rootvolt.\n' > note.txt - Press Enter
- Type
cat note.txt - Press Enter
- Output:
Connectivity practice Prepared on rootvolt.
The source is 44 bytes in the capture. You will make two additional copies with different names. Transferring copies does not move or delete the original note.
Verify the server before starting SFTP
Section titled “Verify the server before starting SFTP”On the trusted server console, learner@labserver:~$, read the host fingerprint:
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.publearner@labserver:~$ ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub256 SHA256:84WqeD9KUlpjJpSW5xtNl8VKN+S6MCp4kivDZRLJYAU root@labserver (ED25519)learner@labserver:~$ Steps (3)
- Type
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub - Press Enter
- Output:
256 SHA256:84WqeD9KUlpjJpSW5xtNl8VKN+S6MCp4kivDZRLJYAU root@labserver (ED25519)
Then, from the local shell in ~/lab/b14-transfer, start:
sftp learner@labserverCompare the entire ED25519 SHA256 fingerprint with the console’s value. Only if it matches, answer yes. Authenticate with the lab account password; it stays hidden while typed. If the values differ, stop the connection and investigate through the console. A fingerprint from B14.4’s separate capture will not match this freshly created server.
After authentication, the client displays sftp>. It is ready for transfer commands. You are still running the client locally, with access to a remote filesystem through the connection.
Label the two working directories
Section titled “Label the two working directories”At sftp>, enter:
lpwdpwdmkdir b14-transferput note.txt b14-transfer/upload.txtget b14-transfer/upload.txt received.txtls b14-transferbyeRead lpwd as “local print working directory.” SFTP’s pwd reports the remote working directory. Its mkdir and ls act remotely. These are SFTP commands with their own meanings, even when the names resemble shell commands.
Here are the two sides together:
| Side | Paths used in this capture |
|---|---|
| Local, rootvolt | Working directory /home/learner/lab/b14-transfer; source note.txt; download received.txt |
| Remote, labserver | Working directory /home/learner; created directory b14-transfer; upload b14-transfer/upload.txt |
put takes a local source followed by a remote destination. get takes a remote source followed by a local destination. Relative paths are resolved against the corresponding side’s working directory. The shared username and home-directory spelling do not make the two filesystems the same.
Predict: after get finishes, is the remote upload removed because you downloaded it?
Run it, then compare with your guess
learner@rootvolt:~/lab/b14-transfer$ sftp learner@labserverThe authenticity of host 'labserver (172.19.0.2)' can't be established.
ED25519 key fingerprint is SHA256:84WqeD9KUlpjJpSW5xtNl8VKN+S6MCp4kivDZRLJYAU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yesWarning: Permanently added 'labserver' (ED25519) to the list of known hosts.
learner@labserver's password: Connected to labserver.
sftp> lpwdLocal working directory: /home/learner/lab/b14-transfer
sftp> pwdRemote working directory: /home/learner
sftp> mkdir b14-transfersftp> put note.txt b14-transfer/upload.txtUploading note.txt to /home/learner/b14-transfer/upload.txt
note.txt 100% 44 157.7KB/s 00:00
sftp> get b14-transfer/upload.txt received.txtFetching /home/learner/b14-transfer/upload.txt to received.txt
upload.txt 100% 44 37.9KB/s 00:00
sftp> ls b14-transferb14-transfer/upload.txt
sftp> byelearner@rootvolt:~/lab/b14-transfer$ Steps (29)
- Type
sftp learner@labserver - Press Enter — LOCAL client: compare this fingerprint with the trusted server console
- Output:
The authenticity of host 'labserver (172.19.0.2)' can't be established. ED25519 key fingerprint is SHA256:84WqeD9KUlpjJpSW5xtNl8VKN+S6MCp4kivDZRLJYAU. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? - Type
yesinto the running program - Press Enter — Accept only after the fingerprint matches
- Output:
Warning: Permanently added 'labserver' (ED25519) to the list of known hosts. learner@labserver's password: - Type the password (the terminal shows nothing)
- Press Enter
- Output:
Connected to labserver. sftp> - Type
lpwdinto the running program - Press Enter — Local working directory, on rootvolt
- Output:
Local working directory: /home/learner/lab/b14-transfer sftp> - Type
pwdinto the running program - Press Enter — Remote working directory, on labserver
- Output:
Remote working directory: /home/learner sftp> - Type
mkdir b14-transferinto the running program - Press Enter — Creates the folder on the remote side
- Output:
sftp> - Type
put note.txt b14-transfer/upload.txtinto the running program - Press Enter — Upload: local note.txt to remote b14-transfer/upload.txt
- Output:
Uploading note.txt to /home/learner/b14-transfer/upload.txt note.txt 100% 44 157.7KB/s 00:00 sftp> - Type
get b14-transfer/upload.txt received.txtinto the running program - Press Enter — Download: remote upload.txt to local received.txt
- Output:
Fetching /home/learner/b14-transfer/upload.txt to received.txt upload.txt 100% 44 37.9KB/s 00:00 sftp> - Type
ls b14-transferinto the running program - Press Enter — Remote listing: the upload is still there after get
- Output:
b14-transfer/upload.txt sftp> - Type
byeinto the running program - Press Enter — Ends SFTP; the local shell prompt returns
No. Both operations copy data. The remote ls still lists the upload after get, and the original local note also remains. Bye ends the SFTP client and returns to the local shell.
Read the path mapping alongside the replay. Follow each copy from its source machine to its destination, using the captured lpwd and pwd results to resolve relative names.
Verify content at both destinations
Section titled “Verify content at both destinations”At the local shell, verify the downloaded file, then open an SSH shell on the same server:
cat received.txtssh learner@labserverThis connection recognizes the host key already accepted by SFTP in the same run. It asks for the account password again. At the remote shell, learner@labserver:~$, verify the uploaded file:
cat ~/b14-transfer/upload.txtlearner@rootvolt:~/lab/b14-transfer$ cat received.txtConnectivity practice
Prepared on rootvolt.learner@rootvolt:~/lab/b14-transfer$ ssh learner@labserverlearner@labserver's password: Welcome to Ubuntu 24.04.5 LTS (GNU/Linux 6.18.33.1-microsoft-standard-WSL2 x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/pro
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.learner@labserver:~$ cat ~/b14-transfer/upload.txtConnectivity practice
Prepared on rootvolt.learner@labserver:~$ rm ~/b14-transfer/upload.txtlearner@labserver:~$ rmdir ~/b14-transferlearner@labserver:~$ test ! -e ~/b14-transfer && printf 'Remote reset complete\n'Remote reset completelearner@labserver:~$ exitlogout
Connection to labserver closed.learner@rootvolt:~/lab/b14-transfer$ Steps (22)
- Type
cat received.txt - Press Enter — LOCAL: verify the downloaded destination
- Output:
Connectivity practice Prepared on rootvolt. - Type
ssh learner@labserver - Press Enter
- Output:
learner@labserver's password: - Type the password (the terminal shows nothing)
- Press Enter
- Output:
Welcome to Ubuntu 24.04.5 LTS (GNU/Linux 6.18.33.1-microsoft-standard-WSL2 x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/pro To run a command as administrator (user "root"), use "sudo <command>". See "man sudo_root" for details. - Type
cat ~/b14-transfer/upload.txt - Press Enter
- Output:
Connectivity practice Prepared on rootvolt. - Type
rm ~/b14-transfer/upload.txt - Press Enter
- Type
rmdir ~/b14-transfer - Press Enter
- Type
test ! -e ~/b14-transfer && printf 'Remote reset complete\n' - Press Enter
- Output:
Remote reset complete - Type
exit - Press Enter
- Output:
logout Connection to labserver closed.
Both reads show the two source lines. A transfer progress line reports completion; reading the destination establishes what arrived there. An ls listing alone would establish only the pathname, not its contents.
Reset both sides
Section titled “Reset both sides”The verification replay continues with this remote reset:
rm ~/b14-transfer/upload.txtrmdir ~/b14-transfertest ! -e ~/b14-transfer && printf 'Remote reset complete\n'exitAfter the local prompt returns, remove the local source and download:
rm note.txt received.txtcd ~/labrmdir ~/lab/b14-transferls -a ~/.sshssh-keygen -R labserverssh-keygen -F labserver || printf 'No saved key for labserver\n'test ! -e ~/lab/b14-transfer && printf 'Local reset complete\n'learner@rootvolt:~/lab/b14-transfer$ rm note.txt received.txtlearner@rootvolt:~/lab/b14-transfer$ cd ~/lablearner@rootvolt:~/lab$ rmdir ~/lab/b14-transferlearner@rootvolt:~/lab$ ls -a ~/.ssh. .. known_hosts known_hosts.oldlearner@rootvolt:~/lab$ ssh-keygen -R labserver# Host labserver found: line 1
# Host labserver found: line 2
# Host labserver found: line 3
/home/learner/.ssh/known_hosts updated.
Original contents retained as /home/learner/.ssh/known_hosts.oldlearner@rootvolt:~/lab$ ssh-keygen -F labserver || printf 'No saved key for labserver\n'No saved key for labserverlearner@rootvolt:~/lab$ test ! -e ~/lab/b14-transfer && printf 'Local reset complete\n'Local reset completelearner@rootvolt:~/lab$ Steps (18)
- Type
rm note.txt received.txt - Press Enter
- Type
cd ~/lab - Press Enter
- Type
rmdir ~/lab/b14-transfer - Press Enter
- Type
ls -a ~/.ssh - Press Enter — Inspect the SSH directory before removing the labserver entry
- Output:
. .. known_hosts known_hosts.old - Type
ssh-keygen -R labserver - Press Enter — Remove only saved host keys for labserver
- Output:
# Host labserver found: line 1 # Host labserver found: line 2 # Host labserver found: line 3 /home/learner/.ssh/known_hosts updated. Original contents retained as /home/learner/.ssh/known_hosts.old - Type
ssh-keygen -F labserver || printf 'No saved key for labserver\n' - Press Enter — Confirm no saved key for labserver remains
- Output:
No saved key for labserver - Type
test ! -e ~/lab/b14-transfer && printf 'Local reset complete\n' - Press Enter — Confirm the local exercise reset is complete
- Output:
Local reset complete
After removing the local source and download, the lookup confirms that no labserver key remains. Keep the SSH directory and trust-file backup as explained in the SSH reset.
The remote and local absence checks both succeed. Removing local files alone would leave the remote upload behind.
Mistakes to recognize
Section titled “Mistakes to recognize”Treating sftp> as a remote Bash prompt. Use SFTP commands there. Bye returns to the local shell; SSH opens a remote shell for the separate content check.
Reversing the direction of put and get. Put starts with a local source. Get starts with a remote source. Write down the two working directories before selecting either path.
Replacing a document accidentally. Upload and download destinations can overwrite files. This exercise uses fresh directories and reserved filenames on both machines.
Try it yourself
Section titled “Try it yourself”Goal: Explain which pathname changes if you download the same remote upload as review.txt instead of received.txt. State where to verify it.
Solution
Only the local destination name changes. The remote source remains b14-transfer/upload.txt. After bye, read review.txt locally. Do not run that read at the remote shell merely because the username is learner on both sides.
- SFTP uses SSH identity checks and authentication for file transfer.
- Lpwd describes the local directory; pwd describes the remote one.
- Put uploads a copy; get downloads a copy.
- Verify destination contents and reset both filesystems separately.
Next, Practice: Basic Connectivity and Remote Access combines inspection, retrieval, remote access, and transfer.
Further reading
Section titled “Further reading”- On Ubuntu:
man sftpandman ssh. - Ubuntu SFTP manual, especially interactive commands, local paths, put, and get.
- Ubuntu SSH manual, for host verification and login.