Use SCP Command to Copy a File in Linux

Use SCP Command in Linux to Securely Copy a File

SCP is the abbreviation of Secure Copy Protocol. Use SCP Command matters when you decide to copy files and directories between two locations. SCP is a command-line tool that helps you provide the security of your files. Using SCP enables you to copy your considered content between Linux systems on a network. Since it uses an SSH connection to encrypt the files and passwords, even if the traffic is intercepted your information will be encrypted and protected. Also, it is possible to use SCP on Linux, Mac, and Windows (using WinSCP).

In this article, you will learn How to Use SCP Command in Linux to Copy a File. Obviously, having a Linux server is the first required step to use SCP in it, but do not purchase a Linux VPS until you have not checked our cheap but fantastic plans.

What is SCP? When do you need to use it?

SCP protects your file/files across an SSH connection when you need to copy them between two computers. To avoid snooping by people on the traffic, both the files and passwords are encrypted while SCP is transferring data. With a secured encrypted connection, SCP command-line tool is the most secure way to do the copies in Linux. Previously, you learned what is SSH port. It helps you to understand how SCP connects via an encryption (SSH) connection and uses port 22 by default.

To copy a file or directory securely, you can use SCP in three different situations:

  1. From: Your local System /Host/Computer. To: A remote System/Host/Computer
  2. Copying file/files from: A remote Computer/Server. To: Another remote Computer/Server
  3. From: A remote System /Host/Computer. To: A local System/Host/Computer

Note: If your internet connection is slow, choose the 2nd case since the traffic goes directly from one server to the other without passing through your system.

What is SCP When do you need to use it

Prerequisites to Use SCP Command in Linux

Since we need to point at diffferent IPs, we assume the IP of the first server is IP A, the IP of the second server is IP B, and the IP of the third server is IP C. Remember to replace your own ones with them while running the commands.

To bring successful use of the linux SCP Command tool, consider the options below.

  • A secure shell login on the server system
  • Administrative privileges on both the client and server (Optional)

SCP Complete Syntax

Join us with this article to see how you can use the scp command in linux with the most common useful and practical examples. In this part, the SCP Command Syntax and all required explanations of each command will be discussed.

You will use the scp command to tell the operating system to copy the file/files over a secure shell connection. (ssh connection)

First, let’s see the syntax for using the scp command.

scp [option] [user_name@source_host:path/to/source/file] [user_name@target_host:target/path]

We need to explain that you are free to specify both username and host in origin (user_name) and destination (target/path). The scp command will copy a local file to a local destination if you avoid determining the username and password.

To show you the meaning of every basic part of it:

  • Option: Include cipher, ssh configuration, ssh port, limit, and recursive copy which are scp options.
  • User_name: As you specify the file/files to be copied, it is called the origin. It contains the information of a remote host or user owning the file/files in that remote host. If you do not specify the user, it would default to the current user on the machine. And if you do not specify the host, it would search the file locally using any given path.
  • Path/to/source: Where you specify the path where you prefer the files to go to be copied. Clearly, this is your destination. It contains the information of the remote host or user in that host. Like the username, if you do not specify the user, it would default to the current username. So, it will log in to the remote server using that user. And if you do not specify the host, it would copy the files locally. You need to specify the local files with an absolute or relative path. Keep in mind that since the scp command will not check the destination location before writing, all the files in the destination with the same name would be overwritten without notification.

How to Use Linux scp Command?

You are highly recommended to consider the below options while working with a remote file, and before you begin using the scp command.

  1. Use an account with at least read access (permission) to the file/files you need to copy on the source system.
  2. Specify the user and host specifications.
  3. Use the colon (:) to see how scp distinguish between local and remote locations.
  4. Provide an ssh key or password to authenticate to the remote system since the scp command relies on ssh for data transfer. For this, you can refer to Setup SSH Key on Linux Server article.
  5. Use an account with at least write access (permission) to the dictionary where the file/files will be saved on the destination system.
  6. Run the scp command inside a screen or tmux session anytime you transfer large files.

How to Customize scp Command in Linux

There are some SCP Command options that enable you to speed up the process by customizing it. You need to add these options as attributes right after the scp command.

  • -o ssh_option – Set options to SSH in ssh_config format.
  • -S program – Use a specified program for encryption connection.
  • -i file – Specify the file from which to read the identity for public key authentication.
  • -P por – Specify the port to which to connect. If not specified, SCP uses port 22.
  • -l limit – Limit the bandwidth (specify the limit in Kbit/s).
  • -F file – Specify an alternative configuration file for SSH.
  • -D debug_level – Set the debug level (1, 2, 3, or 99).
  • -c cipher – Select the cipher for data encryption. If not specified, SCP uses the default – ‘AnyStdCipher’.
  • -b buffer_size – Specify the buffer size used for data transfer. If not specified, uses the default – 32768 bytes.
  • -B – Run in batch mode, disabling all queries for user input.
  • -C – Enable compression.
  • -d – Copy the file, only if the destination directory already exists.
  • -h – Show a list of command options.
  • -q – Run SCP in quiet mode.
  • -Q – Disable displaying any file transfer statistics.
  • -r – Copy recursively.
  • -u – Delete the source file once the copy is complete.
  • -v – Enable verbose mode, which sets the debug level to 2.
  • -1 – Use protocol 1.
  • -2 – Use protocol 2.
  • -4 – Only use Ipv4 addresses.
  • -6 – Only use IPv6 addresses.

13 scp Command Linux Examples

I believe that an explanation will never be complete without examples. Stay with us in this guide to review some scp command examples to become an expert in using the SCP Command utility.

1- Use scp Command to copy a Remote Server File to a Local Host

Run the command below to copy a single file from a remote host to a local host.

