Frequently Asked Question

How to transfer files between local and remote systems using scp?
Last Updated 7 months ago

1. Introduction

Sometimes it may be necessary to transfer files between your local machine and the virtual machine. This guide shows how to transfer files for a Linux local machine by using the scp command. This guide uses following terms interchangeably:

  • local machine - local system -> refers to the device being used
  • remote machine - remote system - remote host -> virtual machine

2. Secure Copy - SCP

SCP (secure copy) is a command-line utility that allows you to securely copy files and directories between two locations.

With scp, you can copy a file or directory:

  • From your local system to a remote system.
  • From a remote system to your local system.
  • Between two remote systems from your local system.

Basic syntax for scp is:

scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
  • OPTION - scp options such as cipher, ssh configuration, ssh port, limit, recursive copy …etc.
  • [user@]SRC_HOST:]file1 - Source file.
  • [user@]DEST_HOST:]file2 - Destination file.

Local files should be specified using an absolute or relative path while remote file names should include a user and host specification.

scp provides a number of options that control every aspect of its behavior. The most widely used options are:

  • -P Specifies the remote host ssh port.
  • -p Preserves files modification and access times.
  • -q Use this option if you want to suppress the progress meter and non-error messages.
  • -C. This option will force scp to compresses the data as it is sent to the destination machine.
  • -r This option will tell scp to copy directories recursively.

The scp command relies on ssh for data transfer, so it requires an ssh key or password to authenticate on the remote systems.
How to create SSH keys?
How to install OpenSSH on Windows 10?

The colon (:) is how scp distinguish between local and remote locations.

To be able to copy files you must have at least read permissions on the source file and write permission on the target system.

Be careful when copying files that share the same name and location on both systems, scp will overwrite files without warning.

When transferring large files, it is recommended to run the scp command inside a screen or tmux session.

2.1 Copy from local machine to remote machine

Example:

scp /local/directory ubuntu@10.10.0.2:/remote/file.txt

2.2 Copy from remote machine to local machine

Example:

scp ubuntu@10.10.0.2:/remote/file.txt /local/directory

2.3 Copy from remote machine to another remote machine

Example:

scp ubuntu@10.10.0.2:/remote/file.txt ubuntu@10.10.0.3:/remote/directory

Loading ...