Home > Network Management > scp

Mastering the scp Command

This guide covers the `scp` command, which uses the Secure Copy Protocol (SCP) to securely copy files between local and remote hosts on Linux. This tool allows you to easily and securely transfer files over the network.

Main Options

Combine various options of the `scp` command to set up file transfer tasks.

1. Basic Transfer

2. Transfer Options

Generated command:

Try combining the commands.

Description:

`scp` Executes the command.

Combine the above options to virtually execute commands with AI.

Understanding Syntax and Paths

The `scp` command uses two key arguments: 'source' and 'destination', which can be local or remote paths. Remote paths are expressed in the format `[user@]host:path`.

SCP Syntax

`scp [options] [source] [destination]` - **Source**: The original file or directory to copy. - **Destination**: The target file or directory where the file will be copied.

Path Specification Method

How to specify local and remote paths distinctly.

  • Local Path: A typical file system path (e.g., `/home/user/document.txt` or `.`)
  • Remote Path: In the format `[username@]hostname_or_IP:/remote/path` (e.g., `user@example.com:/var/www/html/`)

Authentication and Security

`scp` is based on the SSH protocol, so files are encrypted and secure during transfer. Accessing remote hosts requires authentication via username/password or SSH keys, similar to SSH.

Usage Examples

Practice file transfer tasks through practical examples of the `scp` command.

Copy Local File to Remote Server

scp report.txt user@myserver.com:/home/user/

Copies the `report.txt` file from the current directory to the `/home/user/` directory on the remote server `myserver.com`.

Copy File from Remote Server to Local

scp user@myserver.com:/var/log/syslog .

Copies the `/var/log/syslog` file from the remote server `myserver.com` to the current local directory (`.`).

Recursively Copy Local Directory to Remote Server

scp -r my_project user@myserver.com:/var/www/

Copies the entire `my_project` directory from the current directory to the `/var/www/` directory on the remote server `myserver.com`.

Copy File Using Specific Port

scp -P 2222 backup.tar.gz user@myserver.com:/backups/

Copies the `backup.tar.gz` file to a remote server using SSH port 2222.

Copy File Using Identity Key File

scp -i ~/.ssh/my_key data.sql user@myserver.com:/db_dumps/

Copies the `data.sql` file to the remote server using the specified SSH private key (`~/.ssh/my_key`).


Same category commands