How to Use scp Command to Securely Transfer Files in Linux.
Suppose you wish to switch some information between servers on a Linux system. There are some ways you possibly can accomplish the duty, but when your precedence is to switch information securely, you’re left with a couple of choices. The best resolution is to make use of the scp or safe copy command to switch your information with out worrying about safety. In this text, we’ll clarify how to make use of switch information securely using the scp command in your Linux system.
Transfer Files with scp Command in Linux (2023)
What is scp Command in Linux
When it involves transferring information over a safe network, the scp command could be very helpful. It stands for Secure Copy Protocol and transfers information and directories between two programs over SSH (Secure Shell) connection. With ssh, you could be assured of the safety of your information as they’re encrypted by default. In less complicated phrases, the scp command is a safer model of the cp command, which you’ll be able to learn all about in our Linux Terminal instructions article.
How to Use the scp Command: Syntax & Options
Before we are able to switch information through scp, allow us to see the syntax and choices of the command proper right here:
scp <possibility> <host_user_name@host_ip_address:path/to/supply/file> <target_user_name@target_ip_address:goal/path>
Understanding the syntax:
- First,
<host_user_name@host_ip_address:path/to/supply/file>
specifies the ‘source’ system from the place you might want to switch your information/ directories. - Second,
<target_user_name@target_ip_address:goal/path>
specifies the ‘target’ system to which you wish to switch your information/directories.
If you permit both of the above parameters, the scp command in Linux will first search for the file after which copy the file regionally. Some of the choices to pair with the command are:
Options | Destination |
---|---|
-P |
Specifies which port to attach with the host system. If omitted, then it’ll use port 22 by default. |
-p |
Preserves modification instances, entry instances, and modes from the unique file when copied to the goal system. |
-r |
Copies all the listing to the goal system recursively. |
-J |
Used to attach the supply system and vacation spot system through a proxy system (soar host). |
-3 |
when this flag is used, it copies the information to each the goal system in addition to the native system |
-4 |
forces the scp command to make use of solely IPv4 addresses. |
-6 |
forces the scp command to make use of solely IPv6 addresses. |
How to Copy Files Using the scp Command
Before using the scp command in Linux, there are some stipulations that you might want to fulfill on each the host and goal programs:
- ssh must be put in
- root entry or any person with sudo privileges
Copy Files from Local Host to Remote Target
If you’ve got a file you might want to switch out of your native system to a distant goal, you might want to use the next syntax:
scp
<choices>
<path_to_local_file> <remote_user>@<remote_target_ip_address>:<path_to_store_in_remote_destination>
If the above syntax could appear difficult, an instance will clear it out:
scp take a look at.txt remote2@139.144.11.105:/residence/remote2/Documents/
In the above instance:
take a look at.txt
is the identify of the file to be transferred and it’s positioned within the present listing of the native system.
is the username on the goal system.take a look at
139.144.11.105
is the IP tackle of the goal system./residence/remote2/Documents/
is the placement on the goal system the place the transferred file can be saved.

Copy Files from Remote Host to Local Target
If you might want to switch a file from a distant host to your native machine, use the next scp command syntax in your Linux system:
scp
<choices>
<remote_user>@<remote_host_ip_address>:<path_of_file_to_transfer> <path_to_store_file>
For instance, let’s say you might want to switch a file named take a look at.py from the distant server you’re engaged on, use this command:
scp take a look at@10.10.11.113:/residence/take a look at/test1.py ~/test1.py
In this instance:
is the distant host username.take a look at
10.10.11.113
is the distant host IP tackle.
is the trail of the file to be transferred from the distant host./residence/take a look at/test1.py
~/test1.py
is the identify of the file after it’s transferred to the native machine and saved within the residence listing.

