×

Search anything:

scp command in Linux

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

scp is useful for, copying files from a local system to a remote system and vice versa or copying files between two remote systems. It works same as cp command but over networks and uses ssh for encryption and security..

Table of contents.

  1. Introduction.
  2. Syntax.
  3. Commands.
  4. Summary.
  5. References.

Introduction.

The scp command is useful for copying files and directories from remote systems securely. It is similar to the cp command that copies files except that it does this to and from other systems connected via a network.
For security purposes it works through SSH protocol therefore both systems involved must have SSH configured.

SSH provides a secure and encrypted tunnel between the two systems.
Since SSH uses authentication, details such as a password is required.

Syntax.

scp [OPTION] [user@]src:]file1 [user@]dest:]file2
  • [user@]src:]file1 represents the source file.
  • [user@]dest:]file2 represents the destination file.
  • Commonly used options may include;
  • -r meaning recursively copy entire directories.
  • -C to enable compression.
  • -l to limit bandwidth.
  • -o for passing ssh options.
  • -P to specify the port to connect to on the remote system.
  • -v to print debugging messages about the progress.
  • -q to disable the progress meter.
  • -p for preserving modification times, access times and modes from the original file.
  • -4 for using IPv4.
  • -6 for using IPv6.
  • -c to specify a cipher.

Commands.

To copy a file image.iso from local home directory images to a directory remoteImages in a remote machine remote.com, we write,

scp ~/image.iso user@remote.com:~/remoteImages

When we execute this command, we are prompted for a password for remote.com.

We can also use an ip address as follows,

scp image.iso user@169.173.78.12:~/remoteImages

We can also specify the name of the file after copying it in the remote system.

scp image.iso user@remote.com:~/remoteImages/newName.iso

To copy multiple files, we write,

scp user@remote.com:~/images/*.iso ~/remoteImages

The above command copies all files with .iso extension from the local images directory to the remote remoteImages directory.

To copy a directory recursively, we use the -r option as follows,

scp -r images user@remote.com:~/remoteImages

The above command recursively copies the directory images and its subdirectories and files to the remote system.

We can also copy a file from the remote system to our local system as follows,

scp user@remote.com:~/remoteImages/remoteImage.iso ~/images

The above command copies remoteImage.iso remote file into a local directory images.

To specify a port, assuming ssh on the remote system is running n a different port we write,

scp -P 23 image.iso user@remote.com:~/remoteImages

We have used the P option to change the default ssh port 22 to 23 which is running on the remote system.

We can also copy files between remote systems as follows,

scp user@remote1.com:~/remote1Images/remote1Image.iso user2@remote2.com~:~/remote2Images

The above command copies remote1Image.iso from a remote host remote1.com to a remote2Images directory in another remote host remote2.com.

The -C option is used to compress the file being copied and therefore the operation will be faster,

scp -C user@remote.com:~/remoteImages/remoteImage.iso ~/images

To preserve file attributes such as modification and access times, permissions etc, we use the -p option as follows,

scp -p user@remote.com:~/remoteImages/remoteImage.iso ~/images

SCP uses AES-128 cipher for encryption, we can use the -c option to change this as follows,

scp -c 3des user@remote.com:~/remoteImages/remoteImage.iso ~/images

From the above command we have switched to 3des encryption.

We can also limit bandwidth using the -l option as follows,

scp -l 800 user@remote.com:~/remoteImages/remoteImage.iso ~/images

The above command limits bandwidth to 100 kbs.

Summary.

Unlike cp command scp copies over computer networks.
It uses SSH to encrypt data and provides a secure tunnel between two systems, therefore files cannot be sniffed.

A password authentication is required although RSA authentication can be set up which will bypass the password prompt while increasing security.
When files are copied and they already exist on the target system, the original files are replaced.

To be able to copy files one must have appropriate permissions.
On a windows system WinSCP is used to accomplish this.
SCP is a replacement of FTP which is insecure.

References.

  1. Execute the command man scp for its manual page or scp --help
scp command in Linux
Share this