Learn how to securely transfer files between your local machine and virtual machines using SCP (Secure Copy Protocol). SCP provides encrypted file transfers over SSH connections, making it the standard method for secure data exchange with remote servers.
What is SCP?
SCP (Secure Copy Protocol) is a network protocol that enables secure file transfers between hosts over SSH. It combines the functionality of the traditional cp
(copy) command with the security of SSH encryption, ensuring your data remains protected during transfer.
Security: SCP automatically encrypts all data during transfer, including filenames, file contents, and authentication credentials.
Basic SCP Syntax
The general structure of an SCP command follows this pattern:
scp [options] source destination
Where:
- source: The file or directory you want to copy
- destination: Where you want to copy the file
- options: Additional flags to modify behavior
To use SCP, open up your terminal of choice. SCP is available on all systems that have the OpenSSH suite installed, which is typically installed as standard
Transferring Files TO Your VM
To copy files from your local machine to your virtual machine:
Single File Transfer
Multiple Files Transfer
Directory Transfer (Recursive)
Transferring Files FROM Your VM
To copy files from your virtual machine to your local machine:
Download Single File
Download Directory
Transferring Files using SSH
Using the above methods will request a password once the command is sent. For a more secure connection use your private key with the -i flag (see below).
Transferring data to your local using SSH Key Authentication
Useful SCP Options
Common flags that enhance SCP functionality:
-r
: Recursive - Copy directories and their contents-p
: Preserve - Maintain file permissions and timestamps-v
: Verbose - Show detailed progress information-P port
: Port - Specify a custom SSH port (if not 22)-i keyfile
: Identity - Use a specific SSH private key-C
: Compression - Enable compression to speed up transfers
Advanced Example with Options
Real-World Examples
Uploading Research Data using SSH key Authentication
Important: Always double-check your file paths and VM IP addresses before running SCP commands. Incorrect paths can result in files being placed in unexpected locations or transfer failures.
Troubleshooting Common Issue
- Permission denied: Ensure you have read/write permissions on both source and destination
- Connection refused: Verify SSH is running on port 22 (or your custom port)
- Host key verification failed: Remove old host keys with
ssh-keygen -R hostname
- Authentication failure: Check your username, password, or SSH key configuration
- Network timeout: Verify the VM is running and network security groups allow SSH traffic
Pro Tip: For large file transfers, consider using the -C
compression flag to reduce transfer time, especially over slower network connections.