How to copy files over SSH (2024)

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 ApplicationYou 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.Teco TutorialsEric Cabrel TIOGO

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.

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 [email protected]:~/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 [email protected]:~/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 [email protected]:~/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 [email protected]:~/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 [email protected]:~/server

Below is an example of a directory's copy:

How to copy files over SSH (3)

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.

How to copy files over SSH (2024)

FAQs

Is it possible to copy files over SSH? ›

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.

How to copy and paste over SSH? ›

use the mouse to open the context menu and click the Edit > Paste option. hit Ctrl + V on the keyboard. use the keyboard to access the context menu's Edit > Paste option: Alt + Space , then E , then P. if the "Use Ctrl+Shift+C/V as Copy/Paste" option is activated, hitting Ctrl + Shift + V on the keyboard.

What is the fastest way to copy files over SSH? ›

If you're dealing with large files, using the compression option in scp over ssh can significantly speed up the transfer. This is particularly useful when bandwidth is a constraint.

Can files be transferred over SSH? ›

Typically, one of the regular tasks of SSH is to copy files safely over secure connections. If you're using Linux, you can achieve this by using a command called SCP. It's a simple way to ensure your files are transferred safely. So, if you're thinking of transferring files over SSH, SCP is the way to go.

What is the program to transfer files over SSH? ›

SSH File Transfers

File transfers are primary focus of WinSCP. WinSCP supports SFTP (SSH File Transfer Protocol) for secure file transfers. In addition to that it also supports legacy SCP (Secure Copy Protocol). You can use WinSCP to transfer files both manually and automatically.

How do I copy a folder from SSH to local? ›

Copying a Folder From SSH to Local Using SCP

– `user@remote_host:`: Replace “user” with your remote server username and “remote_host” with the remote server's IP address or hostname. – `/remote/folder/path`: Replace this with the path to the folder on the remote server you want to copy.

How to transfer files over SSH in Windows? ›

Transferring Files Via SSH Across Remote Servers: a 3-Step Process
  1. Step One: Checking and Enabling SSH on Remote Servers. Use your remote servers' web console to log in. ...
  2. Step Two: Setting Up SSH Remote Server Connection with PuTTY. ...
  3. Step Three: Transferring Files Between Remote Servers Using Secure Copy (SCP):
Mar 12, 2024

How do I copy files to clipboard using SSH? ›

How to copy/paste:
  1. Hold down Shift + mouse select in the remote micro, then Ctrl + Shift + C; then on the local PC to paste: Ctrl + V or Ctrl + Shift + V or middle-mouse-click.
  2. Notes/caveats: To terminal-UNselect when done, hold Shift and click anywhere in the remote micro window.
Jan 26, 2017

How to copy SSH in terminal? ›

Copy the ssh key into remote servers
  1. Open a terminal on your local computer.
  2. Generate an SSH key pair if you haven't already done so by running the command: ssh-keygen . ...
  3. Once the key pair is generated, run the command: ssh-copy-id user@remote_server . ...
  4. You'll be prompted to enter the password for the remote user account.
Apr 8, 2023

What is the SSH file transfer method? ›

In computing, the SSH File Transfer Protocol (also known as Secure File Transfer Protocol or SFTP) is a network protocol that provides file access, file transfer, and file management over any reliable data stream.

How does SSH copy work? ›

ssh-copy-id is a useful tool for SSH connections to a remote host without using a password. Basically, it copies the SSH key into the remote host's authorized_keys file, which is by default in the $HOME/. ssh directory. In this tutorial, we'll discuss how to automate the usage of ssh-copy-id.

Why is rsync better than SCP? ›

When comparing rsync and scp , the performance largely depends on the context and usage, but generally speaking, rsync is often faster and more efficient than scp for several reasons: Incremental Transfers: rsync is designed to only transfer the changes between source and destination.

Can you use SSH to copy files? ›

Often you will need to move one or more files/folders or copy them to a different location. You can do so using an SSH connection. The commands which you would need to use are mv (short from move) and cp (short from copy).

How to copy large files over SSH? ›

This is achieved by utilizing the capabilities of the rsync utility in conjunction with SSH/SCP. While rsync and scp are distinct tools, rsync can be employed to optimize file synchronization. It also speeds up transfers by running multiple parallel processes.

Can you download files over SSH? ›

OpenSSH SSH/SecSH protocol suite (which comes pre-installed with OS X and available for download for most other *nix systems) includes the scp (secure copy) application which can be used to upload and download files from and to remote hosts.

Does SSH support file transfer? ›

SSH (Secure Shell) is a network protocol enabling secure connections between two systems. SFTP (SSH File Transfer Protocol) allows secure file transfer, leveraging SSH for encrypted connections. While SSH is designed for secure command execution, SFTP focuses on secure file transfers.

Can you use SCP to transfer the file if you're using SSH? ›

The scp command allows you to copy files over ssh connections. This is pretty useful if you want to transport files between computers, for example to backup something. The scp command uses the ssh command and they are very much alike.

Top Articles
Can You Solo a Raid in Pokemon GO? - Playbite
Why is there a difference between my balance and available
Maxtrack Live
Global Foods Trading GmbH, Biebesheim a. Rhein
Warren Ohio Craigslist
My E Chart Elliot
Occupational therapist
COLA Takes Effect With Sept. 30 Benefit Payment
Teenbeautyfitness
How to Type German letters ä, ö, ü and the ß on your Keyboard
Tugboat Information
Tokioof
Buying risk?
Aktuelle Fahrzeuge von Autohaus Schlögl GmbH & Co. KG in Traunreut
Belle Delphine Boobs
Top tips for getting around Buenos Aires
Craigslist Free Stuff Greensboro Nc
Lola Bunny R34 Gif
Hdmovie2 Sbs
Timeforce Choctaw
Yog-Sothoth
The Tower and Major Arcana Tarot Combinations: What They Mean - Eclectic Witchcraft
Blackboard Login Pjc
Vera Bradley Factory Outlet Sunbury Products
Weather October 15
Filmy Met
Bad Business Private Server Commands
Davita Salary
Rogold Extension
Jt Closeout World Rushville Indiana
Amici Pizza Los Alamitos
Navigating change - the workplace of tomorrow - key takeaways
Prima Healthcare Columbiana Ohio
Maybe Meant To Be Chapter 43
Mydocbill.com/Mr
Atlanta Musicians Craigslist
2007 Peterbilt 387 Fuse Box Diagram
Craigslist Pets Plattsburgh Ny
Owa Hilton Email
Chase Bank Zip Code
Ehc Workspace Login
Hillsborough County Florida Recorder Of Deeds
Page 5747 – Christianity Today
Blippi Park Carlsbad
Madden 23 Can't Hire Offensive Coordinator
Walmart Front Door Wreaths
A Snowy Day In Oakland Showtimes Near Maya Pittsburg Cinemas
Fredatmcd.read.inkling.com
Bob Wright Yukon Accident
Latest Posts
Article information

Author: Twana Towne Ret

Last Updated:

Views: 6024

Rating: 4.3 / 5 (64 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Twana Towne Ret

Birthday: 1994-03-19

Address: Apt. 990 97439 Corwin Motorway, Port Eliseoburgh, NM 99144-2618

Phone: +5958753152963

Job: National Specialist

Hobby: Kayaking, Photography, Skydiving, Embroidery, Leather crafting, Orienteering, Cooking

Introduction: My name is Twana Towne Ret, I am a famous, talented, joyous, perfect, powerful, inquisitive, lovely person who loves writing and wants to share my knowledge and understanding with you.