SCP is closely related to the SSH protocol and was indeed born from the same idea.

SCP makes sure that data remains confidential & untampered during transit as an extension of the SSH protocol.

First, let’s understand how this SCP works, and then we’ll see the scp commands.

What is SCP?

A computer with two Secure Copy Protocol monitors and a globe on it.

SCP stands for Secure Copy Protocol.

It is a secure & efficient method for copying files and directories between local and remote hosts or between two remote hosts.

It’s widely used for transferring data securely over a network. SCP relies on SSH (Secure Shell) to establish a secure connection and encrypt the data during transit.

Whether you need to move a single file or an entire directory, SCP provides a reliable solution for remote file transfers.

How SCP Works?

Two laptops use Secure Copy Protocol to transfer folders between them.

SCP is built on top of the SSH protocol – which is a cryptographic network protocol for secure remote login & command execution. It uses the same security mechanisms as SSH, including encryption and authentication, to protect data during transmission.

When using SCP, a user initiates a copy command on their local machine that specifies the source file and the destination location on a remote machine. SCP establishes an SSH connection to the remote system and then securely copies the files from the source to the destination.

When to Use SCP?

A computer with a secure file transfer protocol (SFTP) and a gear on it.

SCP is particularly useful in the following scenarios.

Remote Server Backup

SCP allows you to back up files from a remote server to your local machine or vice versa, which ensures data redundancy & disaster recovery.

Deployment of Applications

It also simplifies the process by securely copying necessary files to each target system when deploying applications across multiple servers,

Transferring Large Files

SCP offers a trustworthy and secure alternate method of transmitting data for large files that might be too large to send as email attachments.

Secure File Sharing

SCP makes sure the data is kept encrypted throughout transit while sending confidential files to other people.

Remote Collaboration

SCP can be used to share files securely with collaborators or team members in different locations.

SCP Command Syntax 

Before getting started with how to use the SCP command, let’s start by understanding the basic syntax.

The SCP command syntax looks like this:

scp [options] [source] [destination]

Explanation of syntax

Options

These are optional flags that modify the behavior of the SCP command. You can use various options to customize the transfer process. Here are some common options:

-r: Recursively copies directories & their contents.
-p: Preserve the permissions, timestamps, and modes of the original file.
-P: Specifies the port number for the SSH connection.
-i: Specifies the private key file for SSH authentication.
-C: Use compression to speed up data transfer.
-l: Limit the bandwidth used for the transfer (in Kbit/s).
-v: Verbose mode – displays debugging messages during transfer.
-o: Passes an option to the SSH client. It can be used to disable strict host key checking.

Source

The source specifies the file or directory you want to copy. It can be a local path or a remote path – depending on the context.

Destination

The destination specifies where you want to copy the source. Like the source, it can also be a local path or a remote path.

Command Examples

#1. Copy a File from Local to Remote server

scp /path/to/local/file.txt user@remotehost:/path/to/destination/

/path/to/local/file.txt: This is the source file you want to copy from your local machine.

user@remotehost: Replace the user with the remote user’s name and the remote host with the hostname or IP address of the remote server.

/path/to/destination/: This is the destination path on the remote server where the file will be copied.

Example: In my case, I have chosen one remote server for transferring the file.

Remote Host username – abhishekvarma

Remote Host Address – 192.168.29.70

scp /home/vboxuser/Desktop/sample.zip abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop

While connecting to a remote machine, it asks for the password. Here I have selected a zip file to copy.

Output

Ubuntu command line - secure copy protocol for transferring files.

And the output will look like this if there is no error in connecting to a remote server.

sample.zip                                    100%   20KB  717.9KB/s   00:00

#2. Copy a File from the Remote to the Local machine

scp user@remotehost:/path/to/remote/file.txt /path/to/local/destination/

Same as above, replace the user and remote host address.

Example

scp abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop/sample.zip /home/vboxuser/Desktop

Output

A screenshot of a computer screen displaying the Secure Copy Protocol.

This is just the sample output in my case. Timestamp varies depending on connection speed.

sample.zip                                    100%   20KB  51.2KB/s   00:00

#3. Copy a Directory Recursively

scp user@remotehost:/path/to/remote/file.txt /path/to/local/destination/

Here the ‘-r’ flag tells SCP to copy directories recursively (including subdirectories and their contents).

Example

scp -r /home/vboxuser/Desktop abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop

Here, I mentioned the whole directory to copy the files.

Output

A screenshot of a file transferred securely using the Secure Copy Protocol (SCP) displayed on a computer screen.
sample.zip                                         100%   20KB    1.0MB/s   00:00
Screenshot from 2023-08-13 21-45-23.png            100%   27KB    1.5MB/s   00:00
.swp                                               100%   12KB  894.2KB/s   00:00
.help.swp                                          100%   12KB  780.4KB/s   00:00

