Tutorial: Secure Copy (SCP) - Transfer Files Securely Over SSH#
SCP (Secure Copy) is a command-line utility that allows you to securely transfer files and directories between a local host and a remote host or between two remote hosts over a Secure Shell (SSH) connection. It provides a secure and encrypted way to transfer data, making it a valuable tool for remote file transfers and backups.
In this tutorial, you'll learn how to use SCP to transfer files between various scenarios:
- Copy files from your local machine to a remote server.
- Copy files from a remote server to your local machine.
- Copy files between two remote servers.
Before proceeding with the tutorial, make sure you have the following:
- Access to a local machine with SCP installed (SCP comes pre-installed on most Unix-based systems).
- Access to a remote server with SSH enabled.
The basic syntax of SCP is as follows:
scp [options] source destination
source: The file or directory you want to copy. It can be a local file/directory or a remote file/directory in the format
destination: The target location where the file/directory should be copied. It can be a local path or a remote path.
1. Copy local file to a remote server:#
scp /path/to/local/file.txt username@remote_host:/path/on/remote/server/
/path/to/local/file.txt with the local file you want to transfer and
username@remote_host with your remote server's SSH credentials. Also, specify the destination directory on the remote server where you want to copy the file.
2. Copy a remote file to your local machine:#
scp username@remote_host:/path/on/remote/server/file.txt /path/to/local/
username@remote_host with your remote server's SSH credentials, and specify the path of the remote file you want to copy. Also, specify the destination directory on your local machine where you want to save the file.
3. Copy a directory from local to remote server:#
scp -r /path/to/local/directory username@remote_host:/path/on/remote/server/
-r option to recursively copy a directory and its contents.
4. Copy a directory from remote to local machine:#
scp -r username@remote_host:/path/on/remote/server/directory /path/to/local/
-r option for recursive copying when transferring directories.
5. Copy files between two remote servers:#
scp username@remote_host1:/path/to/source/file.txt username@remote_host2:/path/to/destination/
username@remote_host2 with the respective remote servers' SSH credentials. Specify the source file on the first remote server and the destination directory on the second remote server.
SCP provides some useful options for different scenarios. Here are a few commonly used ones:
-P: Specify a custom SSH port.
-i: Use a specific identity file (private key) for SSH authentication.
-C: Compresses data during the transfer, reducing the transfer time.
-q: Suppress SCP's progress meter and non-error messages.
-p: Preserve file attributes, such as timestamps and permissions.