How to Download a File from a Server with SSH / SCP

Nov 7, 2016 - 1 Comment

Terminal in OS X Users can securely download a file from any remote server with SSH by using the scp tool at the command line. Essentially this means you can have a file stored securely on a remote server and transfer it to local storage without having to expose that file to the outside world, because scp offers the same level of security and requires the same authentication that ssh does.


Securely downloading files with scp is aimed primarily at advanced users who are using ssh and the command line regularly in either macOS X, bsd, or linux. For those with adequate command line experience, using ssh and scp to download remote files is easy and, conveniently, after the file transfer has completed, the remote connection will end. This makes scp preferential to sftp for quick file downloads, though you could obviously use sftp if you wanted to as well.

Downloading a File from Remote Server with SSH Secure Copy

This assumes the remote server has ssh active, and if you’re able to ssh into the machine then it will have likely have scp active as well. If you don’t have a remote server to try this with, you can try it out between Mac OS X machines or with localhost if you enable ssh and Remote Login on the Mac beforehand.

The basic syntax to use scp (secure copy) for securely downloading remote files is as follows, replacing user, server, path, and target as appropriate:

scp user@server:/path/to/remotefile.zip /Local/Target/Destination

For example, to download a file to the local desktop named “filename.zip” located in the home directory of remote user “osxdaily” on server IP 192.168.0.45, the syntax would be as follows:

% scp osxdaily@192.168.0.45:filename.zip ~/Desktop/
Password:
filename.zip 100% 126 10.1KB/s 00:00
%

Assuming authentication is correct, the target file will immediately start to download to the target destination, offering a percentage completion, download speed, and elapsed transfer time as the file download proceeds.

As usual with the command line, it’s important to specify exact syntax.

If the file or path has a space in the name, you can use quotations or escaping on the path like so:

scp osxdaily@192.168.0.45:"/some remote directory/filename.zip" ~/Desktop/

scp can also be used to securely place a file on a remote server by adjusting the syntax as well, but we’re focusing on downloading a file rather than uploading files here.

If you’re new to ssh and testing this out yourself, and if you have never connected to the remote server before, you will be asked to confirm whether or not you wish to actually connect to the remote machine. This looks like so, and requires a ‘yes’ or ‘no’ answer before the download begins.
% scp osxdaily@192.168.0.4:filename.zip ~/Desktop/
The authenticity of host '192.168.0.4 (192.168.0.4)' can't be established.
ECDSA key fingerprint is SHA256:31WalRuSLR83HALK83AKJSAkj972JJA878NJHAH3780.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.4' (ECDSA) to the list of known hosts.
Password:
filename.zip 100% 126 0.1KB/s 00:00
%

Again, assuming the connection is approve and the login is successful, the remote file will download from the target server to the localhost.

You can also use scp to download multiple files from a remote server:

scp user@host:/remote/path/\{file1.zip,file2.zip\} /Local/Path/

Using ssh for remote file downloads like this is most appropriate for secure transfers which require authentication. Sure you can also downloading files with curl or wget from remote servers, but files accessible with curl and wget tend to be accessible from the outside world as well, whereas ssh and scp requires authentication or a key, and uses 3DES encryption, making it considerably more secure.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Security, Tips & Tricks

One Comment

» Comments RSS Feed

  1. Rob says:

    To download a directory and it’s contents:

    scp -r user@server:/path/to/dir /Local/Target/Destination

    Finally, if there are multiple files with similar naming patterns such as file extensions, wildcards work well:

    scp user@server:/path/to/file/*.jpg /Local/Target/Destination

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates