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.
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
-ris required. - Progress output as evidence: size, speed, and completion status.
- Safer operations: validate paths before transfer to avoid writing into the wrong location.
Walkthrough
testfile.txt to the remote backup
directory.
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
remote.txt into the current local
directory.
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
mydir/ to the remote backup
directory.
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
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.
Credentials or SSH key access is not valid for the target. Confirm the correct user, key, and SSH access policy, then retry.
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.
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.
ls -l testfile.txt remote.txt
# If this was a test run, remove local artifacts:
# rm -f testfile.txt remote.txt
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 byscp(user and hostname/IP). -
.: Represents the current working directory when used as a destination.