Loading...

Lab 68: SCP - Secure Copy Protocol

Transfer files securely over SSH using scp for both upload and download workflows. Practice single-file transfers and recursive directory copies to move artifacts between hosts.

networking access core

Scenario

You need to securely transfer files between local and remote systems. You will use scp to upload a file to a remote backup directory, download a remote file back to your local working directory, and then recursively copy a directory to the same remote location.

Operator context

scp uses SSH for transport, so it inherits SSH authentication, encryption, and host key verification. In production, prefer key-based authentication and treat host key prompts as a security checkpoint, not a formality.

Objective

  • Upload a local file to a remote host using scp.
  • Download a remote file into the current local directory.
  • Recursively copy a directory to a remote destination.

Concepts

  • SSH-based transport: encryption, authentication, and host key verification during file copy.
  • Source and destination syntax: local paths versus user@host:/path.
  • Remote path intent: copying into a directory versus copying to an explicit filename.
  • Recursive transfer behavior and when -r is required.
  • Progress output as evidence: size, speed, and completion status.
  • Safer operations: validate paths before transfer to avoid writing into the wrong location.

Walkthrough

Step 1 : Upload testfile.txt to the remote backup directory.
Command
scp testfile.txt labuser@192.168.1.50:/home/labuser/remote_dir

This uploads the local file from your current directory to the remote path over SSH. The destination must be a valid directory or a full file path on the remote host.

testfile.txt                                 100%   12KB  120.0KB/s   00:00
Step 2 : Download remote.txt into the current local directory.
Command
scp labuser@192.168.1.50:/home/labuser/remote_dir/remote.txt .

The final . means “download into the current working directory.” This is a common pattern when pulling logs or artifacts from a remote system for review.

remote.txt                                   100%    8KB   80.0KB/s   00:00
Step 3 : Recursively copy mydir/ to the remote backup directory.
Command
scp -r mydir labuser@192.168.1.50:/home/labuser/remote_dir

The -r flag is required for directories. Without it, scp will fail because it cannot copy directory trees by default.

mydir/file1.txt                              100%    1KB   10.0KB/s   00:00
mydir/file2.txt                              100%    1KB   10.0KB/s   00:00

Common breakpoints

Host key verification prompt appears unexpectedly

Verify you are connecting to the correct host before accepting a new host key. In managed environments, compare fingerprints to a trusted inventory or request confirmation through the approved channel.

Permission denied (publickey,password)

Credentials or SSH key access is not valid for the target. Confirm the correct user, key, and SSH access policy, then retry.

No such file or directory on the remote path

The destination directory does not exist or the path is wrong. Confirm the remote directory structure and whether you should copy into a directory or to a full filename.

Recursive copy misses expected files

Confirm you copied the correct source directory and that the remote destination has enough permissions and space. If required, verify remote results after transfer.

Cleanup checklist

Cleanup is verifying local and remote paths are correct and that you did not leave sensitive artifacts in the wrong directory. If this was a test transfer, remove the copied files per policy.

Commands
ls -l testfile.txt remote.txt
# If this was a test run, remove local artifacts:
# rm -f testfile.txt remote.txt
Success signal

Transfers complete without unexpected prompts or path ambiguity, and you can show the expected files in the expected locations.

Reference

  • scp <src> user@host:<dest> : Copies a local file to a remote host over SSH.
  • scp user@host:<src> <dest> : Copies a file from a remote host to a local destination.
  • scp -r <dir> user@host:<dest> : Recursively copies a directory to a remote host.
    • -r : Enables recursive directory copy.
  • user@host: : SSH target format used by scp (user and hostname/IP).
  • . : Represents the current working directory when used as a destination.