How to copy file from remote server to remote server or local machine using 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.

SCP copies files between hosts on a network. It uses SSH for data transfer, and uses the same authentication and provides the same security as SSH. SCP will ask for passwords or passphrases if they are needed for authentication.

When copying a source file to a target file which already exists, SCP will replace the contents of the target file. If the target file does not yet exist, an empty file with the target file name is created, then filled with the source file contents.

SCP Command Syntax

The scp command syntax take the following form:

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.

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

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

If you are on the computer from which you want to send a file to a remote computer:

scp /file/to/send username@remote:/where/to/put

Here the remote can be a domain name or an IP address.

On the other hand, if you are on the computer wanting to receive a file from a remote computer:

scp username@remote:/file/to/send /where/to/put

scp can also send files between two remote hosts:

scp username@remote_1:/file/to/send username@remote_2:/where/to/put

So the basic syntax is:

scp username@source:/location/to/file username@destination:/where/to/put

For more details read Linux man pagescp.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *