How to Use the Command 'scp' (with Examples)
The scp
command stands for Secure Copy Protocol and is used for securely transferring files between hosts over a network. It leverages SSH (Secure Shell) for data transfer, ensuring that the copied files remain encrypted end-to-end. This makes it a preferred choice for transferring sensitive data over potentially insecure networks.
Use case 1: Copy a local file to a remote host
Code:
scp path/to/local_file remote_host:path/to/remote_file
Motivation:
One of the most common scenarios when working with remote servers is transferring files from your local machine to the server. This could be anything from website assets to configuration files that you need to deploy or test on the server.
Explanation:
path/to/local_file
: This represents the path to the file on your local machine that you intend to copy.remote_host
: The domain name or IP address of the remote server you’re connecting to.path/to/remote_file
: The destination path on the remote server where you want the file to be copied.
Example Output:
local_file 100% 10MB 500KB/s 00:20
This output indicates that the local file was successfully uploaded to the remote host.
Use case 2: Use a specific port when connecting to the remote host
Code:
scp -P port path/to/local_file remote_host:path/to/remote_file
Motivation:
Sometimes, the default SSH port (typically port 22) may be changed for security reasons or to run multiple SSH services. In such cases, specifying the exact port is crucial to ensure the connection succeeds.
Explanation:
-P port
: Specifies the port number on the remote host to which you want to connect.path/to/local_file
,remote_host:path/to/remote_file
: As described in Use Case 1.
Example Output:
local_file 100% 10MB 500KB/s 00:20
This indicates that the file transfer was successful using a specified port.
Use case 3: Copy a file from a remote host to a local directory
Code:
scp remote_host:path/to/remote_file path/to/local_directory
Motivation:
Copying files from a remote host to your local machine is an essential task for backing up data or retrieving results from computations or logging files.
Explanation:
remote_host:path/to/remote_file
: Specifies the remote server and the file path from which to fetch the file.path/to/local_directory
: The destination path on your local machine where the file will be stored.
Example Output:
remote_file 100% 12MB 600KB/s 00:20
This output shows that the remote file has been successfully fetched to the local directory.
Use case 4: Recursively copy the contents of a directory from a remote host to a local directory
Code:
scp -r remote_host:path/to/remote_directory path/to/local_directory
Motivation:
When dealing with multiple files or nested folders, it becomes efficient to transfer entire directories. Recursive copying allows you to maintain the structure and content of the directory.
Explanation:
-r
: The recursive flag is used to copy directories and their contents.remote_host:path/to/remote_directory
,path/to/local_directory
: As described previously but applied to directories.
Example Output:
files_in_directory 100% 50MB 700KB/s 01:12
Successfully indicates that all files and subdirectories have been replicated locally.
Use case 5: Copy a file between two remote hosts transferring through the local host
Code:
scp -3 host1:path/to/remote_file host2:path/to/remote_directory
Motivation:
This scenario arises when a user must transfer files from one remote host to another but does not have direct connectivity between the two. The local host acts as a bridge.
Explanation:
-3
: Enables copying files between two remote hosts via the local host.host1:path/to/remote_file
andhost2:path/to/remote_directory
: Specify source and destination paths on the respective remote hosts.
Example Output:
remote_file 100% 15MB 500KB/s 00:30
This demonstrates a successful transfer involving two remote hosts.
Use case 6: Use a specific username when connecting to the remote host
Code:
scp path/to/local_file remote_username@remote_host:path/to/remote_directory
Motivation:
Different users can have varying access rights on the same server. Using a specific username ensures that you are authenticated correctly and have the correct permissions.
Explanation:
remote_username@
: Specifies the user account on the remote host.path/to/local_file
,remote_host:path/to/remote_directory
: As described previously.
Example Output:
local_file 100% 8MB 400KB/s 00:18
The file transfer was authorized under the specified user account.
Use case 7: Use a specific SSH private key for authentication with the remote host
Code:
scp -i ~/.ssh/private_key path/to/local_file remote_host:path/to/remote_file
Motivation:
SSH key-based authentication is often used for its security and ease of automation. Using a specific key file can be necessary when multiple keys are present or required by the server policy.
Explanation:
-i ~/.ssh/private_key
: Specifies the file that contains the private key for SSH authentication.path/to/local_file
,remote_host:path/to/remote_file
: As noted previously.
Example Output:
local_file 100% 10MB 500KB/s 00:21
Indicates a successful transfer using the specified SSH key.
Use case 8: Use a specific proxy when connecting to the remote host
Code:
scp -J proxy_username@proxy_host path/to/local_file remote_host:path/to/remote_file
Motivation:
Proxies can be necessary when direct access to the desired network is restricted by firewalls or network topology, allowing you to use an intermediate machine that has the required access.
Explanation:
-J proxy_username@proxy_host
: Specifies the jump host (proxy) through which to connect to the remote.path/to/local_file
,remote_host:path/to/remote_file
: As discussed before.
Example Output:
local_file 100% 14MB 550KB/s 00:24
Denotes a successful transfer using the specified proxy.
Conclusion:
The scp
command is a robust tool for anyone dealing with network file transfers, offering versatile options like specifying ports, usernames, encryption keys, and proxies. Mastering scp
can significantly improve your workflow by providing a reliable and secure method to manage files across systems.