How to copy files over SSH

In the era of cloud and managed services, developers focus on their code, and infrastructure is managed by various tools and services. Other developers manage them manually to deploy applications, test networking features, host a personal blog, etc...

This requires performing some manipulations and copying files from one server to another. You may be wondering what the requirement is for that; here are some use cases for copying files:

  • Backup application data to another server.
  • Deploy an application source code.
  • Pull logs for a running application in production.
  • Update the application configuration file running on a remote server.
  • And many other use cases.

In this post, we will see how to transfer files via SSH.

Prerequisites

You will need a computer running on a UNIX system (Any Linux distribution or MacOS) to follow this tutorial. If you are on Windows, install an SSH client such as Putty or use the WSL.

You will also need a remote server. You can buy a VPS on Hetzner, which provides affordable servers with good performance.

The server running this blog and all my side projects are hosted here. Use my referral link to get €⁠20 after you sign up.

The minimal configuration of a VPS server to host a Web Application
You just bought a fresh VPS to host your Web application but don’t know our the configure it in order to be ready to make your app accessible through Internet? We will cover how to configure it in this tutorial.

Copying files from local machine to remote machine

In this scenario, you have a file on your local computer you want to copy to the server for a particular use case.

💡
The local machine can also refer to another remote server having a file we want to copy elsewhere. 

On your computer, you have Node.js application source code zipped that you want to copy the remote server, then login to the server, unzip it, and run it. You might think there is an easier way of doing that, but many people still deploy applications this way.

The file on your computer is located at $HOME/Desktop/webapp.zip and you want to copy it to the remote server at ~/production.

💡
The symbol ~ represents the user's home folder on the remote server. We cannot use $HOME because it will be interpreted as the home folder on the local computer.

Using SCP command

SCP is the CLI command we use to perform copy over SSH. It stands for Secure Copy Protocol.

The command template for a copy from a local machine to a remote server is the following:


scp -P <SERVER_PORT> <PATH_TO_LOCAL_FILE> <SERVER_USER@SERVER_IP_ADDRESS>:<PATH_TO_DESTINATION_FOLDER>

Let's explain each parameter:

Parameter Description
PATH_TO_LOCAL_FILE The path of the file on your local computer
SERVER_PORT The remote server port; It is not required and must be used only if you changed the default SSH port (22)
SERVER_IP_ADDRESS The IP address of the remote server
SERVER_USER The server's user
PATH_TO_DESTINATION_FOLDER The folder directory in the remote server where the file will be copied

For the use case we defined earlier, this is the command to execute:


scp -P 8984 $HOME/Desktop/webapp.zip johndoe@184.34.232.90:~/production

The command will ask for the password associated with the server user and proceed to the copy if valid.

Using the SCP command with SSH key

An SSH key is one of the most secure ways to access a server through SSH. You can then generate an SSH key on your computer, copy the public SSH key, and add it to the remote server file that stores the authorized server to connect with, usually located at $HOME/authorized_keys.

The copy command template becomes:


scp -i <PATH_TO_LOCAL_PRIVATE_SSH_KEY> <PATH_TO_LOCAL_FILE> <SERVER_USER@SERVER_IP_ADDRESS>:<PATH_TO_DESTINATION_FOLDER>

The parameter PATH_TO_LOCAL_PRIVATE_SSH_KEY is the location of the SSH private key on your local computer (usually $HOME/.ssh/id_rsa).

The command to run for our use case is:


scp -i $HOME/.ssh/id_rsa $HOME/Desktop/webapp.zip johndoe@184.34.232.90:~/production

Copying files from remote server to local machine

As a use case, let's say on your remote server, you have a Node.js application log files you want to download locally for better analysis.

The file on your remote server is located at $HOME/production/webapp/logs.zip and you want to copy it to your local computer at $HOME/Desktop/webapp.

The command template for a copy from the remote server to the local computer is the following:


scp -P <SERVER_PORT> <SERVER_USER@SERVER_IP_ADDRESS>:<PATH_TO_REMOTE_FILE> <PATH_TO_LOCAL_DESTINATION_FOLDER>

We have two parameters to explain:

Parameter Description
PATH_TO_REMOTE_FILE The path of the file on the remote server to copy locally
PATH_TO_LOCAL_DESTINATION_FOLDER The folder directory in the local computer where the file will be copied

The command to run in our use case is:


scp -P 8984 johndoe@184.34.232.90:~/production/webapp/logs.zip $HOME/Desktop/debug

Using the SCP command with SSH key

With an SSH key, the command template is:


scp -i <PATH_TO_LOCAL_PRIVATE_SSH_KEY> <SERVER_USER@SERVER_IP_ADDRESS>:<PATH_TO_REMOTE_FILE> <PATH_TO_LOCAL_DESTINATION_FOLDER>

The command to run for our use case is:


scp -i $HOME/.ssh/id_rsa johndoe@184.34.232.90:~/production/webapp/logs.zip $HOME/Desktop/debug

Copy a directory

It is possible to copy a directory with many files and folders over SSH. For that, you must add the option -r meaning to recursively copy directories.

The command to copy a directory using SCP is:


scp -P 8984 -r $HOME/Desktop/webapp johndoe@184.34.232.90:~/server

Below is an example of a directory's copy:

Copy a directory from the local computer to the server over SSH.

You can add the option -q to disable the progress meter and warning and diagnostic messages.

Wrap up

Copying files over SSH can be achieved using the scp command line that allows remote server authentication with username-password or an SSH key.

One of the most confusing things using this command is understanding which server is local or remote, so pay attention to that and avoid copying a file in the wrong path.

To learn more about scp command options, check out the documentation.

Follow me on Twitter or subscribe to my newsletter to avoid missing the upcoming posts and the tips and tricks I occasionally share.