Copying Files Securely using Secure Copy Protocol (SCP)

Secure Copy Protocol allows us to securely transfer files between computers on a network. SCP uses ssh for data transfer, authentication and security. SCP will ask for passwords or passphrases if it is needed for authentication. There can be a source and a target computer which can be specified by a local pathname, a remote host with an optional specified path, or a URI.

The general syntax is :

  • [options] /path/to/content user@remotehost:/path/to/content
  • [options] user@remotehost1:[/path/to/source/content] user@remotehost2:[/path/to/destination/content]

SCP Options

SCP options can be used to modify the behavior of SCP. This potentially specifiying an identity file, limiting bandwidth or utilizing a custom ssh port.

OptionDescription
-3Copies between two remote hosts are transferred through the local host. Without this option the data is copied directly between the two remote hosts. This option will disable the progress indicator.
-4Forces scp to use IPv4 addresses only.
-6Forces scp to use IPv6 addresses only.
-BSelects batch mode (prevents asking for passwords or passphrases).
-CCompression enable. Passes the -C flag to ssh to enable compression
-c cipherSelects the cipher to use for encrypting the data transfer. This option is directly passed to ssh.
-F ssh_configSpecifies an alternative per user configuration file for ssh. This option is directly passed to ssh.
-i identity_fileSelects the file from which the private key for public key authentication is read. This option is directly passed to ssh.
-J destinationConnect to the target host by first making an scp connection to the jump host described by destination and then establishing a TCP forwarding to the ultimate destination from there. Multiple jump hops may be specified separated by comma characters. This is a shortcut to specify a ProxyJump configuration di‐rective. This option is directly passed to ssh(1).
-l limitLimits the used bandwidth, specified in Kbits/s. (1 megabit is equal to 1024 Kilobits)
-o ssh_optionUsed to pass options to ssh. This can be used for specifying options for which there is no seperate scp command-line flag.
-P portSpecifies the port to connect to on the remote host.
-pPreserves modification times, access times, and modes from the original file.
-qQuet mode, disables the progress meter in addition to warning and diagnostic messages received from ssh.
-rRecursively copy entire directories. SCP follows symbolic links encountered when it traverses the directory tree.
-S programSpecify the name of the program to use for the encrypted connection. The program should understand ssh options.
-TDisable strict filename checking. Normally when files are copied from a remote host to a local directory, scp checks that the received filenames match those requested on the command-line to prevent the remote end from sending unexpected or unwanted files.

Due to differences in operating systems and shells when it comes to interpreting filename wilcards files that are wanted might be rejected due to the checks.

This option will disable the checks at the cost of trusting that the server will not send unexpected filenames.
-vVerbose mode. Causes scp and ssh to print debuggin messages about progress. This can aid in debugging issues related to connections, authentication, and configuration.

Practical examples

Between local host and remote host

In this example I will be copying files from a Windows computer to a Linode cloud instance. These are files that are of great importance to me and they contain highly confidential information.

This is my folder that I will be copying from local host to remote host.

From local host to remote host

From remote host to my local host

Between two remote hosts

I am transferring a pre-uploaded file from a linode instance in Texas to an instance in the Netherlands. In my experience, for this to work ssh keys should be generated and copied to each of the instances. In this example the data is copied directly between the two remote hosts.

Texas source machine with example.bin file
Local Windows machine command
Netherlands destination machine result

Some people may argue that rsync is a better than scp, and to that I have to say it depends on the environment. Try to get someone who does not use Linux on the daily to try it. They will need to run around installing Windows Subsystem for Linux before they can even think of starting file transfers.

For more information with regards to scp, please consult the the man pages for scp.


Posted

in

,

by

Tags: