scp – Copy files between local host and remote host in an encrypted way

SCP stands for Secure Copy Protocol. It is a command-line utility used for copying files between local and remote hosts in a secure and encrypted way. SCP uses SSH (Secure Shell) protocol for data transfer and provides the same level of security and authentication as SSH. SCP is a widely used tool for securely transferring files between Linux servers.

Overview

The basic syntax of the SCP command is as follows:

scp [options] source_file destination_file

Where source_file specifies the file to be copied, and destination_file specifies the location where the file will be copied to. The source_file and destination_file can be local or remote files, and can be specified using absolute or relative paths.

To copy a file from a local host to a remote host, use the following syntax:

scp source_file remote_username@remote_host:destination_folder

Where source_file is the file to be copied, remote_username is the username on the remote host, remote_host is the IP address or hostname of the remote host, and destination_folder is the folder on the remote host where the file will be copied to.

To copy a file from a remote host to a local host, use the following syntax:

scp remote_username@remote_host:source_file destination_folder

Where remote_username is the username on the remote host, remote_host is the IP address or hostname of the remote host, source_file is the file to be copied, and destination_folder is the folder on the local host where the file will be copied to.

Examples

To copy a file named file.txt from the local host to a remote host with IP address 192.168.1.100 and username user1, and save it in the ~/Documents folder on the remote host, use the following command:

scp file.txt user1@192.168.1.100:~/Documents

To copy a file named file.txt from a remote host with IP address 192.168.1.100 and username user1, and save it in the /home/user/Documents folder on the local host, use the following command:

scp user1@192.168.1.100:file.txt /home/user/Documents

Options

The following table lists the available options for the SCP command:

Option Description
-P Specifies the port number to use for the SSH connection. Default is port 22.
-r Recursively copies entire directories.
-p Preserves the modification times, access times, and modes from the original file.
-q Suppresses progress meter and non-error messages.
-v Enables verbose mode. Displays debugging messages.

Troubleshooting tips

  • If you receive a “Permission denied” error message, make sure that you have the necessary permissions to access the file or folder you are trying to copy.
  • If you receive a “Host key verification failed” error message, it means that the remote host’s SSH key is not recognized. You can resolve this issue by adding the remote host’s SSH key to your local host’s known_hosts file.
  • If you receive a “Connection timed out” error message, it means that the remote host is not reachable. Check the network connection and make sure that the remote host is up and running.

Notes

  • SCP is a secure and reliable way to transfer files between Linux hosts, but it is not suitable for transferring large amounts of data or for transferring files frequently. In such cases, it is recommended to use other file transfer protocols such as FTP or SFTP.