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.
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
scpto compress data as it’s sent to the destination machine.
-r– This option instructs
scpto copy directories recursively.
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 firstname.lastname@example.org:/remote/directory
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
192.168.2.1 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 email@example.com 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
scp -P 2322 file.txt firstname.lastname@example.org:/remote/directory
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
scp -r /local/directory email@example.com:/remote/directory
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
192.168.2.1, use the following command:
scp firstname.lastname@example.org:/remote/file.txt /local/directory
Securely transfer files between two remote systems using SCP
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
host1.com to the directory
/files on the remote host
scp email@example.com:/files/file.txt firstname.lastname@example.org:/files
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
scp -3 email@example.com:/files/file.txt firstname.lastname@example.org:/files