Virtual Machines

SCP File Transfer Data Transfer

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

$ scp /path/to/local/file.txt username@vm-ip:/remote/path/ # Copies file.txt to the remote VM

Multiple Files Transfer

$ scp file1.txt file2.txt file3.txt username@vm-ip:/remote/path/ # Copies multiple files in one command

Directory Transfer (Recursive)

$ scp -r /path/to/local/directory username@vm-ip:/remote/path/ # -r flag enables recursive copying of directories

Transferring Files FROM Your VM

To copy files from your virtual machine to your local machine:

Download Single File

$ scp username@vm-ip:/remote/path/file.txt /local/destination/ # Downloads file.txt from the VM to your local machine

Download Directory

$ scp -r username@vm-ip:/remote/directory /local/destination/ # Downloads entire directory structure

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

$ scp -i ~/.ssh/my-key.pem username@vm-ip:/remote/directory /local/destination/ # Remember to store your SSH keys in the ~/.ssh folder

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

$ scp -r -p -v -C -i ~/.ssh/my-key.pem /local/data/ username@vm-ip:/backup/ # Recursive, preserve permissions, verbose, compressed transfer using specific key

Real-World Examples

Uploading Research Data using SSH key Authentication

$ scp -r -i ~/.ssh/azure-vm-key.pem ~/research_data/ [email protected]:/home/azureuser/data/ # Upload your local research_data directory to Azure VM

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.