Config Server Firewall

SCP Command: Securely Copy Files on Linux, Windows, and macOS

In this tutorial, you will learn how to use the scp command to securely copy files and directories between local and remote systems using SSH.

System Requirements for SCP

Local Computer:

Remote Computer:

If your Linux system doesn’t have an SSH server, click this link for instructions on how to install the OpenSSH Server.

You can install the OpenSSH Server on Windows as well.

SCP Command Syntax

The basic syntax of the scp command looks like this:

scp [options] source destination

Command Options:

Remote Path Format

When specifying a remote source or destination, the format is as follows:

user_name@IP-Address:path

Example:

root@192.168.1.10:/var/www/

The user@ portion is optional if your local username is the same as the remote username. If it’s missing, the scp command will use your local user account name.

Examples: Copying Files from Local to Remote

Copy file1.txt from the local computer to the /tmp directory on the remote server at 192.168.1.10:

scp file1.txt user1@192.168.1.10:/tmp/

Copy file1.txt and file2.txt to the remote server at the same time:

scp file1.txt file2.txt user1@192.168.1.10:/tmp/

Examples: Copying Files from Remote to Local

Download /etc/passwd from the remote computer and save it in the C:\data\ folder on your Windows computer:

scp user1@192.168.1.10:/etc/passwd C:\data\

Download multiple files at once from the remote server:

scp user1@192.168.1.10:/tmp/file1.txt /tmp/file2.txt /backup/

Note: The above syntax for downloading multiple remote files might not work as expected in the Windows version of the scp command. It works in a Linux Bash shell.

Copying Directories

When copying a directory, you must use the -r option to copy all files and sub-directories recursively.

Examples:

scp -r /var/www/ ubuntu@192.168.1.10:/tmp/
scp -r ubuntu@192.168.1.10:/var/www/ /backup

Copying Between Two Remote Servers

It's also possible to copy files between two remote computers using scp, but the data is transferred through your local machine.

Example:

scp -r user1@192.168.1.10:/var/www/ user2@192.168.1.20:/backup/

Using an Identity File (Private Key)

And the last example we’ll look at is how to copy files using an identity file or private key, if you’re using passwordless authentication.

The command option to specify the identity file is -i.

scp -r -i srv01.pem ubuntu@192.168.1.10:/var/www/ /backup/

And that concludes this tutorial on the Linux scp command. If you found it helpful, subscribe to @RealClearComputing for more Linux tutorials.