Master Secure Copy Protocol in Linux for Fast & Secure File Transfers

Master Secure Copy Protocol in Linux for Fast & Secure File Transfers
06/02/2025 •

Master Secure Copy Protocol in Linux

Here’s a guide to using SCP (Secure Copy Protocol) in Linux for 10 of the most common scenarios, including step-by-step instructions and detailed command options.

Master Secure Copy Protocol in Linux

1. Copy a File from Local to Remote Server

Scenario: You have a file on your local machine and want to copy it to a remote server.

Command:

      scp /path/to/local/file.txt username@remote_host:/path/to/remote/directory
    

Steps:

  1. Replace /path/to/local/file.txt with the full path to your file.
  2. Replace username with your username on the remote server.
  3. Replace remote_host with the server’s IP address or hostname.
  4. Replace /path/to/remote/directory with the remote directory where you want to place the file.

Master Secure Copy Protocol in Linux for Fast & Secure File Transfers


2. Copy a File from Remote Server to Local Machine

Scenario: You have a file on a remote server and want to bring it to your local machine.

Command:

      scp username@remote_host:/path/to/remote/file.txt /path/to/local/directory
    

Steps:

  1. Replace username, remote_host, and /path/to/remote/file.txt with your remote server details and file path.
  2. Replace /path/to/local/directory with the directory on your local machine where you want to save the file.

Master Secure Copy Protocol in Linux for Fast & Secure File Transfers


3. Copy a Directory from Local to Remote Server

Scenario: You need to copy an entire directory from your local machine to a remote server.

Command:

      scp -r /path/to/local/directory username@remote_host:/path/to/remote/directory
    

Explanation:

  • -r: The recursive option to copy directories and all their contents.

Steps:

  1. Specify the local directory path and the remote server path.
  2. Ensure permissions on the remote server allow you to write to the specified location.

Master Secure Copy Protocol in Linux for Fast & Secure File Transfers


4. Copy a Directory from Remote Server to Local Machine

Scenario: You want to download an entire directory from a remote server to your local machine.

Command:

      scp -r username@remote_host:/path/to/remote/directory /path/to/local/directory
    

Steps:

  1. Specify the remote directory path on the server.
  2. Define the local path where the directory will be saved.

Master Secure Copy Protocol in Linux for Fast & Secure File Transfers


5. Copy Multiple Files at Once

Scenario: You want to transfer multiple files to a remote server.

Command:

      scp file1.txt file2.txt username@remote_host:/path/to/remote/directory
    

Steps:

  1. Specify each file name separated by spaces.
  2. Define the remote server path as the destination.

Master Secure Copy Protocol in Linux for Fast & Secure File Transfers


6. Copy Files Between Two Remote Servers from Local Machine

Scenario: You need to transfer files between two remote servers without saving them locally.

Command:

      scp username1@remote_host1:/path/to/file username2@remote_host2:/path/to/destination
    

Steps:

  1. Replace username1@remote_host1:/path/to/file with the source server details.
  2. Replace username2@remote_host2:/path/to/destination with the target server details.

Master Secure Copy Protocol in Linux for Fast & Secure File Transfers


7. Specify SSH Port for SCP

Scenario: If the remote server is configured to use a non-standard SSH port, specify it with SCP.

Command:

      scp -P 2222 /path/to/local/file.txt username@remote_host:/path/to/remote/directory
    

Explanation:

  • -P: Specifies the SSH port (default is 22).

Steps:

  1. Replace 2222 with the SSH port of the remote server.
  2. Define the file and directory paths as needed.

Master Secure Copy Protocol in Linux for Fast & Secure File Transfers


8. Limit Bandwidth for SCP Transfers

Scenario: You want to control the bandwidth used by SCP to avoid overloading the network.

Command:

      scp -l 5000 /path/to/local/file.txt username@remote_host:/path/to/remote/directory
    

Explanation:

  • -l: Limits the bandwidth in kilobits per second (Kbps).

Steps:

  1. Set a bandwidth limit (e.g., 5000 Kbps).
  2. Define your file paths and other details.

Master Secure Copy Protocol in Linux for Fast & Secure File Transfers


9. Enable Compression for Faster Transfer

Scenario: Compressing files during transfer can increase speed, especially for large files.

Command:

      scp -C /path/to/local/file.txt username@remote_host:/path/to/remote/directory
    

Explanation:

  • -C: Enables compression for faster data transfer.

Steps:

  1. Use -C to compress files during transfer.
  2. Replace file paths as needed for your source and destination.

Master Secure Copy Protocol in Linux for Fast & Secure File Transfers


10. Use SSH Key Authentication with SCP

Scenario: For secure, passwordless transfers, use SSH keys instead of passwords.

Command:

      scp -i /path/to/private_key /path/to/local/file.txt username@remote_host:/path/to/remote/directory
    

Explanation:

  • -i: Specifies the private key file for authentication.

Steps:

  1. Replace /path/to/private_key with the path to your SSH private key.
  2. Ensure the server is configured to accept your public key for authentication.

Master Secure Copy Protocol in Linux for Fast & Secure File Transfers


SCP Command Options Overview

Here are some commonly used SCP command options for added functionality:

  • -r: Recursively copy entire directories.
  • -C: Enable compression to speed up transfers.
  • -i: Use an identity (private key) file for SSH key authentication.
  • -P: Specify an alternative SSH port (other than the default port 22).
  • -l: Limit bandwidth in Kbps to control network usage.

Master Secure Copy Protocol in Linux for Fast & Secure File Transfers


Summary of SCP Scenarios

ScenarioCommand Example
Local to Remote File Transferscp /path/to/local/file.txt user@remote_host:/path/to/remote/
Remote to Local File Transferscp user@remote_host:/path/to/remote/file.txt /path/to/local/
Local to Remote Directory Transferscp -r /path/to/local/dir user@remote_host:/path/to/remote/
Remote to Local Directory Transferscp -r user@remote_host:/path/to/remote/dir /path/to/local/
Transfer Multiple Filesscp file1.txt file2.txt user@remote_host:/path/to/remote/
Transfer Between Two Remote Serversscp user1@host1:/path/to/file user2@host2:/path/to/destination
Specify SSH Portscp -P 2222 /path/to/local/file.txt user@remote_host:/path/to/remote/
Limit Bandwidthscp -l 5000 /path/to/local/file.txt user@remote_host:/path/to/remote/
Enable Compressionscp -C /path/to/local/file.txt user@remote_host:/path/to/remote/
Use SSH Key Authenticationscp -i /path/to/private_key /path/to/local/file.txt user@remote_host:/path/to/remote/

By following these detailed SCP commands and options, you can securely transfer files and directories in various scenarios, achieving efficient, secure data transfers across different networked systems.

Master Secure Copy Protocol in Linux for Fast & Secure File Transfers


https://ubuntu.com/tutorials

https://sanchitgurukul.com/tutorials-cat

Master Secure Copy Protocol in Linux for Fast & Secure File Transfers


Master Secure Copy Protocol in Linux for Fast & Secure File Transfers

This article provided insights on the topic. For latest updates and detailed guides, stay connected with Sanchit Gurukul.

Disclaimer: This article may contain information that was accurate at the time of writing but could be outdated now. Please verify details with the latest vendor advisories or contact us at admin@sanchitgurukul.com.

Discover more from

Subscribe now to keep reading and get access to the full archive.

Continue reading