12 SCP Command Examples To Securely Transfer Files In Linux

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

This Tutorial Explains Secure Copy Protocol or SCP Command used for Securely Copying the files in Linux and Unix with Syntax and Examples:

In this article, we will discuss SCP (Secure Copy Protocol) command which is used for file transfer. We will see what it is and how it works with the help of a few examples. So, let us first try to understand what is SCP command.

SCP Command

What Is SCP Command?

SCP (Secure Copy Protocol) is a network protocol that is used for securely transferring the files between hosts on a computer network. Using this command-line utility in Linux and Unix like systems, you can do file transfer from a localhost to a remote host, or from a remote host to a local system, or between two remote hosts.

SCP Command

[image source]

SCP ensures the authenticity, encryption, and confidentiality of the data by making use of SSH (Secure Shell) mechanism for transferring the files. So, the data in transit is protected from the snooping attacks. Clients can upload and download files and directories to and from a server using this protocol. It requires either password or keys for authentication. The default port for SCP is TCP port 22.

The benefit of the SCP protocol is that you do not need to start an FTP session or log into the remote hosts explicitly for file transfer.

Syntax For SCP Protocol

#1) For copying the file from local to the remote host

scp [options] SourceFileName UserName@TargetHost:TargetPath

This is the very basic syntax of the SCP command that will copy the source file from the current host to the target path at the target host using a user account. Typically, it’s quite similar to the copy cp command.

#2) For copying from a remote host to local

For copying file:

scp [options] UserName@SourceHost:SourceFilePath TargetFileName

Or, simply download the file:

scp [options] UserName@SourceHost:SourceFilePath

For copying Folder (recursively):

scp -r UserName@SourceHost:SourceDirectoryPath TargetFolderName

If the remote host uses a port other than the default port 22, then the port number needs to be explicitly mentioned in the command using -P option.

#3) Copying from one remote computer to another remote computer

scp [options] UserName@SourceHost:SourcePath UserName@TargetHost:TargetPath

When you copy files from one remote computer to another, the traffic does not pass by your computer. This operation takes place directly between the two remote servers.

#4) Copying multiple files

For copying multiple files from localhost to a remote host:

scp file1 file2 UserName@TargetHost:TargetDirectoryPath

For copying multiple files from a remote host to a current directory of localhost:

scp UserName@SourceHost:SourceDirectoryPath{file1, file2}

Options Used With SCP command

The most commonly used options with SCP command are listed below:

  • -C : C, here stands for enable compression. By using this option, compression will be enabled and transfer speed will be increased while copying. It will automatically enable compression at the source and decompression at the target.
  • -c <cipher>: c stands for cipher. By default, SCP uses ‘AES-128’ for encryption of files. If you want to change the cipher, you need to use -c option followed by cipher name.
  • -i <identity_file>: i stands for identify file or private key. Generally, key-based authentication is chosen in Linux environments. So, we can specifically mention the private key file or identity file using -i option.
  • -l <limit>: l stands for limit bandwidth. Through this option, you can set the maximum bandwidth to be used. It is in Kbits/s.
  • -B: This option is used for using batch mode while copying.
  • -F <ssh_config>: This option is used for using a different ssh_config file while copying in situations where you need to use different networks to connect to Linux systems. In such scenarios, you need to assign a substitute per-user SSH configuration file.
  • -P <port>: If the ssh port number of destination host is different than the default port number 22, then you need to specifically mention the port number by using -P option.
  • -p: This option is used for preserving file permissions, modifications, and access times while copying.
  • -q: This option will execute the SCP command in quiet mode. It will turn off the progress meter and will not show up the transfer progress, warning, or diagnostic messages of ssh on the Linux terminal screen.
  • -r: -r option is used to copy the files and directories recursively. For example, if you want to copy the entire folder (along with contents inside the folder) on a target machine, you need to use -r option.
  • -S <program>: This option is used for specifying the program to use for connecting.
  • -v: v stands for verbose. This option will show step by step progress of the SCP command execution on the terminal screen. It is really helpful in debugging.