scp IP A:/home/remote_dir/sample_example.txt home/Desktop

Have a look at the information:

root@IP A – The file’s current location on the remote server, along with its username and IP address.

/home/remote_dir/sample_example.txt – Specifies the name of the file you want to copy and its location.

home/Desktop – The location of the copied file storage.

2- Use scp Command to copy a File Between Two Remote Servers

You do not need to log in (ssh) to one of the servers when you are transferring file/files from one to another remote machine.

scp root@IP A:/home/remote_dir/sample_example.txt username@IP B:home/Desktop

Check the information of the command above:

IP A- The username and location of the file being copied.

username@IP B– The remote server’s username and IP address, to which we wish to copy the file.

home/Desktop – The location on the other server where the copied file should be kept.

3- Use scp Command to copy a Local File to a Remote Server

Here, you can see an example of copying one single file from a local host to a remote server.

scp Desktop/sample_example.txt root@IP C:/home/remote_dir

The command includes:

Desktop/sample_example.txt – The name and location of the file being copied.

root@IP c – The remote server’s IP address and username.

/home/remote_dir – The location of the copied file storage.

4- Use scp Command to copy a Local Host Folder to Remote Server Recursively

SCP is capable of safely copying directories to or from distant servers. You can use the command below to copy a single directory to a remote server recursively.

scp -r example root@IP A:/home/remote_dir

It includes the information below:

-r – The choice to copy the folder in a recursive manner.

example – The name of the folder that is being copied from the local server.

root@IP A – The IP address and username of the distant server receiving the folder.

/home/remote_dir – The path on the remote server where the copied folder should be kept.

5- Copy Multiple Files with scp command linux

Using SCP enables you to copy multiple files in a single command. As you can see below, it is possible to copy two files from a local host to a remote server.

scp example/sample1.txt example/sample2.txt root@IP A:/home/remote_dir

The command includes:

example/sample1.txt – The first file being copied’s name and location.

example/sample2.txt – Name and location of the second file that is being copied.

root@IP A – The IP address and username of the remote server that is receiving the files.

/home/remote_dir – The location on the other server where the copied files should be kept.

6- Use scp Command to copy a File with SCP in Verbose Mode

The 6th example of SCP Command is to set the debug level to 2 and use scp in verbose mode by adding the -v option. In this way, printing debugging information in the output causes good help while facing trouble.

As you see in the following example, to enable verbose mode, you need to add-voption afterscp.

scp -v Desktop/sample_example.txt root@IP A:/home/remote_dir

7- Copy a File with scp Command in Quiet Mode

You can disable the progress meter and non-error messages from showing in the output if you run a scp command in quit mode. You just need to add the -q option to do this.

scp -q Desktop/sample_example.txt root@IP c:/home/remote_dir

8- Copy a File with scp Using a Specific Cipher

While SCP uses AES-128 to encrypt files by default, you can change the cipher SCP will use to encrypt the file by using the -c option. The following example shows you how you can increase security by switching to 3des encryption.

scp -c 3des Desktop/sample_example.txt root@IP A:/home/remote_dir

9- Use scp Command to copy a File and Limit Bandwidth

SCP Command is also used to limit the bandwidth if you add the -l parameter. You will find it useful anytime you decide to copy large files to prevent SCP from draining the bandwidth.

Note: Specify the number in Kilobits/s when you want to limit bandwidth.

scp -l 800 Desktop/sample_example.txt root@IP A:/home/remote_dir

10- Use scp Command to copy a File Using a Specific Port

SCP uses port 22 by default. While using a remote system that is configured to listen to SSH requests on a different port, you can use the –P switch to specify the port.

The below example helps you to see how you can use the SCP Command to copy a file from a local to a remote server. Here the used port is 2222.

scp -P 2222  Desktop/sample_example.txt root@IP A:/home/remote_dir

Let’s see what are the components of the above command:

-P 2222 – use port 2222.

Desktop/sample_example.txt – The location and name of the file you wish to copy.

root@IP A – The IP address and user name of the remote server that is receiving the file.

/home/remote_dir – The location on the other server where the copied file should be kept.

11- Use scp Command to Copy a File Using IPv4 or IPv6

If you force SCP, it will only use IPv4 or IPv6 depending on your needs by adding the -4 or -6 attribute.

Now, look at the below example to see how you can copy a sample file from a local server to a remote host only using IPv6.

scp -6 Desktop/sample_example.txt root@IP A:/home/remote_dir

12- Use scp Command to Copy a File with SCP Faster

Speed always matters. When a file is being transferred from one server to another, you can speed up it by adding the -C option. This will compress the file during the transmission. But you do not need to be concerned about the size of the file since the file will reach its destination when returns to its normal size.

scp -C Desktop/sample_example.txt root@IP A:/home/remote_dir

13- Use scp Command to copy a File with SCP Preserving File Attributes

SCP Command is capable to preserve file attributes such as modification and access times, modes, and permissions. To copy a file using SCP with the mentioned situation, you need to use the -p option.

scp -p Desktop/sample_example.txt root@IP A:/home/remote_dir

Yes, you just need to run the command below and replace your considered type withext.

scp /path/to/folder/*.ext user@server:/path/to/folder/


In this article, you learned what is Secure Copy Protocol command-line utility and How to Use linux SCP Command to securely Copy a File. To understand better and see the ways you can use SCP Command, 13 examples were explained and discussed. If you have used FTP, now you know that SCP Command is an ideal replacement for it since FTP is insecure by default. It also helps you to create a command set to manage your considered files between Linux machines.

Share your opinion if you have work experience with any one of them to copy your files and directories in Linux. And if you encounter any problems, please do not hesitate to contact us. Our technical support team will try their best to solve your problems.

Leave a Reply

Your email address will not be published. Required fields are marked.