scp Command in Linux (With Examples)

The scp (Secure Copy) command allows Linux users to securely copy files and directories across multiple systems using an SSH (Secure Shell) connection. When transferring files and directories using this command, they are encrypted, as well as any passwords associated with them. As such, the scp command is one of the most secure ways to transfer data between systems.

Note: For this guide, we’re using three separate systems for this article:

  • techobservatory-local-serverZorin OS 16 (mtr-Zorin) — the user on this server is mtravisrose.
  • techobservatory-remote-server-1 — Debian 11 (mtr-debian) — the user on this server is techobservatory.
  • techobservatory-remote-server-2Xubuntu LTS 22.04 (mtr-xubuntu) — the user on this server is mtravisrose.

Although we’re using three different Linux distros, all Linux commands featured in this article are distro-independent, meaning they will work on any Linux distro.

Copying Files and Directories Using the scp Command

There are three scenarios where Linux users typically use the scp command:

  • To copy from your system to a remote server.
  • To copy from a remote server to your system.
  • To copy from a remote server to another remote server.

scp Command Purpose, Syntax, and Options

With the scp command, you can securely copy a file or directory via SSH. The syntax for the scp command is:

# scp [options] [source server]/dir/file(s) ... [destination server]/dir/file(s)

The scp command comes with several powerful options. Some of the most commonly used options include:

  • -P — Specify server SSH port (note the upper case P).
  • -q — Quiet mode, don’t display progress or messages.
  • -p — Preserve permissions, modes, and access time of files.
  • -q — Quiet mode. Don’t display progress or messages.
  • -C — Compress the data during transmission (note the upper case C).
  • -r — Recursive. Include all subdirectories and their contents.
  • -l — Limit the bandwidth while copying.
  • -v — Verbose output.
  • -3 — If copying between two remote hosts, the data is transferred through the local host. This option disables the progress meter and selects batch mode for the second host.

In our three examples, we’ll be using the -C option. The -C option is always recommended to speed up the transfer.

Note: to successfully use scp to copy files, it’s important to have an SSH server installed, enabled, and port 22 (SSH) open on the target system.

Using scp to Copy to a Remote Server

  1. Log in to the local server (In this instance, mtr-Zorin).
  2. Launch the terminal.
  3. Securely copy Download/tar.zip from the local server to mtr-xubuntu in the /home/mtravisrose/Downloads directory. Enter the password for the remote server when prompted. Enter the following command to accomplish this:
# scp -C Downloads/sample.tar [email protected]:Downloads
Securely copy tar.zip from mtr-Zorin to mtr-xubuntu.
Securely copy tar.zip from mtr-Zorin to mtr-xubuntu.

Note that sample.tar is on mtr-xubuntu in the /home/mtravisrose/Downloads directory.

sample.tar file on mtr-xubuntu in the /home/mtravisrose/Downloads directory.
sample.tar file on mtr-xubuntu in the /home/mtravisrose/Downloads directory.

Using scp to Copy from a Remote Server to the Local Server

  1. Login to the local server (in this case, mtr-Zorin).
  2. Launch the terminal.
  3. Securely copy Download/tar.zip from mtr-xubuntu to the /home/mtravisrose/Downloads directory on the local server. Enter your password for your user on the remote server when prompted. Enter the following command to accomplish this:
# scp -C [email protected]:/home/mtravisrose/Downloads/sample.tar /home/mtravisrose/Downloads
Securely copy tar.zip from mtr-xubuntu to the local server.
Securely copy tar.zip from mtr-xubuntu to the local server.

Using scp to Copy from a Remote Server to a Remote Server

  1. Login to the source server (in this case, mtr-debian).
  2. Launch the terminal.
  3. Securely copy /home/mtravisrose/Downloads/tar.zip from mtr-xubuntu to the /home/techobservatory/Downloads directory on mtr-debian. Enter your passwords for your user on both remote servers when prompted. Enter the following command to accomplish this:
# scp -3 [email protected]:/home/mtravisrose/Downloads/sample.tar [email protected]:/home/techobservatory/Downloads

 

Securely copy tar.zip from mtr-xubuntu to the mtr-debian.
Securely copy tar.zip from mtr-xubuntu to the mtr-debian.

Note that sample.tar is on mtr-debian in the /home/techobservatory/Downloads directory.

sample.tar file on mtr-debian in the /home/mtravisrose/Downloads directory.
sample.tar file on mtr-debian in the /home/mtravisrose/Downloads directory.

Commands Used in this Guide

  • hostname – display the hostname of the system.
  • ls – list directory structures.
  • pwd – print working directory.
  • scp – secure file/directory copy.

Was this article helpful?

Leave a Comment