Transfer Files from One Remote Host to Another Remote Target
With the scp command, you can’t solely switch information between your native system and a distant machine but in addition switch information between two distant programs. But, earlier than you possibly can switch information from one distant system to a different distant system, it’s really helpful to generate a non-public ssh key and a public ssh key on the supply system and retailer a replica of the general public key within the vacation spot system.
Generally, customers complain in regards to the “Host key verification failed” error when transferring information between two distant programs. To bypass the error, use an ssh-key as an added measure. We have defined how you are able to do that proper right here:
1. Generate a public/ non-public key pair on the supply server with this command:
ssh-keygen -t <encryption_algorithm>
2. For encryption algorithms, you should use “rsa
,” which is probably the most generally used algorithm or another algorithm of your selection.
3. Then, you may be requested to decide on the placement to retailer the ssh key. You can both retailer it at any location of your selection or within the default location.
4. For the passphrase, you possibly can enter something of your selection or go away it clean by urgent Enter.

5. Then, copy the general public key to the vacation spot server using the command beneath. With this, you possibly can even log in to the vacation spot system with out a password using ssh.
ssh-copy-id <destination_username>@<destination_ip_address>
Note: the passwordless methodology of logging in will solely work for the person for which you’ve got generated the ssh-key.
6. Once you’ve got created and saved the ssh key on the distant server, use this scp command syntax to trade information between two distant programs working Linux:
scp <choices> <remote_user_1>@<remote_host_ip_address>:<path_of_file_to_transfer> <remote_user_2>@<remote_target_ip_address>:<path_to_store_in_remote_destination>

Let’s say you might want to switch a file named take a look at.txt from one distant host to a different distant receiver, use the command:
scp remote1@10.10.11.113:/residence/test1/take a look at.txt remote2@10.11.27.111:/residence/remote2/Documents/test1.txt
In this instance:
remote1
is the identify of the person within the distant sender host10.10.11.113
is the IP tackle of the distant sender host/residence/test1/take a look at.txt
is the trail to the file to be despatchedremote2
is the identify of the person within the distant receiver goal10.11.27.111
is the IP tackle of the distant receiver goal/residence/remote2/Documents/test1.txt
is the identify and path to be saved for the file to be obtained.
Transfer Multiple Files Using the scp Command
Transferring a number of information one after the other generally is a tedious job for anybody. Instead, you should use the syntax beneath to trade information using the scp command on Linux:
scp <path_to_file_1> <path_to_file_2> <remote_receiver>@<remote_target_ip_address>:<path_to_store_in_remote_destination>
For instance – Suppose you might want to ship 4 information, together with test1.txt, test2.py, test3.sh, and test4.c, to a distant receiver, you should use the command beneath:
scp -P 22 test1.txt test2.py test3.sh test4.c remote1@10.10.11.113:/residence/remote_1/Documents
Let’s perceive how the command within the instance works and what it does:
-p 2222
is used to specify to attach through port 22test1.txt test2.py test3.sh test4.c
are the identify of the information to be transferredremote_1
is the username of the receiving system10.10.11.113
is the IP tackle of the receiver/residence/remote_1/Documents
refers back to the path to retailer the obtained file.

You may even use wildcards to ship a number of information with the identical extension as proven beneath:
scp <wildcard>.<extension> <remote_receiver>@<remote_target_ip_address>:<path_to_store_in_remote_destination>
For instance, if you might want to ship all .py information to a distant server, use the beneath scp command within the Linux Terminal:
scp *.py remote1@10.10.11.113:/residence/remote_1/
Here,
*.py
signifies all python informationremote1
is the receiver’s username10.10.11.113
is the receiver’s IP tackle/residence/remote_1/
is the placement to retailer the obtained file

Frequently Asked Questions
The Secure Copy Protocol or scp is a protocol that’s primarily based on SSH expertise and makes use of encryption and authentication for the trade of information between two hosts. This ensures full information safety and integrity between the 2 hosts.
Both SCP and SFTP protocols are at par when it comes to safety. The foremost benefit of using scp over SFTP is the quick switch speeds that are particularly helpful in high-latency networks.
Transfer Files Securely Using scp Command in Linux
At some cut-off date, each person must trade some information over the network. The scp command makes it simple to switch information securely and effectively even on a high-latency network. Surely, figuring out the varied scp choices and syntax could be helpful too. You also can consult with our guides on how to rename a file in Linux and the way to delete a file in Linux to additional enhance your file administration abilities on the OS. We hope this text helped you discover ways to use the scp command for transferring information on Linux computer systems. If you face any points whereas using the command, do tell us within the feedback part.
Check out more article on – How-To tutorial and latest highlights on – Technical News