How do I download and upload files using SSH?

SSH is the most secure protocol for accessing remote servers. It provides the highest level of end-to-end data security over communication channels. The SCP (Secure Copy) command uses the SSH protocol for copying files between remote and local servers.

The remote server must have a running SSH server. This tutorial will help you to understand download and upload files over SSH protocol.

Download file over SSH Protocol

Here are some useful examples for downloading files from the remote system over SSH protocol.

  • This will connect to example.com server with user “username” and copy the /backup/file.zip file to local system directory /local/dir. To use theis command replace the values as per your environment.
    scp [email protected]:/backup/file.zip /local/dir 
    
  • If the SSH is running on a non-standard port, You can specify the port using -P option with SCP command.
    scp -P 2222 [email protected]:/backup/file.zip /local/dir 
    
  • If your remote server required a private key to connect server, You can use -i followed by a private key file path to connect your server using the SCP command. This can be helpful for AWS servers.
    scp -i private_key.pem [email protected]:/backup/file.zip /local/dir 
    

Upload file using SSH

You can also upload files to the remote server using SSH protocol using the SCP command. Use the following example command for uploading files to the SSH server.

scp file.zip [email protected]:/remote/dir 

Similarity you can use -P switch to define port of the SSH server and -i to define private key for the user authentication.

Conclusion

In this tutorial, you have learned about transferring files between two systems over the SSH protocol.

How to download a file via SSH

This particular guide covers one specific feature – downloading files over SSH. There are a few benefits SSH may offer in terms of downloading files:

  • The connection is encrypted all the way through, so you may not worry about the fact that files may get compromised during the transfer
  • With the help of SSH keys and aliases it’s possible to establish an SSH connection without entering the password each time, so it is possible to download private files in a secure way very quickly
  • There are ample opportunities to make the whole process automatic. For instance, it’s possible to create a server-side script which would create a regular backup of your hosting account and another script on your computer which will allow downloading the fresh backup to your local computer on designated time.

  • Linux and OS X systems
  • Windows 10
  • Windows OS (before Windows 10)


Linux and OS X systems

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.

Here are few examples of how to use it for:

1. Uploading a file from a local computer to a remote one:

scp /path/to/local/file username@hostname:/path/to/remote/file This command can be used to upload a specific file to your account on the server:

scp -P 21098 /home/localuser/site/example.html cpanel_user@servername:/home/cpanel_user/public_html

How do I download and upload files using SSH?

Or this one, if the entire directory should be uploaded:

scp -P 21098 -r /home/localuser/site/ cpanel_user@servername:/home/cpanel_user/public_html

How do I download and upload files using SSH?

2. Downloading a file from a remote system to your computer:

scp username@hostname:/path/to/remote/file /path/to/local/file This particular example can be used to download an error_log from public_html of a hosting account to your local computer:

scp -P 21098 cpanel_user@servername:/home/cpanel_user/public_html/error_log /home/localuser/logs/

How do I download and upload files using SSH?

NOTE: When one of the commands above is used, you will be asked to insert the password into your cPanel account (when you enter the password, it is automatically hidden for the security purposes).

Windows 10

Currently, only Windows 10 has native support for SSH via the PowerShell app. You can locate it by clicking on the Start button > typing in 'PowerShell' and choosing the Windows PowerShell option in the menu:

How do I download and upload files using SSH?

You are able to use the same commands as for Linux and OS X systems to upload and download files:

1. Uploading a file from a local computer to a remote one:

scp /path/to/local/file username@hostname:/path/to/remote/file This command can be used to upload a specific file to your account on the server:

scp -P 21098 C:\Users\Windows_user\Desktop\image.jpg :/home/cpuser/public_html

How do I download and upload files using SSH?

Or this one, if the entire directory should be uploaded:

scp -P 21098 -r C:\Users\Windows_user\Desktop\images\ :/home/cpuser/public_html

How do I download and upload files using SSH?

2. Downloading a file from a remote system to your computer:

scp username@hostname:/path/to/remote/file /path/to/local/file This particular example can be used to download an error_log from public_html of a hosting account to your local computer:

scp -P 21098 :/home/cpuser/public_html/error_log C:\Users\Windows_user\Desktop

How do I download and upload files using SSH?

NOTE: When one of the commands above is used, you will be asked to insert the password into your cPanel account (when you enter the password, it is automatically hidden for the security purposes).

You can use PowerShell on other Windows versions as well following the next workarounds:

To use native Windows command line utilities, select the Start button > click on the Run… option. In the command line type in powershell and press Enter:

How do I download and upload files using SSH?

Here is an example of the command for downloading the file from the server to your computer:

Invoke-WebRequest http://domain.com/path-to-file.zip -UseBasicParsing -OutFile local.zip

where:

http://domain.com/path-to-file.zip should be replaced with the URL to the file in question
local.zip should be replaced with the name you would like the downloaded file to be stored with. You may also specify a full path there. By default, it will be downloaded to C:\Users\your-windows-username directory:

How do I download and upload files using SSH?

The Invoke-WebRequest uses the HTTP protocol instead of SSH one. Its sole resemblance to scp is that the command line interface is being used as well.

This method has its disadvantages. First of all, the connection is not encrypted unless you have an SSL certificate and a specified https:// protocol in your URL. The file should be publicly accessible, which is not acceptable in some cases. Also, the file contents are stored in memory before being recorded to the disk, making this approach unsuitable for downloading large files.

2.Another workaround includes installing the Cygwin command line interface for Windows, which features the scp command. In order to use it, do the following:

  • Download the cygwin installation file from here
  • Install cygwin on your computer (do not forget to include openssh from the net bundle during installation process)
  • Once installed, you will be able to run the scp command from the first part of the article using the Windows command line terminal (accessible via the Start button > Run… option > In the command line, type in cmd and press Enter).

    That's it!

                                         Need any help? Contact our HelpDesk

Can I upload files through SSH?

It's based on the SSH protocol used with it. A client can use an SCP to upload files to a remote server safely, download files, or even transfer files via SSH across remote servers.

Can SSH download files?

With the help of SSH keys and aliases it's possible to establish an SSH connection without entering the password each time, so it is possible to download private files in a secure way very quickly.