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.
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.
scp.
user@host:/path.
-r is
required.
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
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 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.
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.