SCP Command on Linux Systems.

SCP (Secure Copy Protocol) is a command-line tool in Linux and Unix-like (UN*X) systems that is used to transfer/ copy files and directories across the systems in a secure way over the network.

SCP Command on Linux Systems.

The scp command uses SSH to transfer data, so it requires either key or password-based authentication. Unlike RCP or FTP, scp encrypts both the file and any passwords exchanged so that anyone snooping on the network cannot get anything sensitive.
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 Command Syntax

The scp command syntax goes as follows:

scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
  • OPTION - SCP options such as cipher, ssh configuration, ssh port, limit, recursive copy …etc.
  • The colon (:) is how scp distinguish between local and remote locations.
  • [user@]SRC_HOST:]file1 - Source file.
  • [user@]DEST_HOST:]file2 - Destination file

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

Options:

  • -p - Preserves files modification and access times.
  • -q - Use this option if you want to suppress the progress meter and non-error messages.
  • -C - By default SCP using "AES-128" to encrypt files. If you want to use another cipher feel free.
  • -r - This option tells scp to copy directories recursively.
  • -P - Specifies the remote host ssh port.
  • -v - Print debug information. It can help you debug connection, authentication, and configuration problems.
  • -C - This parameter will compress your files on the go. When the file arrives at the destination server, it will return to its original size.
  • -l - This option will limit the bandwidth to use. It will be useful if you do an automation script to copy a lot of file, but you don’t want the bandwidth to be drained by the SCP process.
  • -3 - Copies 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 also disables the progress meter.

Copy a Local File to a Remote System

To copy a file from a local to a remote system run the following command:

$ scp file.txt remote_username@10.10.0.9:/remote/directory

Where file.txt is the name of the file we want to copy, remote_username is the user on the remote server, 10.10.0.9 is the server IP address. The /remote/directory is the path to the directory you want to copy the file to.
For any reason; if you don’t specify a remote directory, the file will be copied to the remote user's home directory.

You will be prompted to enter the user password, and the transfer process will start. Below is the output:

Output
remote_username@10.10.0.9's password:
file.txt                             100%    0     0.0KB/s   00:00

*Excluding the filename from the destination location copies the file with the original name. If you want to save the file under a different name, you need to specify the new file name:

scp file.txt remote_username@10.10.0.9:/remote/directory/newfilename.txt

SCP to a different port

If SSH on the remote host is listening on a port other than the default 22 then you must specify the port using the -P argument:

scp -P 12202 file.txt remote_username@10.10.0.9:/remote/directory

SCP Recursively

To achieve this, we'll use the -r flag, which tells scp to recursively copy all of the directory's contents to a remote system.

scp -r /local/directory remote_username@10.10.0.9:/remote/directory

Copy a Remote File to a Local System

For example to copy a file named file.txt from a remote server with IP 10.10.0.9 run the following command:

scp remote_username@10.10.0.9:/remote/file.txt /local/directory

Copy a File Between Two Remote Systems

Note that, unlike rsync , when using scp you don’t have to log in to one of the servers to transfer files from one to another remote machine.

The following command will copy the file /files/file.txt from the remote host host1.com to the directory /files on the remote host host2.com.

scp user1@host1.com:/files/file.txt user2@host2.com:/files

If both hosts use different ports on SSH (i.e. 12202 and 2021), here is how to specify these ports to the SCP command.

scp://user1@host1:port 12202/path/to/file1 scp://user2@host2:port 2021/path/to/file2

Once the connection is initiated, you will be asked for the passwords for both remote destinations. The data will be transferred directly from one remote host to the other.

Rerouting traffic

To route the traffic through the local host (machine on which the command is issued), use the -3 option:

scp -3 user1@host1.com:/files/file.txt user2@host2.com:/files

That's all there is to it. As you can see, using the scp command ensures authenticity and confidentiality when transferring/ copying data from a remote host to your local host and vice versa. That’s what Linux is all about, invest your time in understanding some basics.