SCP Command Examples

Let us understand how to use SCP Command with the help of examples:

Example 1: for copying from local to the remote host

scp -v lockfile.txt kaushapx@10.172.80.167: /home/cpf657/kaushapx/test1

In the above example,

  • -v option is used as a verbose option to see the details of the output of this command on the Linux terminal. Using the verbose output, you can get to know exactly what’s happening in the background when the command is executed. This helps debug.
  • Lockfile.txt is the source file name that we want to transfer to a remote host.
  • Kaushapx is an example of a username. Using this username account, we will securely copy the file to the remote host.
  • 10.172.80.167 is the example of the IP of the target remote host onto which we want to transfer the file.
  • /home/cpf657/kaushapx/test1 is an example of an absolute path where we want to put this transferred file.

The below screenshots demonstrates the execution of the above SCP command.

Execution of the SCP command

Execution of the SCP command continuation

Example 2: for copying from remote host to local system:

scp kaushapx@10.172.80.167:/home/cpf657/kaushapx/test/parent/directory1/DemoFile.txt /home/tpf655/kaushapx

To copy from remote host to local system

Example 3: for copying multiple files to a remote host:

scp DemoFile.txt log.xml kaushapx@10.172.80.167:/home/cpf657/kaushapx/test

To copy multiple files to a remote host

Example 4: for copying files across two remote systems:

scp kaushapx@10.172.80.167:/home/cpf657/kaushapx/console.txt kaushapx@10.182.46.76:/home/tpf655/kaushapx/test

To copy files across two remote systems

Example 5: for copying files and directories recursively (using -r option):

Suppose, I have a folder called ‘test’ in the localhost and this folder contains four files. I want to copy the entire folder inside another folder called ‘test1’ present at a remote host.

I will use the following command:

scp -r test kaushapx@10.172.80.167:/home/cpf657/kaushapx/test1

for copying files and directories recursively

Example 6: for increasing the speed of copy by enabling compression (using -C option):

Let us transfer the same folder that we did in Example 5, but this time by enabling the compression:

scp -r -C test kaushapx@10.172.80.167:/home/cpf657/kaushapx/test1

increasing the speed of copy by enabling compression

Example 7: for limiting the bandwidth while copying (using -l option):

Let us continue with the same option. This time we will use -l option and specify the bandwidth, say 500. Remember, the bandwidth we have put here is in Kbit/s.

limiting the bandwidth while copying

Example 8: for specifying different ssh port while copying (using -P option):

If the remote server on which you are copying the file is using some port other than the default port 22 then you need to explicitly tell the port number in the SCP command by using -P option. For example, if the ssh port of the remote server is 2022, then you will mention -P 2022 in the SCP command.

scp -P 2022 console.txt kaushapx@10.182.46.76:/home/tpf655/kaushapx/test

specifying different ssh port while copying

Example 9: for preserving file permissions, modifications, and access times while copying (using -p option):

scp -p console.txt kaushapx@10.172.80.167:/home/cpf657/kaushapx/test1

for preserving file permissions, modifications, and access times while copying

Example 10: for copying files in quiet mode (using -q option):

scp -q console.txt kaushapx@10.172.80.167:/home/cpf657/kaushapx/test1

copying files in quiet mode

Example 11: for identifying files in SCP while copying (using -i option):

In the above example, my_private_key.pem is the identity file or private key file.

identifying files in SCP while copying

Example 12: for using a different cipher while copying through SCP (using -c option):

scp -c 3des-cbc -r test1 kaushapx@10.182.46.76:/home/tpf655/kaushapx/test

Using a different cipher while copying through SCP

FAQ About SCP Command

In this section, we will cover some frequently asked questions on the SCP command.

Q #1) What is SCP command?

Answer: SCP stands for Secure Copy Protocol. Using the SCP command, you can perform the copying of files securely between hosts on a network. It uses the mechanisms of SSH for data transfer. It uses either key or password-based authentication.

Q #2) What does SCP do in Linux?

