Streamline File Transfer with scp Command in Linux

Streamline File Transfer with scp Command in Linux

Transferring files between servers in a Linux environment necessitates security considerations. Amidst various methods available, employing the scp (secure copy) command emerges as the optimal solution for secure file transfers. This article elucidates the utilization of scp for ensuring the secure transfer of files within your Linux infrastructure.

Understanding the scp Command in Linux

When transferring files over a secure network, the scp command proves invaluable. It stands for Secure Copy Protocol and facilitates the transfer of files and directories between two systems through an SSH (Secure Shell) connection. With SSH, file security is ensured as files are encrypted by default. Essentially, the scp command offers enhanced security compared to the cp command, as detailed in our Linux Terminal commands article.

Using the scp Command: Syntax & Options

Prior to file transfer via scp, familiarize yourself with the command’s syntax and options:

Syntax:

  • Specify the ‘source’ system for file/directory transfer.
  • Second, specify the ‘target’ system for transferring your files/directories.

If you omit either parameter, the scp command in Linux will search for the file and copy it locally. Some options to pair with the command include:

Options Destination
-P Specifies port to connect with the host system. If omitted, it’ll use port 22 by default.
-p Preserves modification times, access times, and modes from the original file when copied to the target system.
-r Copies entire directory to target system recursively.
-J Connects source system and destination system via a proxy system (jump host).
-3 When this flag is used, it copies files to both target system and local system.
-4 Forces scp command to use only IPv4 addresses.
-6 Forces scp command to use only IPv6 addresses.

Copying Files with the scp Command

  • Ensure ssh is installed
  • Have root access or sudo privileges

Copying Files from Local Host to Remote Target

To transfer a file from your local system to a remote target, use this syntax:

If this syntax seems complicated, an example will clarify:

scp test.txt remote2@139.144.11.105:/home/remote2/Documents/

  • test.txt is the file to transfer from the current local directory.
  • remote2 is the target system’s username.
  • 139.144.11.105 is the target system’s IP address.
  • /home/remote2/Documents/ is the destination directory on the target system.

Copy Files from Remote Host to Local Target

To transfer a file from a remote host to your local machine, use this scp command syntax on your Linux system:

For example, transfer a file named test.py from the remote server you’re working on using:

scp test@10.10.11.113:/home/test/test1.py ~/test1.py

  • test: remote host username.
  • 10.10.11.113: remote host IP address.
  • /home/test/test1.py: path of the file to be transferred from the remote host.
  • ~/test1.py: name of the file after transfer to the local machine stored in the home directory.

Transfer Files from One Remote Host to Another Remote Target

Using the scp command, you can transfer files between your local system and a remote machine, as well as between two remote systems. However, before transferring files between remote systems, it’s recommended to generate a private and public ssh key on the source system and store the public key in the destination system.

Users often encounter the “Host key verification failed” error when transferring files between two remote systems. To bypass this error, use an ssh-key as an additional measure. Here’s how:

1. Generate a public/private key pair on the source server with this command:

Streamline File Transfer with scp Command in Linux

2. For encryption, you can use “rsa,” the most commonly used algorithm, or any other preferred algorithm.

3. Choose where to store the SSH key. It can be any location or the default.

4. For the passphrase, enter anything or leave it blank.

5. Copy the public key to the destination server using the command below. This allows passwordless login via SSH.

Note: Passwordless login only works for the generated SSH key’s user.

6. After creating and storing the SSH key on the remote server, use this SCP command syntax to exchange files between two Linux systems:

For instance, to transfer a file named test.txt from one remote host to another, use:

To transfer files efficiently using the scp command on Linux, follow this syntax:

  • Specify the remote sender host user as ‘remote1’.
  • Provide the IP address of the remote sender host as ‘10.10.11.113’.
  • Enter the path to the file to be sent as ‘/home/test1/test.txt’.
  • Designate the remote receiver target user as ‘remote2’.
  • Supply the IP address of the remote receiver target as ‘10.11.27.111’.
  • Define the name and path for saving the received file as ‘/home/remote2/Documents/test1.txt’.

Streamlining File Transfer with scp Command

Streamlining file transfer on Linux is crucial to save time and effort. Rather than transferring files individually, streamline the process using the scp command with the syntax provided below:

For example, Suppose you need to send four files, including test1.txt, test2.py, test3.sh, and test4.c, to a remote receiver, you can use the command below:

scp -P 22 test1.txt test2.py test3.sh test4.c remote1@10.10.11.113:/home/remote_1/Documents

  • -p 2222 is used to specify to connect via port 22
  • test1.txt test2.py test3.sh test4.c are the name of the files to be transferred
  • remote_1 is the username of the receiving system
  • 10.10.11.113 is the IP address of the receiver
  • /home/remote_1/Documents refers to the path to store the received file.

You can even use wildcards to send multiple files with the same extension as shown below:

For example, if you need to send all .py files to a remote server, use the below scp command in the Linux Terminal:

Transfer all python files securely using scp command in Linux:

  • Use *.py to signify all python files.
  • Specify receiver’s username as remote1.
  • Indicate receiver’s IP address as 10.10.11.113.
  • Designate the location to store the received file as /home/remote_1/.

Secure File Transfer with scp Command in Linux

Every user needs to exchange files over the network at some point. The scp command facilitates secure and efficient file transfer, even on high-latency networks. Understanding various scp options and syntax can also prove useful. Refer to our guides on renaming and deleting files in Linux to enhance your file management skills. We trust this article aided your understanding of using scp for file transfer on Linux. If you encounter issues with the command, please inform us in the comments.

Frequently Asked Questions

Is SCP file transfer secure?

The Secure Copy Protocol (SCP) is based on SSH technology, utilizing encryption and authentication for file exchange between two hosts, ensuring data security and integrity.

Is SCP more secure than SFTP?

SCP and SFTP protocols offer comparable security. However, SCP boasts faster transfer speeds, particularly beneficial in high-latency networks.