---
title: How to Use the SCP Command in Linux
url: https://docs.vultr.com/how-to-use-the-scp-command-in-linux
description: Learn how to securely transfer files between local and remote systems using the SCP command in Linux with step-by-step examples and essential syntax options.
publish_date: 2024-10-18T13:00:47.577305Z
last_updated: 2024-10-18T13:00:47.577305Z
author: Vishwas Agrawal
header_image: https://docs.vultr.com/public/doc-assets/1973/1c95d1b4-f40a-4403-b578-5ccb4be74a07.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

## Introduction

`scp` stands for Secure Copy Protocol. The `scp` command in Linux lets you securely transfer files between hosts using an encrypted SSH (Secure Shell) connection. Unlike regular copy commands such as `cp`, `scp` also ensures data integrity during transfers.

This article explains how to use the `scp` command to effectively transfer files and directories in Linux.

## Prerequisites

Before you begin:

* Deploy two [Ubuntu 24.04 instances](https://www.vultr.com/servers/ubuntu/) on Vultr and enable the `limited user login` option to use as your `local` and `remote` workstations respectively.
* [Access both instances using SSH](https://www.vultr.com/docs/how-to-use-ssh-with-vultr-servers/).
* [Update the instances](https://www.vultr.com/docs/how-to-update-a-vultr-cloud-server/).

## The `scp` Command Syntax

The following is a basic `scp` command syntax:

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

In the above `scp` command:

* `[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.

## Use Most Common `scp` Command Options

The following are the most 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

Follow the steps below to create sample directories and files to test the `scp` command in Linux.

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

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

1. Create a sample `dir` directory on your local server.

    ```console
    $ mkdir dir
    ```

1. Create three sample `.txt` files on the remote server.

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

1. Create `dir1` and `dir2` sample directories on the remote server.

    ```console
    $ mkdir dir1 dir2
    ```

## Transfer Files and Directories Using SCP

Follow the steps below to transfer files and directories using the `scp` command in Linux.

1. Copy the `file1.txt` file from your local server to the remote server.

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

    The above command securely copies `file1.txt` from your local server to the remote server user's home directory. By default, the SCP command copies files to the user's home directory unless you specify another directory.

    Output:

    ![Copy file to remote server](https://docs.vultr.com/public/doc-assets/1973/79bc5eec-6280-4b8b-9334-9e6e6bc5044e.png)

    Enter `yes` to accept the remote server's fingerprint key when prompted.

1. Copy `file4.txt` from the remote server to your local server.

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

    Output:

    ![Copy file from remote server](https://docs.vultr.com/public/doc-assets/1973/e9b29f47-fff8-4784-9a6e-3843582cadd7.png)

1. 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/
    ```

    The above command uses the `-r` option to recursively copy the `dir` directory from the local server to the `linuxuser2` home directory on your remote server including all files and subdirectories.

    Output:

    ![Copy directory to remote server](https://docs.vultr.com/public/doc-assets/1973/c8cd456d-a422-496c-9772-f2ce4ff871c5.png)

1. Copy the `dir1` directory from the remote server to your local server user's home directory.

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

    Output:

    ![Copy directory from remote server](https://docs.vultr.com/public/doc-assets/1973/14cca37d-eca8-4713-a017-340328b6b47c.png)

   You can explore the [find command in Linux](https://docs.vultr.com/how-to-use-the-find-command-in-linux) to quickly locate files on your system.

## Enable Passwordless Authentication Using an SSH Key

The SCP command uses password authentication by default to copy and transfer files. SSH key-based authentication enables passwordless authentication when transferring files using the SCP command. Follow the steps below to generate a new SSH key to use with the SCP command.

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

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

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

    Output:

    ![Generate SSH key pair](https://docs.vultr.com/public/doc-assets/1973/76c7ca66-808f-4c74-8670-1c9b3fa818dc.png)

1. 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.

    Output:

    ![Copy SSH public key](https://docs.vultr.com/public/doc-assets/1973/ce780535-42d3-47b7-acc4-b219a4bce7aa.png)

1. Access the remote server using SSH and verify that you're not prompted to enter a password.

    ```console
    $ ssh linuxuser2@remote_server
    ```

    Output:

    ![Verify passwordless login](https://docs.vultr.com/public/doc-assets/1973/07bb1bd5-3963-4e76-93da-c6df968762ce.png)

## Transfer Files Using SCP with SSH Key Authentication

Follow the steps below to transfer files using the `scp` command 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/
    ```

    The above command securely copies the `file2.txt` file from your local server to `dir2` on the remote server.

    Output:

    ![Copy file without password](https://docs.vultr.com/public/doc-assets/1973/bc6987a5-98f2-4675-b610-383ea74eac70.png)

1. Copy the `dir2` directory from the remote server to your local server.

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

    Output:

    ![Copy directory without password](https://docs.vultr.com/public/doc-assets/1973/e498c68f-f51e-4080-901c-d398d301bfb6.png)

## Use the SCP Command in Linux with Advanced Options

1. Copy `file3.txt` to the `dir2` directory on the remote server and preserve the original file attributes using the `-p` option.

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

    The above command uses the `-p` option to copy `file3.txt` to the `dir2` directory while keeping the original file attributes including the original timestamps and file permissions.

    Output:

    ![Preserve file modification time](https://docs.vultr.com/public/doc-assets/1973/83168a9e-7698-48c5-9ebc-31b947656a36.png)

1. Copy `file1.txt` to the remote server and enable compression using the `-C` option.

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

    The above command copies `file1.txt` to the `dir2` directory and uses the `-C` option to compress data during the transfer to speed up the process.

    Output:

    ![Enable transfer compression](https://docs.vultr.com/public/doc-assets/1973/f56888a1-323f-476b-9a39-23ef5dbf1e9f.png)

1. Copy `file2.txt` and limit the bandwidth in kilobits per second (Kbps) using the `-l` option.

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

    The above command uses the `-l` option to limit the transfer speed to `500` Kbps and avoid overusing network resources while transferring the file to the remote server.

    Output:

    ![Limit transfer bandwidth](https://docs.vultr.com/public/doc-assets/1973/cd2e1af0-6ec1-4619-ad83-cc78c447db15.png)

1. Display detailed debugging messages in verbose mode using the `-v` option.

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

    Output:

    ![Display verbose debugging](https://docs.vultr.com/public/doc-assets/1973/b6afc54d-0ff8-4d70-93dc-719f367bb8aa.png)

1. Specify a private key for authentication using the `-i` option. Replace `/path/to/private_key` with your actual private key path.

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

    The above command uses the `-i` option to specify a private key file for SSH authentication. This is useful when using multiple SSH keys and requires a specific key to establish a connection.

    Output:

    ![Use specific identity file](https://docs.vultr.com/public/doc-assets/1973/9985ba94-696e-49eb-a098-a960d129dd57.png)

    Run the following command in your user home directory to view the default SSH private key location.

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

    Output:

    ![SSH key location](https://docs.vultr.com/public/doc-assets/1973/93fa8e85-feee-4b7f-8347-2d9be1ac760c.png)

1. Copy `file3.txt` to the `/dir2` remote server directory using a specific SSH port to transfer the files.

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

    The above command uses the `-P` option to transfer files using the default SSH port `22`. This is important when the SSH daemon runs on a custom port on the remote server.

    Output:

    ![SCP using custom port](https://docs.vultr.com/public/doc-assets/1973/580cb30e-250f-491d-957e-bf5584047ceb.png)

1. Transfer archived files using `tar` and the `scp` commands. For example, compress the `dir` directory and transfer the archived file to the remote server.
  
    ```console
    $ tar czf - dir | ssh linuxuser2@remote_server 'tar xzf - -C /home/linuxuser2/dir1'
    ```

    The above command compresses the `dir` directory and copies the data over SSH to the remote server. The `tar xzf - -C` command extracts the files into the `dir1` directory in the user home directory to enable advanced compression and efficient file transfers.

    Output:

    ![Transfer directory with tar](https://docs.vultr.com/public/doc-assets/1973/e610abcf-3730-4b88-93c5-62fa4dd89331.png)

## Conclusion

You have used the `scp` command in Linux, its syntax, options, and practical examples. Additionally, you've set up SSH key-based authentication, allowing you to transfer files and directories more efficiently without a password. The `scp` command allows you to securely and efficiently transfer files between hosts by combining various command options in Linux.