#4. Copy Files Matching a Pattern

scp /path/to/local/*.txt user@remotehost:/path/to/destination/

*.txt – This is a wildcard pattern that matches all files with a .txt extension in the specified local directory. Source and destination paths are the same as before.

Example

scp /home/vboxuser/Desktop/locfolder/*.png abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop

Output

Ubuntu ssh client - screenshot thumbnail with Secure Copy Protocol support.

Here, I have tried with a .png extension.

Screenshot from 2023-08-13 21-45-23.png            100%   27KB    1.4MB/s   00:00
Screenshot from 2023-08-13 21-55-28.png            100%   24KB    1.7MB/s   00:00
Screenshot from 2023-08-13 22-04-03.png            100%   38KB    2.1MB/s   00:00

#5. Preserve File Attributes

scp -p /path/to/local/file.txt user@remotehost:/path/to/destination/

Here the ‘-p’ flag preserves the modification times, access times, and modes of the source file when copying.

Example

scp -p /home/vboxuser/Desktop/sample.zip abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop

Output

Ubuntu terminal - screenshot thumbnail with Secure Copy Protocol integration.
sample.zip                                         100%   20KB    420.0KB/s   00:00

#6. Specify a Different SSH Port

scp -P 22 /path/to/local/file.txt user@remotehost:/path/to/destination/

The ‘-P’ flag specifies a different SSH port (22 in this case) for the SCP connection. You can use any port you want.

Example

scp -P 22 /home/vboxuser/Desktop/sample.zip abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop

Output

A screenshot of a secure copy protocol transfer in an Ubuntu terminal.
sample.zip                                         100%   20KB    1.2MB/s   00:00

#7. Copy with compression

scp -C /path/to/local/file.txt user@remotehost:/path/to/destination/

This ‘-C’ flag enables compression during the transfer, potentially reducing the size of the transferred data. The source and destination address are the same as explained previously.

Example

scp -C /home/vboxuser/Desktop/sample.zip abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop

Output

A screenshot of a computer screen displaying a file transfer using Secure Copy Protocol.
sample.zip                                         100%   20KB    985.8KB/s   00:00

#8. Verbose output for debugging

scp -v /path/to/local/file.txt user@remotehost:/path/to/destination/

This ‘-v’ option activates the verbose output, which provides more detailed information during the transfer process.

Example

scp -v /home/vboxuser/Desktop/sample.zip abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop

Output

Ubuntu terminal - Secure Copy Protocol.

The sample output will look like this.

Executing: program /usr/bin/ssh host 192.168.29.70, user abhishekvarma, command scp -v -t /Users/abhishekvarma/Desktop OpenSSH_8.9p1 Ubuntu-3ubuntu0.3, OpenSSL 3.0.2 15 Mar 2022
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to 192.168.29.70 [192.168.29.70] port 22. debug1: Connection established.
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_rsa-cert type -1 debug1: identity file /root/.ssh/id_ecdsa type -1 debug1: identity file /root/.ssh/id_ecdsa-cert type -1 debug1: identity file /root/.ssh/id_ecdsa_sk type -1 debug1: identity file /root/.ssh/id_ecdsa_sk-cert type -1

#9. Copy from Remote to Local with Verbose output

scp -v user@remotehost:/path/to/remote/file.txt /path/to/local/destination/

Same explanations as the previous verbose example, with the roles of local and remote locations reversed.

Example

scp -v abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop/sample.zip /home/vboxuser/Desktop

Output

A screenshot demonstrating the Secure Copy Protocol implemented in a python script on a pc.

The output will look like this.

Executing: program /usr/bin/ssh host 192.168.29.70, user abhishekvarma, command scp -v -f /Users/abhishekvarma/Desktop/sample.zip OpenSSH_8.9p1 Ubuntu-3ubuntu0.3, OpenSSL 3.0.2 15 Mar 2022
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to 192.168.29.70 [192.168.29.70] port 22. debug1: Connection established.
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_rsa-cert type -1 debug1: identity file /root/.ssh/id_ecdsa type -1 debug1: identity file /root/.ssh/id_ecdsa-cert type -1 debug1: identity file /root/.ssh/id_ecdsa_sk type -1 debug1: identity file /root/.ssh/id_ecdsa_sk-cert type -1

#10. Copy Using Different SSH Key


scp -i /path/to/private_key.pem /path/to/local/file.txt user@remotehost:/path/to/destination/

This “-i /path/to/private_key.pem” flag specifies a different SSH private key for authentication.

Example

scp -i /path/to/private_key.pem /home/vboxuser/Desktop/sample.zip abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop

Output

Ubuntu terminal - screenshot thumbnail featuring Secure Copy Protocol.

If you have a private key, then you can use it or else you can use the default one.

sample.zip                                         100%   20KB    1.1MB/s   00:00

#11. Copy Multiple Files to the Remote server

scp file1.txt file2.txt user@remotehost:/path/to/destination/

This command copies file1.txt and file2.txt from the local directory to the specified destination on the remote server.

Example

scp  /home/vboxuser/Desktop/image.png  /home/vboxuser/Desktop/sample.zip  abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop 

Output

Ubuntu terminal - screenshot thumbnail with Secure Copy Protocol.
image.png                                          100%   39KB    1.4MB/s   00:00
sample.zip                                         100%   20KB    1.5MB/s   00:00

#12. Copy a Remote File with a Different Name

scp user@remotehost:/path/to/remote/file.txt /path/to/local/newfilename.txt

This command copies file.txt from the remote server to the local directory, renaming it as newfilename.txt in the process.

Example

scp abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop/sample.zip /home/vboxuser/Desktop/newfile.zip

Output

A secure screenshot of a python program on a phone utilizing the Secure Copy Protocol.

The old and new file extensions should be the same while executing this command.

sample.zip                                         100%   20KB    28.7KB/s   00:00

#13. Copying with Specific Bandwidth Limit

scp -l 1000 /path/to/local/file.txt user@remotehost:/path/to/destination/

This ‘-l’ flag specifies a bandwidth limit in Kbps (1000 Kbps in this case) for the transfer.

Example

scp -l 300 /home/vboxuser/Desktop/sample.zip abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop

Output

Ubuntu terminal - screenshot thumbnail using Secure Copy Protocol for file transfer.
sample.zip                                         100%   20KB    31.3KB/s   00:00
scp -rp /path/to/local/source/ user@remotehost:/path/to/destination/

The ‘-p’ flag preserves file attributes and the -r flag recursively copies directories. This command also preserves symbolic links during the copy.

Example

scp -rp /home/vboxuser/Desktop/image.png abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop

Output

Ubuntu terminal - screenshot thumbnail for Secure Copy Protocol.
image.png                                        100%   39KB    1.8MB/s   00:00

#15. Copying Files Verbosely with Compression

scp -vC /path/to/local/file.txt user@remotehost:/path/to/destination/

This command combines both verbose output (-v) and compression (-C) during the file transfer.

Example

scp -vC /home/vboxuser/Desktop/image.png abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop

Output

A screenshot of a computer screen displaying a Secure Copy Protocol (SCP) session with a list of files.

The output will look like this.

scp: debug1: fd 3 clearing O_NONBLOCK
Sending file modes: C0664 40050 image.png
Sink: C0664 40050 image.png
image.png
d 6 clearing O_NONBLOCK
scp: debug1: fd • clearing O_NONBLOCK
image.png
debug1: client_input_channel_req: channel ✪ type exit-status reply debug1: channel 0: free: client-session, channels 1
Transferred: sent 40220, received 2576 bytes, in 0.1 seconds
Bytes per second: sent 352570.0, received 22581.3
root@ubuntu: ~
debug1: Exit status 0
debug1: compress outgoing: raw data 40287, compressed 38198, factor 0.95 debug1: compress incoming: raw data 792, compressed 757, factor 0.96 

#16. Copying Between Two Remote Servers

If you want to transfer files from one remote server to another, Then you can use this command.

scp user1@remotehost1:/path/to/remote/file.txt user2@remotehost2:/path/to/destination/

This command directly transfers file.txt from remotehost1 to remotehost2.

Example

scp user1@remotehost1:/path/to/remote/file.txt abhishekvarma@192.168.29.70:/Users/abhishekvarma/Desktop

Output

file.txt                100%  512KB 512.0KB/s   00:00

If there are any errors or issues during the transfer – the output might display error messages that provide information about what went wrong.

ssh: connect to host source.example.com port 22: Connection refused
lost connection

In this case, the error message suggests that the SSH connection to the source server was refused. It could be due to various reasons, such as incorrect hostname, IP address, port, or issues with the SSH configuration.

FAQs on SCP

Here are some frequently asked questions related to SCP protocol, along with their answers.

How does SCP differ from FTP or SFTP?

FTP stands for File Transfer Protocol.

SFTP stands for File Transfer Protocol. These are both used for file transfers.

SCP operates over SSH and offers both encryption & authentication in a single protocol which makes it more secure and efficient.

When should I use SCP?

SCP is best suited for scenarios where secure and reliable file transfers are required, such as remote backups, software deployment, distributing configuration files, and collaboration among team members.

Is SCP compatible with IPv6 addresses?

Yes, SCP supports both IPv4 and IPv6 addresses which allows you to transfer files over networks that use any addressing scheme.

Conclusion✍️

I hope you found this article helpful in learning about SCP and how it works.

You may also be interested in learning about the Linux Commands for System Administrator