SCP (Secure Copy Protocol) is a command-line tool used to transfer files and folders securely between a local machine and a remote server over an SSH (Secure Shell) connection.

Prerequisites

Before diving into SCP, ensure you have:

  1. Access to a Linux or UNIX-based system (SCP is pre-installed on most distributions)
  2. SSH access to the remote server
  3. Basic knowledge of the command line and file system navigation

Using SCP: The Basics

The general syntax for the SCP command is:

scp [options] [source] [destination]
  • options: Optional flags that modify SCP’s behavior
  • source: The file or directory to be copied
  • destination: The target location where the file or directory should be copied

Copying Files from Local to Remote

To copy a file from your local machine to a remote server, use the following format:

scp /path/to/local/file user@remote_host:/path/to/remote/directory

Replace /path/to/local/file with the local file’s path, user with your remote server’s username, remote_host with the server’s IP address or hostname, and /path/to/remote/directory with the target directory on the remote server.

Copying Files from Remote to Local

To copy a file from a remote server to your local machine, simply reverse the source and destination:

scp user@remote_host:/path/to/remote/file /path/to/local/directory

Copying Directories

To copy a directory, use the -r (recursive) option:

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

Advanced SCP Usage

Custom SSH Port

If your remote server uses a non-standard SSH port, use the -P option followed by the port number:

scp -P port_number /path/to/local/file user@remote_host:/path/to/remote/directory

Limiting Transfer Speed

To limit the transfer speed (in Kbps), use the -l option:

scp -l 1000 /path/to/local/file user@remote_host:/path/to/remote/directory

This example limits the transfer speed to 1000 Kbps.

Preserving File Attributes

To preserve file attributes (permissions, timestamps), use the -p option:

scp -p /path/to/local/file user@remote_host:/path/to/remote/directory

Verbose Output

For more detailed output, use the -v (verbose) option:

scp -v /path/to/local/file user@remote_host:/path/to/remote/directory

SCP is a powerful and versatile command-line tool for securely transferring files and folders between local machines and remote servers over SSH. By mastering its various options and features, you’ll streamline your file transfer tasks and enhance your cybersecurity practices.