How to Use SCP Command to Securely Transfer Files

Update on:
Oct 2, 2021

SCP (secure copy) is a command-line utility that means that you can securely copy information and directories between two areas.

With scp, you possibly can copy a file or listing:

  • Out of your native system to a distant system.
  • From a distant system to your native system.
  • Between two distant techniques out of your native system.

When transferring knowledge with scp, each the information and password are encrypted in order that anybody snooping on the site visitors doesn’t get something delicate.

On this tutorial, we’ll present you find out how to use the scp command by means of sensible examples and detailed explanations of the most typical scp choices.

SCP Command Syntax

Earlier than going into find out how to use the scp command, let’s begin by reviewing the fundamental syntax.

The scp command syntax take the next kind:

scp [OPTION] [[email protected]]SRC_HOST:]file1 [[email protected]]DEST_HOST:]file2

Native information must be specified utilizing an absolute or relative path, whereas distant file names ought to embody a person and host specification.

scp offers plenty of choices that management each side of its conduct. Essentially the most broadly used choices are:

  • -P – Specifies the distant host ssh port.
  • -p – Preserves information modification and entry instances.
  • -q – Use this selection if you wish to suppress the progress meter and non-error messages.
  • -C – This selection forces scp to compresses the info as it’s despatched to the vacation spot machine.
  • -r – This selection tells scp to repeat directories recursively.

Earlier than you Start

The scp command depends on ssh for knowledge switch, so it requires an ssh key or password to authenticate on the distant techniques.

The colon (:) is how scp distinguish between native and distant areas.

To have the ability to copy information, you should have at the least learn permissions on the supply file and write permission on the goal system.

Watch out when copying information that share the identical identify and site on each techniques, scp will overwrite information with out warning.

When transferring giant information, it is suggested to run the scp command inside a display screen or tmux session.

Copy Recordsdata and Directories Between Two Programs with scp

Copy a Native File to a Distant System with the scp Command

To repeat a file from an area to a distant system run the next command:

$ scp file.txt [email protected]:/distant/listing

The place file.txt is the identify of the file we wish to copy, remote_username is the person on the distant server, is the server IP tackle. The /distant/listing is the trail to the listing you wish to copy the file to. In the event you don’t specify a distant listing, the file might be copied to the distant person dwelling listing.

You may be prompted to enter the person password, and the switch course of will begin.

[email protected]'s password:
file.txt                             100%    0     0.0KB/s   00:00

Omitting the filename from the vacation spot location copies the file with the unique identify. If you wish to save the file beneath a unique identify, it’s worthwhile to specify the brand new file identify:

$ scp file.txt [email protected]:/distant/listing/newfilename.txt

If SSH on the distant host is listening on a port apart from the default 22 then you possibly can specify the port utilizing the -P argument:

$ scp -P 2322 file.txt [email protected]:/distant/listing

The command to repeat a listing is very similar to as when copying information. The one distinction is that it’s worthwhile to use the -r flag for recursive.

To repeat a listing from an area to distant system, use the -r choice:

$ scp -r /native/listing [email protected]:/distant/listing

Copy a Distant File to a Native System utilizing the scp Command

To repeat a file from a distant to an area system, use the distant location as a supply and native location because the vacation spot.

For instance to repeat a file named file.txt from a distant server with IP run the next command:

$ scp [email protected]:/distant/file.txt /native/listing

In the event you haven’t set a passwordless SSH login to the distant machine, you can be requested to enter the person password.

Copy a File Between Two Distant Programs utilizing the scp Command

In contrast to rsync , when utilizing scp you don’t need to log in to one of many servers to switch information from one to a different distant machine.

The next command will copy the file /information/file.txt from the distant host host1.com to the listing /information on the distant host host2.com.

$ scp [email protected]:/information/file.txt [email protected]:/information

You may be prompted to enter the passwords for each distant accounts. The information might be switch immediately from one distant host to the opposite.

To route the site visitors by means of the machine on which the command is issued, use the -3 choice:

$ scp -3 [email protected]:/information/file.txt [email protected]:/information


On this tutorial, you realized find out how to use the scp command to repeat information and directories.

You may additionally wish to arrange an SSH key-based authentication and hook up with your Linux servers with out coming into a password.

If you’re frequently connecting to the identical techniques, you possibly can simplify your workflow by defining your entire connections within the SSH config file .

Related Posts

The 30 most useful Linux commands

This article will introduce some practical and common Linux or Unix command , which is the Linux system administrators normally use the command . This article is not a complete list, but a brief list of commands that can be useful...

15 Practical examples of tcpdump command

15 Practical examples of tcpdump command

The tcpdump is the most essential and powerful command-line network packet sniffing tool. We use it to analyze network packets, capture or filter TCP/IP packets that transferred and received over the network on a specific network interface. It is available in almost...

15 Useful “df” Commands to Check Disk Space in Linux

15 Useful “df” Commands to Check Disk Space in Linux

There are several Linux tools available on the internet to check disk space utilization; we can use any of them. But, Linux has a best built-in command-line utility called “df”. The “df” is the short form of “disk filesystem”, we can get detailed information on used...

15 Practical examples of echo command

15 Practical examples of echo command

If you are writing a bash script, you know echo command very well. It is use to display text lines on an output screen or a file. The echo command is the most common command used by all Linux users, and it is built-in Linux command. echo command syntax: echo...

Follow Us

Our Communities

More on Linux Command

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!



Submit a Comment

Your email address will not be published.

17 + 6 =