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.

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:
- Replace /path/to/local/file.txt with the full path to your file.
- Replace username with your username on the remote server.
- Replace remote_host with the server’s IP address or hostname.
- 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:
- Replace username, remote_host, and /path/to/remote/file.txt with your remote server details and file path.
- 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:
- Specify the local directory path and the remote server path.
- 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:
- Specify the remote directory path on the server.
- 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:
- Specify each file name separated by spaces.
- 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:
- Replace username1@remote_host1:/path/to/file with the source server details.
- 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:
- Replace 2222 with the SSH port of the remote server.
- 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:
- Set a bandwidth limit (e.g., 5000 Kbps).
- 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:
- Use -C to compress files during transfer.
- 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:
- Replace /path/to/private_key with the path to your SSH private key.
- 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
| Scenario | Command Example |
| Local to Remote File Transfer | scp /path/to/local/file.txt user@remote_host:/path/to/remote/ |
| Remote to Local File Transfer | scp user@remote_host:/path/to/remote/file.txt /path/to/local/ |
| Local to Remote Directory Transfer | scp -r /path/to/local/dir user@remote_host:/path/to/remote/ |
| Remote to Local Directory Transfer | scp -r user@remote_host:/path/to/remote/dir /path/to/local/ |
| Transfer Multiple Files | scp file1.txt file2.txt user@remote_host:/path/to/remote/ |
| Transfer Between Two Remote Servers | scp user1@host1:/path/to/file user2@host2:/path/to/destination |
| Specify SSH Port | scp -P 2222 /path/to/local/file.txt user@remote_host:/path/to/remote/ |
| Limit Bandwidth | scp -l 5000 /path/to/local/file.txt user@remote_host:/path/to/remote/ |
| Enable Compression | scp -C /path/to/local/file.txt user@remote_host:/path/to/remote/ |
| Use SSH Key Authentication | scp -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
Useful Links
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.
