How to Download a File from a Server with SSH / SCP
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.
I want to use my mac as a terminal. I was to send a file from 1 remote system, to another remote system without downloading the file.(easy enough). The problem is i want to have the 2nd remote system forward (not download) that file to a 3rd remote session.
I use specific ports and ssh keys for the communication between (2nd and 3rd) remote hosts.
Any thoughts?
If you don’t have an scp client installed or you are on mobile, you can try an online client that also provides a graphical user interface to browse the filesystem
Great article. I want to add that for servers with different port than the standard 22:
scp -P port user@server:file.ext ~/Dir
thanks!
PD note that P is capital
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