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:
- The
scpcommand relies on the SSH protocol. - On the local computer where you're running the
scpcommand, only an SSH client is needed. - Linux, macOS, and Windows all come with the
scpcommand installed by default.
Remote Computer:
- However, on the remote server side you need
OpenSSH serverto accept incoming SSH connections. - So the OpenSSH server must be running on the 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
- The source is the file or folder you want to copy.
- The destination is where you want to copy it to.
Command Options:
-r: Recursively copy directories.-P port: Specifies the port number to connect to on the remote host (if it's not the default port 22).-i identity_file: Specifies the private key file to use for authentication.
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.