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 from the Downloads directory on the local computer to the /root directory on the remote server whose IP is 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/ 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/     root@
$scp -i  /path/to/mykey Downloads/ 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, the username is root, and the file path is /root/

And the destination is ./, the path on the local machine, which is the current working directory:

$scp root@ ./
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@
$scp  -r  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

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