How To Transfer Files From a Remote Server to another Remote Server Using SSH

How To Transfer Files From a Remote Server to another Remote Server Using SSH

A relatively common situation is requiring the move of a website from one server to another. There are a number ways one can do this. One could simply download the entire website to a local computer and upload it to the new server or one could use third party FTP tools to log into both servers and transfer files from one to the other, using their local computer as a sort of “middle man”. There is another method which is relatively easier which we’ll look at today and that is transferring files across servers using SSH.

SSH

SSH, or Secure Shell, is a secure network protocol used to operate network services securely by establishing a secure channel over an unsecure network. Unlike most third party FTP apps, SSH is operated via the command line. This could be the Command Prompt in Windows or Terminal in either Linux or Mac machines. In pre-10 versions of Windows however you’re going to need a third party app to use SSH as it had not become native to Windows systems until Windows 10. In such cases, the most the popular Windows SSH app to use is known as Putty (available from www.Putty.org).

In Mac and Linux machines SSH is a native feature, available right out of the box. Also, most servers are run on Linux, meaning that, unless your server runs on the Windows equivalent, IIS (Internet Information Service) then SSH should be available natively as well. As such, you won’t have to install any third party applications, other than Putty, to follow this tutorial.

SCP

Copying files via SSH uses the SCP (Secure Copy) protocol. SCP is a method of securely transferring files and entire folders between computers and it is based on the SSH protocol that it’s used with. Using SCP a client can send (upload) files securely to a remote server or request (download) files. It may also be used to transfer files across remote servers, and it is this function that we’ll elaborate on today.

Transferring Files Across Servers

The following steps will walk you through connecting to a remote Linux or Mac server from a local Windows computer that Putty has already been installed on..

  1. Login to your remote servers via their web console and access their control panels to ensure that they have SSH enabled on both
      • SSH access is usually disabled by default so this service may have to be activated
        • At the very least, you should check to see that it’s activated.
        • Consult with either your provider’s tech support team or your specific server OS documentation for instructions on how to activate this.
      • Record the assigned SSH username (whether this is generated by the server or created by you, depends on the server OS)
      • Enter and Re-enter (confirmation) the password to complete the SSH account creation
        • The password can be changed but instruction on this are server specific
      • Record the Secure Shell Connection Information
        • This will be required to connect to the remote server when using the SSH client
      • For additional security purposes it is advised that you record the host key for your servers.
    1. Activate Putty and establish a SSH connection to the remote server.
      • Enter the relevant information in the Host Name field of the Configuration window
      • initiate the connection
      • If you are connecting to a server for the first time Putty will present a Security Alert
        • CAUTION: If this is NOT the first time you are connecting to this server yet you see this security alert then be wary. It could be the case that someone is attempting what’s known as a Man-In-The-Middle attack. The attacker may be attempting to occupy your connection and steal your password.
        • If you are confident that the connection is genuine, Click Yes.
        • Putty should now present you with a terminal window
          • You will be prompted for your username and password
          • Enter your credentials
          • Note: When entering passwords via SSH there will be no on screen response. Simply type in your password when prompted and hit Enter.
        • If you have successfully entered your password the terminal window will now provide you with a command line on the server.
          • You can type commands in this terminal window, allowing you limited control over the server
          • The server’s responses will be displayed in said window
    1. Navigate to your intended destination folder on the remote server via Putty
    2. View the contents of the folder to ensure that there are no files nor folders with the same name(s) as the file(s) or folders you wish to transfer.
      • If they are, and if possible, either move them to a different folder, rename them or delete them.
    3. Use Secure Copy to transfer files from one server to the next
      1. The Secure Copy syntax is as follows
        scp [switch] [source content location] [destination content location]
      2. “scp” is the command to activate the function.
      3. The switch is an optional parameter. If you are transferring a single file, or multiple files located in the same directory, and destined for the same folder, then you will not need a switch. If you plan on transferring entire folders then the recursive switch is required.
        • -r
        • This is not the only switch available. There are many other switches available to be used to control SCP but that is beyond the scope of this tutorial.
      4. The Content Location syntax is slightly different depending on its location
        • If the content is located on the server which you are logged into then the content location is simply the directory, or filename, you wish to use on that server.
          • E.g.: “/var/www/dir”
        • If the content is located on the second remote server to which you are not logged into then the content location parameter is a little more complex.
          • [userid]@[remote server 2 url or ip address]:[directory or file]
          • Eg. “user1@www.server2.com:/var/www/”
      5. So, in full, an example of the syntax to copy a directory from one remote server to another remote server, assuming that the destination is the server you are currently logged into, would be as follows:
        scp -r user1@www.server2.com:/var/www/ /var/www/dir
    4. You will then be prompted for the password for the second remote server. Type it in (again, you will not see it fill in) and press Enter.

If you have successfully entered the correct password for the second remote server then you should see the file transfer begin. You may now close the terminal window as the process will continue automatically between the two remote servers.

Check out the top 3 Best web hosting services

Hostinger
$2.99 /mo
Starting price
Visit Hostinger
Rating based on expert review
  • User Friendly
    4.7
  • Support
    4.7
  • Features
    4.8
  • Reliability
    4.8
  • Pricing
    4.7
IONOS
$1.00 /mo
Starting price
Visit IONOS
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.3
Ultahost
$2.90 /mo
Starting price
Visit Ultahost
Rating based on expert review
  • User Friendly
    4.3
  • Support
    4.8
  • Features
    4.5
  • Reliability
    4.0
  • Pricing
    4.8

How to use SSH to Back Up Your WordPress Website Hosted on a Linux VPS

The default directory path for websites in Linux Servers is usually./var/www/htm
1 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Set Up SSH on an Ubuntu 16.04 VPS or Dedicated Server

This tutorial shows you how to set up secure shell (SSH) on an Ubuntu 16.04 virt
2 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How To Set Up SSH for an Ubuntu 16.04 VPS From a Linux Client

How to set up ssh private key authentication on an Ubuntu 16.04 server with a Li
2 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Set Up SSH for your Ubuntu 18.04 VPS or Dedicated Server

This how-to article shows you how to create a public and a private key for acces
4 min read
Bruno Mirchevski
Bruno Mirchevski
Hosting Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top