How to Use SCP Command to Securely Transfer Files

Updated on

5 min read

Use SCP Command to Securely Transfer Files

SCP (secure copy) is a command-line utility that allows you to securely copy files and directories between two locations.

When transferring data with scp, both the files and password are encrypted so that anyone snooping on the traffic doesn’t get anything sensitive. SCP uses the SSH protocol for both authentication and encryption.

This tutorial will demonstrate how to use the scp command through practical examples and detailed explanations of the most commonly used scp options.

SCP Command Syntax

Before going into how to use the scp command, let’s start by reviewing the basic syntax.

The scp command syntax takes the following form:

scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
  • OPTION - scp options such as cipher, ssh configuration, ssh port, limit, recursive copy …etc.
  • `` - .
  • [user@]SRC_HOST:]file1 - Path to the source file. The name of the user on the source machine and the hostname (or the IP address) of the source machine are used when the file is located on a remote machine.
  • [user@]DEST_HOST:]:file2 - Path to the destination file. The name of the user on the destination machine and the hostname (or the IP address) of the destination machine are used when the file is located on a remote machine.

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

scp provides a number of options that control every aspect of its behavior. The most widely used options are:

  • -P - Specifies the remote host ssh port.
  • -p - Preserves file modification and access times.
  • -q - Use this option if you want to suppress the progress meter and non-error messages.
  • -C - This option forces scp to compress the data as it is sent to the destination machine.
  • -r - This option tells scp to copy directories recursively.

Before you Begin

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

The colon (:) is how scp distinguishes between local and remote locations.

To be able to copy files, you must have at least read permissions on the source file and write permission on the target system.

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

When transferring large files, it is recommended to run the scp command inside a screen or tmux session.

Copy Files and Directories Between Two Systems with scp

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.

Copy a Local File to a Remote System with the scp Command

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

scp file.txt remote_username@

Where file.txt is the name of the file we want to copy, remote_username is the user on the remote server, is the server IP address. The /remote/directory is the path to the directory you want to copy the file to. 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.

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

Omitting 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@

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

scp -P 2322 file.txt remote_username@

To copy a directory and all its files, invoke the scp command with the -r flag, which recursively copies the entire directory and its contents:

scp -r /local/directory remote_username@

When using wildcards such as * or ? to match specific files or directories, to avoid shell expansion, you must enclose the path that includes wildcard characters in quotes (" ") or single quotes (' '). In the following example, we are copying all the .txt files from the local Projects directory to the Projects directory on the remote server:

scp "~Projects/*.txt" remote_username@

If you want to preserve the file metadata, which, among other information, includes ownership and creation date of that file, pass the -p option. For example, to copy file.txt to the remote directory and preserve the metadata, enter:

scp -p file.txt remote_username@

Copy a Remote File to a Local System using the scp Command

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

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

scp remote_username@ /local/directory

If you haven’t set a passwordless SSH login to the remote machine, you will be asked to enter the user password.

Copy a File Between Two Remote Systems using the scp Command

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

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


You will be prompted to enter the passwords for both remote accounts. The data will be transferred directly from one remote host to the other.

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

scp -3


If you’re looking for a fast, reliable, and secure way to copy files and directories between your Linux systems, scp is the right tool for the job.

You may also want to set up an SSH key-based authentication and connect to your Linux servers without entering a password.

If you regularly connect to the same systems, you can simplify your workflow by defining all of your connections in the SSH config file .