How To Securely Transfer Files/Directories to and from Remote Linux Servers
This guide shows how to securely transfer files to and from remote Linux servers using the
scp command-line tool.
What is scp(or secure copy)?
SCP is a command line tool used to securely copy files between hosts on a local network or over the Internet. scp employs ssh for data transfer and as a result, it also uses the same authentication methods that ssh supports such as password-based authentication and public key-based(or passwordless) authentication. Importantly, scp provides the same security as ssh by encrypting the communication.
Also check out: How to Connect to a Remote Server Using SSH
The syntax for using scp is as follows:
$scp options source destination
Securely Copy Files/Directories To Remote Linux Servers
This example shows how to copy the file cert_2023.zip from the Downloads directory on the local computer to the /root directory on the remote server whose IP is 10.1.1.77. If the ssh daemon(sshd) on the remote server is configured to use a password authentication method, you will be prompted to enter the password of the user.
For this example, the remote server is configured to use public key-based(passwordless) ssh authentication.
$scp Downloads/certs_2023.zip email@example.com:/root/
To explicitly specify the identity(or private) key on the command line, use the
-i option like this:
$scp -i .ssh/mykey Downloads/certs_2023.zip firstname.lastname@example.org:/root/ OR $scp -i /path/to/mykey Downloads/certs_2023.zip email@example.com:/root/
Securely Copy Files/Directories From Remote Linux Servers
You can also securely transfer files from a remote server to the local machine. The following is an example where the source server is 10.1.1.77, the username is root, and the file path is /root/scripts.zip.
And the destination is ./, the path on the local machine, which is the current working directory:
$scp firstname.lastname@example.org:/root/scripts.zip ./
Recursively Copy Contents of a Directory Using scp
To copy the contents of a directory and all sub-directories in it, use the -r option which implies recursively copying the entire directory:
$scp -r Documents/projects/* email@example.com:/root/projects/ OR $scp -r firstname.lastname@example.org:/root/* ./files/
For more information and usage options of scp, check its man page like this:
scp is a popular tool to securely transfer files between two remote Linux computers, for example, your local computer and a remote server in the cloud. In this guide, we have covered different ways of using scp to transfer files and directories to and from remote Linux computers. If you have any questions concerning scp or this guide, use the feedback form below to share it.