How to Use the SCP Command in Linux

Updated on October 18, 2024
How to Use the SCP Command in Linux header image

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:

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

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

    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/
    

    Output:

    Copy file from remote server

  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/
    

    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

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

    You can explore 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 Enter to accept the default private key file location and set an optional passphrase when prompted.

    Output:

    Generate SSH key pair

  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.

    Output:

    Copy SSH public key

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

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

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

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

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

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

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

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

    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

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

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

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.