SCP Command in Linux {13 Examples}

November 16, 2023

Introduction

SCP (Secure Copy Protocol) is a network protocol that securely copies files/folders between Linux (Unix) systems on a network. To transmit, use the scp command line utility, a safer variant of the cp (copy) command.

SCP protects your data while copying across an SSH (Secure Shell) connection by encrypting the files and the passwords. Therefore, the information is still encrypted even if the traffic is intercepted.

This guide shows you how to copy files using the SCP command and includes 13 practical.

SCP Command in Linux

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 the target/path) from the command, the program looks for (or copies) the file locally.

When working with remote files, always specify the user and host specifications.

Use an account with read access to the file(s) you want to copy on the source system. Additionally, you need to 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 check the destination location before writing. Any files in the destination with the same name will be overwritten without notification.

SCP Command Options

You can add many scp command options to the command to customize and speed up the process. Options are added as attributes right after the scp command.

Each option has a short, single-character form and a longer, descriptive equivalent.

OptionDescription
-1Use protocol 1.
-2Use protocol 2.
-4Only use Ipv4 addresses.
-6Only use IPv6 addresses.
-BRun 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.
-CEnable 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).
-dCopy the file only if the destination directory already exists.
-F [file]Specify an alternative configuration file for SSH.
-hShow 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_optionSet options to SSH in ssh_config format.
-P [port]Specify the port to which to connect. If not specified, SCP uses port 22.
-qRun SCP in quiet mode.
-QDisable file transfer statistics.
-rCopy recursively.
-S [program]Use a specified program for encryption connection.
-uDelete the source file once the copy is complete.
-vEnable verbose mode, which sets the debug level to 2.

SCP Command Examples

Use the SCP command in the following cases:

  • 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 understand this useful utility better, take a look at 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 [email protected]:/home/remote_dir 

The command includes the following information:

  • Desktop/sample_example.txt - The name of the file being copied and its location.
  • [email protected] - The username and IP address of the remote server.
  • /home/remote_dir - The location where to store the copied file.
Copy a file from a local server to a remote server using the scp command.

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

  • [email protected] - 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 a file from a remote server to a local server using the scp command.

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 [email protected]:/home/remote_dir/sample_example.txt [email protected]:home/Desktop

The command above specifies:

  • [email protected] - 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.
  • [email protected] - The username and IP address of the remote server where we want to copy the file.
  • home/Desktop - The location where to store the copied file 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 [email protected]:/home/remote_dir

It includes the following information:

  • example/sample1.txt - The name and location of the first file being copied.
  • example/sample2.txt - The name and location of the second file being copied.
  • [email protected] - The username and IP address of the remote server receiving the files.
  • /home/remote_dir - The location where to store the copied files on the remote server.
Copy multiple files from local server to remote server using the scp command.

Copy Directory from Local Host to Remote Server Recursively

Apart from files, scp can also securely copy directories to or from remote servers. The following command shows how to copy a sample directory to a remote server recursively:

scp -r example [email protected]:/home/remote_dir

The command includes:

  • -r - The option for copying the directory recursively.
  • example - The name of the directory copied from the local server.
  • [email protected] - The username and IP address of the remote server receiving the folder.
  • /home/remote_dir - The location where to store the copied directory on the remote server.

Copy File with SCP Using Specific Port

By default, SCP uses port 22. However, if a remote system is configured to listen to SSH requests on a different port, use the –P switch to specify the port.

For example, the following command copies a file from a local to a remote server using port 2222:

scp -P 2222  Desktop/sample_example.txt [email protected]:/home/remote_dir

The components of the command above are:

  • -P 2222 - Instruct the command to use the port 2222.
  • Desktop/sample_example.txt - The file name you want to copy and its location.
  • [email protected] - The username and IP address of the remote server receiving the file.
  • /home/remote_dir - The location where to store the copied file on the remote server.

Copy File with SCP in Quiet Mode

Running a scp command in quiet mode means disabling the progress meter and non-error messages from showing in the output. To do so, you need to add the -q option:

scp -q Desktop/sample_example.txt [email protected]:/home/remote_dir
Copy a file using the scp command in quiet mode.

Copy File with SCP in Verbose Mode

You can run scp in verbose mode by adding the -v option, which sets the debug level to 2. This flag prints debugging information in the output to help you when troubleshooting.

Add the -v option after scp to enable verbose mode, as in the example below:

scp -v Desktop/sample_example.txt [email protected]:/home/remote_dir
Copy a file using the scp command in verbose mode.

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 especially useful when copying large files to prevent SCP from draining the bandwidth.

When limiting bandwidth, specify the number in Kilobits/s. Bear in mind that 1 byte = 8 bits. Therefore, if you want to limit the bandwidth for SCP to 100 KB/s, the value in -l kbps would be 800 (100 x 8), as in the command below:

scp -l 800 Desktop/sample_example.txt [email protected]:/home/remote_dir

Copy File with SCP Faster

To speed up the file transfer from one server to another, add the -C option to compress the file while it's being transferred. Once the file reaches its destination, it returns to its regular size.

scp -C Desktop/sample_example.txt [email protected]:/home/remote_dir

Copy File with SCP Using Specific Cipher

By default, SCP uses AES-128 to encrypt files. However, the -c option allows you to change the cipher SCP will use to encrypt the file.

For instance, to increase security, you can switch to 3DES encryption, as in the following example:

scp -c 3des Desktop/sample_example.txt [email protected]:/home/remote_dir

Copy File with SCP Using IPv4 or IPv6

You can force SCP to only use IPv4 or IPv6 by adding the -4 or -6 attribute.

To copy a sample file from a local server to a remote host only using IPv6, enter:

scp -6 Desktop/sample_example.txt [email protected]:/home/remote_dir

Copy File with SCP and Preserve File Attributes

To copy a file using SCP and preserve file attributes such as modification and access times, modes, and permissions, use the -p option:

scp -p Desktop/sample_example.txt [email protected]:/home/remote_dir

Conclusion

In this guide, you have learned what the scp command is and how to use it to secure the transmission of files. This tool is especially useful as a replacement for FTP, which is inherently insecure by default.

The secure copy protocol also follows regular command-line and SSH functionality, creating a seamless command set for managing files between Linux machines.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP who is passionate about programming. Her background in Electrical Engineering and Computing combined with her teaching experience give her the ability to easily explain complex technical concepts through her content.
Next you should read
How to Copy Files and Directories in Linux
December 28, 2023

Want to learn how to copy files in Linux OS? This guide will show you how to use the Linux commands to copy...
Read more
How to Use mkdir Command to Make or Create a Linux Directory
December 1, 2023

The mkdir command in Linux allows users to create or make new directories. mkdir stands for "make directory."
Read more
How To Use grep Command In Linux/UNIX
February 29, 2024

This guide details the most useful grep commands for Linux / Unix systems. After going through all the...
Read more
How to Find Files in Linux With the Find Command
December 19, 2018

The find command is a useful command-line tool in Linux. It allows you to use the terminal to search for...
Read more