Securely Transfer Files Using SCP

The complete guide to scp

SCP (Secure Copy) is a command-line tool that enables you to transfer files and directories between two locations in a secure manner.

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.

When using SCP to transfer data, both the files and password are encrypted, which prevents any snooping on the traffic from obtaining any sensitive information.

This tutorial demonstrates how to use the scp command with practical examples and in-depth explanations of the most common SCP options.

SCP syntax

Before learning how to use the scp command, let’s review the basic syntax.

The syntax of the scp command is as follows:

scp [OPTION] [user@SOURCE_HOST:]file1 [user@DESTINATION_HOST:]file2
  • OPTION – SCP options such as cipher, SSH configuration, SSH port, limit, recursive copy, and so on.
  • [user@SOURCE_HOST:]file1 – The source file.
  • [user@DESTINATION_HOST:]file2 – The destination file.

Local files should be specified using an absolute or relative path, while remote file names should include a user and host specification.

SCP offers numerous options that control every aspect of its behavior. The most commonly used options are:

  • -P – Specifies the SSH port of the remote host.
  • -p – Preserves the modification and access times of files.
  • -q – Use this option to suppress the progress meter and non-error messages.
  • -C – This option forces scp to compress data as it’s sent to the destination machine.
  • -r – This option instructs scp to copy directories recursively.

The scp command relies on SSH for data transfer, so it requires either an SSH key or password to authenticate with remote systems.

The colon (:) is the character that scp uses to differentiate between local and remote locations.

To copy files, you need at least read permission on the source file and write permission on the target system.

Be cautious when copying files that have the same name and location on both systems because scp will overwrite files without giving a warning.

Copy files from a local system to a remote system using SCP

To copy a file from a local system to a remote one, use the following command:

scp file.txt remote_username@

In this example, file.txt is the name of the file you want to copy, remote_username is the username on the remote server, and is the IP address of the server. /remote/directory is the path to the directory where you want to copy the file. 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’s password, and the transfer process will commence.

If you exclude the filename from the destination location, the file will be copied with its original name. To save the file under a different name, you must specify the new filename:

scp file.txt remote_username@192.168.2 1:/remote/directory/newfilename.txt

If the SSH service on the remote host is listening on a port other than the default port 22 (2322 for example), you can specify the port using the -P option:

scp -P 2322 file.txt remote_username@

Copying a directory is similar to copying files, but with the added step of using the -r flag for recursive copying.

To copy a directory from a local system to a remote one, use the -r option:

scp -r /local/directory remote_username@

Copy files from a remote system to a local system using SCP

To copy a file from a remote system to a local one, use the remote location as the source and the local location as the destination.

For instance, to copy a file named file.txt from a remote server with IP address, use the following command:

scp remote_username@ /local/directory

Securely transfer files between two remote systems using SCP

Unlike rsync, which requires logging into one of the servers to transfer files between remote machines, you can use scp to do so directly.

Use the following command to copy the file /files/file.txt from the remote host to the directory /files on the remote host


You will need to enter the passwords for both remote accounts when prompted. The data will be transferred directly between the remote hosts.

If you want to route the traffic through the machine where the command is executed, use the -3 option:

scp -3