# Using the SCP Command in Linux

`scp` stands for Secure Copy Protocol. It lets you securely transfer files and directories between Linux hosts over an encrypted SSH connection, and unlike plain `cp`, it also verifies data integrity during the transfer. This guide covers the `scp` command syntax, its most useful options, and practical examples for copying files and directories between a local and a remote server, plus how to set up SSH key authentication for passwordless transfers. By the end, you'll have working `scp` workflows for transferring single files, whole directories, and tar archives between two Linux systems, both with and without a password prompt, following secure file transfer practices documented in [**Vultr Docs**](https://docs.vultr.com/how-to-use-the-scp-command-in-linux).

---

## Prerequisites

- Two Linux servers (one acting as local, one as remote) that you can reach over SSH
- SSH access configured on both servers
- Both servers updated to the latest packages

## The `scp` Command Syntax

```console
scp [options] [source] [destination]
```

In the command above:

- `[options]`: Accepts optional flags to modify the command's behavior
- `[source]`: Specifies the file or directory to copy
- `[destination]`: Sets the target location where you want to place the files

## Common `scp` Command Options

| Option | Description |
| --- | --- |
| `-P` | Specifies the port to connect on the remote host |
| `-p` | Preserves the original file's modification times, access times, and modes |
| `-r` | Recursively copies entire directories |
| `-q` | Quiet mode; suppresses non-error messages |
| `-C` | Enables compression |
| `-l` | Limits the bandwidth used during the transfer |
| `-v` | Verbose mode; displays detailed debugging messages |
| `-i` | Sets the private key location to use with a public key for authentication |

## Create Sample Directories and Files

**1\. Create three sample** `.txt` **files on your local server:**

```console
touch file1.txt file2.txt file3.txt
```

**2\. Create a sample** `dir` **directory on your local server:**

```console
mkdir dir
```

**3\. Create three sample** `.txt` **files on the remote server:**

```console
touch file4.txt file5.txt file6.txt
```

**4\. Create** `dir1` **and** `dir2` **sample directories on the remote server:**

```console
mkdir dir1 dir2
```

## Transfer Files and Directories Using SCP

**1\. Copy** `file1.txt` **from your local server to the remote server:**

```console
scp file1.txt linuxuser2@remote_server:/home/linuxuser2
```

This securely copies `file1.txt` to the remote user's home directory. By default, `scp` copies files to the user's home directory unless you specify another directory. Enter `yes` to accept the remote server's fingerprint key when prompted.

**2\. Copy** `file4.txt` **from the remote server to your local server:**

```console
scp linuxuser2@remote_server:/home/linuxuser2/file4.txt /home/linuxuser1/
```

**3\. Copy the** `dir` **directory from the local server to the remote server using the recursive** `-r` **option:**

```console
scp -r dir linuxuser2@remote_server:/home/linuxuser2/
```

This recursively copies the `dir` directory, including all files and subdirectories, into the `linuxuser2` home directory on the remote server.

**4\. Copy the** `dir1` **directory from the remote server to your local server's home directory:**

```console
scp -r linuxuser2@remote_server:/home/linuxuser2/dir1 /home/linuxuser1/
```

## Enable Passwordless Authentication Using an SSH Key

`scp` uses password authentication by default. SSH key-based authentication lets you transfer files without entering a password each time.

**1\. Generate a new SSH key pair on your local server:**

```console
ssh-keygen -t rsa -b 4096
```

Press Enter to accept the default private key file location and set an optional passphrase when prompted.

**2\. Copy your public key to the remote server:**

```console
ssh-copy-id linuxuser2@remote_server
```

Enter your remote user password when prompted to copy the public key.

**3\. Access the remote server using SSH and verify that you're not prompted for a password:**

```console
ssh linuxuser2@remote_server
```

## Transfer Files Using SCP with SSH Key Authentication

**1\. Copy** `file2.txt` **from your local server to the** `dir2` **directory on the remote server:**

```console
scp file2.txt linuxuser2@remote_server:/home/linuxuser2/dir2/
```

**2\. Copy the** `dir2` **directory from the remote server to your local server:**

```console
scp -r linuxuser2@remote_server:/home/linuxuser2/dir2 /home/linuxuser1
```

## Use SCP with Advanced Options

**1\. Preserve file attributes during transfer with the** `-p` **option:**

```console
scp -p file3.txt linuxuser2@remote_server:/home/linuxuser2
```

This copies `file3.txt` to the remote user's home directory while keeping the original timestamps and file permissions.

**2\. Enable compression during transfer with the** `-C` **option:**

```console
scp -C file1.txt linuxuser2@remote_server:/home/linuxuser2/dir2/
```

This copies `file1.txt` to the `dir2` directory and compresses the data during transfer to speed up the process.

**3\. Limit the transfer bandwidth in kilobits per second (Kbps) with the** `-l` **option:**

```console
scp -l 500 file2.txt linuxuser2@remote_server:/home/linuxuser2
```

This limits the transfer speed to `500` Kbps to avoid overusing network resources.

**4\. Display detailed debugging messages in verbose mode with the** `-v` **option:**

```console
scp -v file3.txt linuxuser2@remote_server:/home/linuxuser2
```

**5\. Specify a private key for authentication with the** `-i` **option:**

```console
scp -i /path/to/private_key file1.txt user@remote_server:/home/linuxuser2
```

Replace `/path/to/private_key` with your actual private key path. This is useful when you use multiple SSH keys and need a specific one to establish the connection. Run the following command in your home directory to view the default SSH private key location:

```console
ls -l /.ssh/
```

**6\. Transfer files using a specific SSH port with the** `-P` **option:**

```console
scp -P 22 file3.txt linuxuser2@remote_server:/home/linuxuser2
```

This is important when the SSH daemon runs on a custom port on the remote server.

**7\. Transfer archived files over SCP's underlying SSH connection using** `tar`**:**

```console
tar czf - dir | ssh linuxuser2@remote_server 'tar xzf - -C /home/linuxuser2/dir1'
```

This compresses the `dir` directory and streams the data over SSH to the remote server, where `tar xzf - -C` extracts it into the `dir1` directory for efficient, bandwidth-saving transfers.

## Next Steps

- Explore the `find` command in Linux to quickly locate files before transferring them
- Automate recurring transfers with a cron job that runs `scp` on a schedule
- Combine `scp` with `rsync` for incremental transfers that only copy changed files
- Harden SSH access further by disabling password authentication once key-based login is confirmed working

For the full guide with additional tips, visit the original article on [**Vultr Docs**](https://docs.vultr.com/how-to-use-the-scp-command-in-linux).