Answer: In Linux, the SCP command transfers the files between servers in a secure manner. It could be a file copy between a remote server and a local host or between two remote servers. SCP is a pre-installed command in Linux and is known for its simplicity and security.

Q #3) How do we SCP files in Linux?

Answer: You can SCP files by the following command syntax:

 scp [options] [username@][source_host:]file1 [username@][destination_host:]file2.

Many options can be used with the SCP command. For example, -C for compression, -c for cipher, -P for the port, -I for the private key, -l for limit, -r for recursive copy, etc.

Q #4) How do we SCP a file?

Answer: You can SCP a file by using the SCP command as mentioned in Q #3.

Q #5) Does SCP copy or move?

Answer: SCP command copies the file(s) from source to destination. So, after SCP, the file will be present at both the hosts.

Q #6) Can you use SCP for a directory?

Answer: Yes, we can use SCP for a directory. You need to use -r option for copying the entire directory along with its contents.

Below is the SCP command syntax for copying the directory from a localhost to a remote host:

scp -r localhost_path_to_directory username@target_server_ip:/path_to_target_directory/

Q #7) How do we use SCP for all files in a directory?

Answer: To use SCP on all files in a directory, you need to add * with the directory path:

scp -r localhost_path_to_directory/* username@target_server_ip:/path_to_target_directory/

This way, all the files inside the local directory will be copied to the remote directory.

Q #8) Can we use SCP in Windows?

Answer: Yes, you can use SCP in Windows. However, it is not pre-downloaded in Windows, unlike Linux and Mac, so for Windows, you need to install the SCP software separately.

You can download Putty which includes SCP for Windows (software called Putty SCP (PSCP), or you can download WinSCP (Windows Secure Copy). PSCP client runs directly from the Windows command prompt. There are other software as well for using SCP in Windows.

Q #9) How do we use SCP for multiple files?

Answer: For copying multiple files from localhost to a remote host using SCP :

scp file1 file2 UserName@TargetHost:TargetDirectoryPath

For copying multiple files from a remote host to a current directory of localhost using SCP :

scp UserName@SourceHost:SourceDirectoryPath{file1, file2}

Q #10) What is the difference between SCP and SFTP?

Answer: SCP is the Secure Copy Protocol. SFTP is the Secure File Transfer Protocol. Both use TCP port 22 and run on the SSH mechanism. But they differ in specifications and functions.

SCP only transfers the data, whereas SFTP also performs file access and file management functions, in addition to the file transfer. With SFTP, you can do operations like listing remote directories or deleting files. But SCP only allows the copying of files and directories between servers.

The file transfer speed in SCP is faster than SFTP because it uses a more efficient algorithm for transferring files.

In SFTP, you can resume interrupted file transfer from the command line client. But SCP does not have this function.

SFTP offers a GUI component but SCP does not have that.

Q #11) What is the SCP Command in Windows for securely copying a file?

Answer: Open Windows Command Prompt and give below command for securely copying a file from a Windows local machine to a server (it can be a Linux server):

pscp filepath userid@target_server_ip:target_path

Example: pscp c:\desktop\sample.txt userxyz@192.162.x.x:/tmp/foo/sample.txt

You should have PSCP installed for executing this command.

Q #12) Is SCP Secure?

Answer: Yes, SCP is secure. It uses SSH (Secure Shell Protocol) mechanism for data transfer, thus it gets benefitted from the security offered by SSH. The data in transit is kept confidential and also its authenticity is ensured.

Conclusion

In this tutorial, we have seen how to use the SCP command for securely copying the files between two remote hosts or between a local host and a remote host, without initiating an FTP session or logging into the remote machines explicitly.

SCP employs the SSH mechanism for copying the data and thus the data in transit is encrypted and secured. It needs a password or key for authentication. In contrast with RCP (Remote Copy Protocol) or FTP (File Transfer Protocol), SCP encrypts both the file and passwords exchanged between the systems to protect any snooping on the network.

Was this helpful?

Thanks for your feedback!

Leave a Comment