The mechanism of a computer user accessing different directory files on different hard disk partitions is comparatively similar to how SSH is used to access remote machines existing under a common or different network.
[ You might also like: How to Disable SSH Login to Specific User in Linux ]
This network can be public like the Internet or private like LAN (Local Area Network) and SSH makes it possible for these machines to be accessible but what about downloading their associated files?
This article is for the Linux user on a desktop environment who wishes to access and download files on a remote Linux server environment via SSH.
You need to have authenticated access to these two Linux environments and some familiarity with the Linux command-line interface.
Basic Syntax of Using SSH with SCP Command
The SSH (Secure Shell) protocol is used to enable the SCP (Secure Copy) command to successfully accomplish specific file download objectives from a Linux server environment to a Linux desktop environment.
The standard syntax of using the SSH command with the SCP command looks like the following:
$ scp username@server_url_or_ip:/path/to/downloading/file/from/server /path/to/download/location/on/desktop
Firstly, you should know the username and the IP address or hostname of the remote server from where you wish to retrieve a specific file.
Secondly, you should accurately specify the relative path to your download file location on the remote server and the relative path to the download file storage location on your desktop computer.
Create Files in Remote Linux Using SSH
You can achieve successful file downloads from a remote Linux server environment to a Linux desktop environment through three techniques.
Before we reference the three SCP file download techniques, let us first have some files on our server that we wish to download. If these files already exist on your end, you are good to go. All you need is the files’ correct relative path.
$ ssh [email protected]
Create files on a remote Linux server.
$ touch LinuxShellTips_secrets.pdf && ls $ touch LinuxShellTips_certification.pdf && ls $ touch LinuxShellTips_for_beginners.pdf && ls
Let’s check out three ways to download files from a remote Linux server.
Download Remote Files Using SCP Command in Linux
In reference to the above syntax rule, we can download our first file with the following approach from your desktop environment.
$ scp [email protected]:/home/ec2-user/LinuxShellTips_secrets.pdf /home/dnyce/Desktop/files
Check for the existence of the downloaded file:
$ cd /home/dnyce/Desktop/files && ls
Download Remote Files with Port Number Using SCP Command in Linux
Maybe you have defined a non-standard port or you want to use a standard port on your Linux server environment for handling all remote file downloads. In this case, you should adhere to the following syntax rule:
$ scp -P port_number username@server_url_or_ip:/path/to/download/file/on/server /path/to/download/location/on/desktop
Let us try to download the second file we created:
$ scp -P 22 [email protected]:/home/ec2-user/ LinuxShellTips_certification.pdf /home/dnyce/Desktop/files
From the look of things, the download was a success.
Download Remote Files with Private Key Using SCP Command in Linux
If the remote server you are using restricts you to a private key/certificate for your remote connections, worry not. You can still achieve remote file download with the following syntax.
$ scp -i private_key/certificate_file.pem username@server_url_or_ip:/path/to/download/file/on/server /path/to/download/location/on/desktop
Let us try to download the third file we created using this approach.
$ scp -i my_file_name.pem [email protected]:/home/ec2-user/ LinuxShellTips_for_beginners.pdf /home/dnyce/Desktop/files
Your terminal instance should be on the same path as this certificate file.
Finally, let us check for the existence of the three downloaded files:
$ cd /home/dnyce/Desktop/files && ls
By using a wild card
(*), you could download all these files at once since they have a common file extension.
$ scp firstname.lastname@example.org:/home/ec2-user/*.pdf /home/dnyce/Desktop/files
This tutorial has taught us how to use SCP (Secure Copy) through the SSH protocol to accomplish specific file downloads from a remote server. The covered approaches are flexible enough to meet your remote files download needs.