Introduction
SCP (Secure Copy Protocol) is a network protocol used to securely copy files and folders between Linux (Unix) systems on a network. It utilizes the scp
command line utility, which is a more secure alternative to the traditional cp
(copy) command.
SCP ensures the protection of your data during transfer by encrypting both the files and passwords over an SSH (Secure Shell) connection. This means that even if the traffic is intercepted, the information remains encrypted and secure.
This guide demonstrates how to use the SCP command to copy files and provides 13 practical examples.
Prerequisites
- A secure shell login on the server.
- (optional) Root access on both the client and server.
- A secure shell login on the server system.
SCP Command Syntax
The syntax for using the scp
command is:
scp [option] [user_name@source_host:path/to/source/file] [user_name@target_host:target/path]
If you omit:
- The
user_name
of the host (or target), the command defaults to the current user. - The
path/to/source
(or thetarget/path
), the program looks for (or copies) the file locally.
When working with remote files, always specify the user and host details.
Use an account with read access to the file(s) on the source system. Additionally, ensure you use an account with write access to the directory where the file(s) will be saved on the destination system.
Note: The scp
command does not verify the destination location before writing. Any files at the destination with the same name will be overwritten without notification.
SCP Command Options
You can enhance and accelerate the scp
command by adding various options. These options are specified as attributes immediately following the scp
command.
Each option is available in a short, single-character form as well as a longer, more descriptive equivalent.
Option | Description |
---|---|
-1 | Use protocol 1. |
-2 | Use protocol 2. |
-4 | Only use Ipv4 addresses. |
-6 | Only use IPv6 addresses. |
-B | Run in batch mode and disable all queries for user input. |
-b [buffer_size] | Specify the buffer size used for data transfer. If not specified, uses the default 32768 bytes. |
-C | Enable compression. |
-c [cipher] | Select the cipher for data encryption. If not specified, SCP uses the default – 'AnyStdCipher' . |
-D [debug_level] | Set the debug level (1, 2, 3, or 99). |
-d | Copy the file only if the destination directory already exists. |
-F [file] | Specify an alternative configuration file for SSH. |
-h | Show a list of command options. |
-i [file] | Specify the file from which to read the identity for public key authentication. |
-l [limit] | Limit the bandwidth (specify the limit in Kbit/s). |
-o ssh_option | Set options to SSH in ssh_config format. |
-P [port] | Specify the port to which to connect. If not specified, SCP uses port 22. |
-q | Run SCP in quiet mode. |
-Q | Disable file transfer statistics. |
-r | Copy recursively. |
-S [program] | Use a specified program for encryption connection. |
-u | Delete the source file once the copy is complete. |
-v | Enable verbose mode, which sets the debug level to 2. |
SCP Command Examples
Use the SCP command in the following scenarios:
- Copying files from a local host to a remote host.
- Copying files from a remote host to a local host.
- Copying files between two remote servers.
To gain a better understanding of this useful utility, explore the following SCP command examples.
Copy a File from Local to Remote Server
In the example below, we copy a sample file from a local host to a remote server:
scp Desktop/sample_example.txt root@136.183.142.28:/home/remote_dir
The command includes the following information:
Desktop/sample_example.txt
– The name of the file being copied and its location.root@136.183.142.27
– The username and IP address of the remote server./home/remote_dir
– The location where to store the copied file.
Copy File from Remote Server to Local Host
To copy a sample file from a remote host to a local host, run this command:
scp 147.182.143.27:/home/remote_dir/sample_example.txt home/Desktop
The information provided is
root@147.182.143.27
– The username and IP address of the remote server from where the file is currently located./home/remote_dir/sample_example.txt
– The name of the file being copied and its location.home/Desktop
– The location where to store the copied file.
Copy File from One Remote Server to Another
Next, copy a file from one remote server to another remote server using the scp
command in the following format:
scp root@147.182.143.27:/home/remote_dir/sample_example.txt sofija@146.153.129.25:home/Desktop
The command above specifies:
root@147.182.143.27
: Indicates the username and IP address of the remote server where the file is currently located./home/remote_dir/sample_example.txt
: Specifies the name of the file being copied and its current location.sofija@146.153.129.25
: Indicates the username and IP address of the remote server where we want to copy the file./home/Desktop
: Specifies the location where the copied file will be stored on the remote server.
Copy Multiple Files with SCP
SCP allows you to copy multiple files in a single command. For example, the command below copies two files from a local host to a remote server:
scp example/sample1.txt example/sample2.txt root@147.182.143.27:/home/remote_dir
It contains the following details:
example/sample1.txt
: Specifies the name and location of the first file being copied.example/sample2.txt
: Specifies the name and location of the second file being copied.root@147.182.143.27
: Indicates the username and IP address of the remote server receiving the files./home/remote_dir
: Specifies the location where the copied files will be stored on the remote server.
Copy Directory from Local Host to Remote Server Recursively
In addition to files, scp
can securely copy directories to or from remote servers. The following command demonstrates how to recursively copy a sample directory to a remote server:
scp -r example root@147.182.143.27:/home/remote_dir
The command consists of:
-r
: This option enables recursive copying of the directory.example
: This is the name of the directory being copied from the local server.root@147.182.143.27
: Specifies the username and IP address of the remote server receiving the folder./home/remote_dir
: Indicates the location where the copied directory will be stored on the remote server.
Copy File with SCP Using Specific Port
By default, SCP uses port 22 for SSH connections. However, if a remote system is configured to listen to SSH requests on a different port, you can use the -P
switch to specify that port.
For instance, the following command copies a file from a local system to a remote server using port 2222:
scp -P 2222 Desktop/sample_example.txt root@147.182.143.27:/home/remote_dir
The components of the command above are:
-P 2222
: Instructs the command to use port 2222.Desktop/sample_example.txt
: Specifies the file name you want to copy and its location.root@147.182.143.27
: Indicates the username and IP address of the remote server receiving the file./home/remote_dir
: Specifies the location where to store the copied file on the remote server.
Copy File with SCP in Quiet Mode
To run an scp
command in quiet mode, you can disable the progress meter and suppress non-error messages from displaying in the output. To achieve this, include the -q
option:
scp -q Desktop/sample_example.txt root@136.183.142:/home/remote_dir
Copy File with SCP in Verbose Mode
You can activate verbose mode in scp
by including the -v
option, which sets the debug level to 2. This option prints debugging information in the output to assist you in troubleshooting.
To enable verbose mode, add the -v
option after scp
, as demonstrated in the example below:
scp -v Desktop/sample_example.txt root@147.182.143.27:/home/remote_dir
Copy File with SCP and Limit Bandwidth
Another helpful option is limiting the bandwidth used by the scp
command by adding the -l
parameter. This is particularly useful when copying large files to prevent SCP from consuming all available bandwidth.
When limiting bandwidth, specify the value in kilobits per second (kbps). Since 1 byte equals 8 bits, if you want to limit the bandwidth for SCP to 100 KB/s, the value for the -l
option would be 800 (100 x 8), as shown in the command below:
scp -l 800 Desktop/sample_example.txt root@147.182.143.27:/home/remote_dir
Copy File with SCP Faster
To speed up the file transfer from one server to another, use the -C
option to compress the file during transfer. Once the file reaches its destination, it will decompress to its original size.
scp -C Desktop/sample_example.txt root@147.182.143.27:/home/remote_dir
Copy File with SCP Using Specific Cipher
By default, SCP uses AES-128 to encrypt files. However, you can use the -c
option to change the cipher SCP uses for encryption.
For example, to enhance security, you can switch to 3DES encryption with the following command:
scp -c 3des Desktop/sample_example.txt root@147.182.143.27:/home/remote_dir
Copy File with SCP Using IPv4 or IPv6
You can force SCP to use only IPv4 or IPv6 by adding the -4
or -6
option.
To copy a sample file from a local server to a remote host using only IPv6, enter:
scp -6 Desktop/sample_example.txt root@147.182.143.27:/home/remote_dir
Copy File with SCP and Preserve File Attributes
To copy a file using SCP while preserving file attributes such as modification and access times, modes, and permissions, use the -p
option:
scp -p Desktop/sample_example.txt root@147.182.143.27:/home/remote_dir
Conclusion
In this guide, you have learned about the scp
command and how to use it to securely transmit files. This tool is particularly valuable as a replacement for FTP, which is insecure by default.
The Secure Copy Protocol (SCP) integrates seamlessly with regular command-line and SSH functionality, providing a unified command set for managing files between Linux machines.