How To Securely Transfer Files/Directories to and from Remote Linux Servers

Overview:

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 root@10.1.1.77:/root/
Transfer files from the local computer to the remote Linux server

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     root@10.1.1.77:/root/
OR
$scp -i  /path/to/mykey Downloads/certs_2023.zip root@10.1.1.77:/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 root@10.1.1.77:/root/scripts.zip ./
Transfer a file from a remote Linux server to the local computer.
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/*     root@10.1.1.77:/root/projects/
OR
$scp  -r  root@10.1.1.77:/root/*    ./files/
Recursively transfer all contents of a directory.

For more information and usage options of scp, check its man page like this:

$man scp
Conclusion

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.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Page Contents