Skip to content

Transferring files with SFTP

Module
Basic Connectivity and Remote Access
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (two lab containers), GNU Bash 5.2.21
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.

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.

At the local shell, learner@rootvolt:~/lab$, create the directory:

Terminal window
mkdir ~/lab/b14-transfer
cd ~/lab/b14-transfer

Use your editor to create note.txt in this local directory with these contents:

Contents to write in the local editor
Connectivity practice
Prepared 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:

Local source file
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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. LOCAL source: note.txt contains two lines before any transfer starts.
Steps (5)
  1. Type printf 'Connectivity practice\nPrepared on rootvolt.\n' > note.txt
  2. Press Enter
  3. Type cat note.txt
  4. Press Enter
  5. 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.

On the trusted server console, learner@labserver:~$, read the host fingerprint:

Terminal window
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
Server console: verify the SFTP host identity
learner@labserver:~$ ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub256 SHA256:84WqeD9KUlpjJpSW5xtNl8VKN+S6MCp4kivDZRLJYAU root@labserver (ED25519)learner@labserver:~$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS (lab server console), GNU Bash 5.2.21. SERVER console: obtain this capture's fingerprint independently before accepting it in SFTP.
Steps (3)
  1. Type ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
  2. Press Enter
  3. Output: 256 SHA256:84WqeD9KUlpjJpSW5xtNl8VKN+S6MCp4kivDZRLJYAU root@labserver (ED25519)

Then, from the local shell in ~/lab/b14-transfer, start:

Terminal window
sftp learner@labserver

Compare 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.

At sftp>, enter:

Commands typed at sftp>
lpwd
pwd
mkdir b14-transfer
put note.txt b14-transfer/upload.txt
get b14-transfer/upload.txt received.txt
ls b14-transfer
bye

Read 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
SFTP: labeled local and remote transfer paths
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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. LOCAL note.txt is uploaded as REMOTE b14-transfer/upload.txt, then downloaded as LOCAL received.txt; lpwd and pwd identify the separate roots.
Steps (29)
  1. Type sftp learner@labserver
  2. Press Enter — LOCAL client: compare this fingerprint with the trusted server console
  3. 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])?
  4. Type yes into the running program
  5. Press Enter — Accept only after the fingerprint matches
  6. Output: Warning: Permanently added 'labserver' (ED25519) to the list of known hosts. learner@labserver's password:
  7. Type the password (the terminal shows nothing)
  8. Press Enter
  9. Output: Connected to labserver. sftp>
  10. Type lpwd into the running program
  11. Press Enter — Local working directory, on rootvolt
  12. Output: Local working directory: /home/learner/lab/b14-transfer sftp>
  13. Type pwd into the running program
  14. Press Enter — Remote working directory, on labserver
  15. Output: Remote working directory: /home/learner sftp>
  16. Type mkdir b14-transfer into the running program
  17. Press Enter — Creates the folder on the remote side
  18. Output: sftp>
  19. Type put note.txt b14-transfer/upload.txt into the running program
  20. Press Enter — Upload: local note.txt to remote b14-transfer/upload.txt
  21. Output: Uploading note.txt to /home/learner/b14-transfer/upload.txt note.txt 100% 44 157.7KB/s 00:00 sftp>
  22. Type get b14-transfer/upload.txt received.txt into the running program
  23. Press Enter — Download: remote upload.txt to local received.txt
  24. Output: Fetching /home/learner/b14-transfer/upload.txt to received.txt upload.txt 100% 44 37.9KB/s 00:00 sftp>
  25. Type ls b14-transfer into the running program
  26. Press Enter — Remote listing: the upload is still there after get
  27. Output: b14-transfer/upload.txt sftp>
  28. Type bye into the running program
  29. 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.

At the local shell, verify the downloaded file, then open an SSH shell on the same server:

Terminal window
cat received.txt
ssh learner@labserver

This 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:

Terminal window
cat ~/b14-transfer/upload.txt
Verify both destinations and reset the server
learner@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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. The local download and remote upload both contain the source text; remote cleanup then removes only the uploaded file and its directory.
Steps (22)
  1. Type cat received.txt
  2. Press Enter — LOCAL: verify the downloaded destination
  3. Output: Connectivity practice Prepared on rootvolt.
  4. Type ssh learner@labserver
  5. Press Enter
  6. Output: learner@labserver's password:
  7. Type the password (the terminal shows nothing)
  8. Press Enter
  9. 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.
  10. Type cat ~/b14-transfer/upload.txt
  11. Press Enter
  12. Output: Connectivity practice Prepared on rootvolt.
  13. Type rm ~/b14-transfer/upload.txt
  14. Press Enter
  15. Type rmdir ~/b14-transfer
  16. Press Enter
  17. Type test ! -e ~/b14-transfer && printf 'Remote reset complete\n'
  18. Press Enter
  19. Output: Remote reset complete
  20. Type exit
  21. Press Enter
  22. 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.

The verification replay continues with this remote reset:

Terminal window
rm ~/b14-transfer/upload.txt
rmdir ~/b14-transfer
test ! -e ~/b14-transfer && printf 'Remote reset complete\n'
exit

After the local prompt returns, remove the local source and download:

Terminal window
rm note.txt received.txt
cd ~/lab
rmdir ~/lab/b14-transfer
ls -a ~/.ssh
ssh-keygen -R labserver
ssh-keygen -F labserver || printf 'No saved key for labserver\n'
test ! -e ~/lab/b14-transfer && printf 'Local reset complete\n'
Reset local exercise files and trust in labserver
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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. LOCAL reset removes the two practice copies, the empty directory, and only labserver's saved host keys.
Steps (18)
  1. Type rm note.txt received.txt
  2. Press Enter
  3. Type cd ~/lab
  4. Press Enter
  5. Type rmdir ~/lab/b14-transfer
  6. Press Enter
  7. Type ls -a ~/.ssh
  8. Press Enter — Inspect the SSH directory before removing the labserver entry
  9. Output: . .. known_hosts known_hosts.old
  10. Type ssh-keygen -R labserver
  11. Press Enter — Remove only saved host keys for labserver
  12. 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
  13. Type ssh-keygen -F labserver || printf 'No saved key for labserver\n'
  14. Press Enter — Confirm no saved key for labserver remains
  15. Output: No saved key for labserver
  16. Type test ! -e ~/lab/b14-transfer && printf 'Local reset complete\n'
  17. Press Enter — Confirm the local exercise reset is complete
  18. 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.

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.

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.