Download a file from a server using SSH

In this article, we will discuss different ways to download a file from a server using SSH.

Table Of Contents

Introduction

While working in a project, most of the times we have to download files from Linux server to our host machine. We can utilize the SSH feature that come out-of-the-box with most of the Linux operating systems, to perform this operation. SSH is one of the most secure way to access remote servers. Let’s looks at various ways in which we can download the files from a server

SSH or Secure Shell is mostly used in Linux operating system to secure communication between two networked computers. We can use either username/password or public/private key authentication to establish a SSH connection. Following is the syntax to connect to a remote server using SSH with private key authentication

Syntax:

ssh -i <private-key> <username>@<remote_server_name>
  • username : OS username of remote linux server
  • remote_server_name : hostname of remote linux server
  • private-key : we need to generate the public/private key pair and use the private key of remote server to establish a connection

It will help you connect to the remote server using ssh. Now let’s see different ways to download a file from a server over SSH.

Download a file from a server using SCP command:

SCP (Secure Copy) is one of the most widely used command in linux operating system to copy files between remote and local servers.

Syntax:

scp <username>@<remote_server_name>:<remote_file_path> <local_file_path>
  • username : OS username of remote linux server
  • remote_server_name : hostname of remote linux server
  • remote_file_path : file path in remote linux server
  • local_file_path : file path in your local machine

We can use this command to download a file from a server. Let’s see some examples,

Examples:

If we want to copy a file from remote linux server to your local machine then use the below commands

scp usradmin@oracletestinst:/home/opc/www/web/css/main.css /home/usr1/

After entering the above command, we will be prompted for the password, then provide the password of the remote user. Once the authentication is successful, then system will display a message indicating the percentage completion, download speed and transfer time.

It will copy the file file main.css from remote server to the local box, inside folder “/home/usr1/”.

Sometimes we need to connect to remote server using a password-less SSH key based authentication. In those cases, use the below command to download the file from remote server using SSH keys

Syntax:

scp -i <private_key_filename> <username>@<remote_server_name>:<remote_file_path> <local_file_path>

Example:

scp private_key.pem usradmin@oracletestinst:/home/opc/www/web/css/main.css /home/usr1/

It will copy the file from remote server to local box.

Download all files of a directory from a server using SCP command

Using SCP command we can also download all the files within a directory. In order to download all files at once, use the below syntax.

Syntax:

scp -r <username>@<remote_server_name>:<remote_directory_path> <local_directory_path>

It will download all files from remote server’s directory to the given directory on the local box.

Example:

scp -r usradmin@oracletestinst:/home/opc/www /home/usr1/

It will download all files from remote server’s directory “/home/opc/www” to the directory “/home/usr1/” on the local box.

Download a file from a server using SFTP command:

The SFTP (Secure File Transfer Protocol) is a protocol used to access and transfer files over SSH. Unlike SCP command which supports only file transfers over SSH, SFTP command allows us to perform wide range of operations on remote files. Following SFTP syntax can be used to connect to a remote server

Syntax:

sftp <username>@<remote_server_name>

If we are using a username/password authentication to connect to the remote server, then the above command will prompt us to enter the password. Once authentication is successful then we will be connected to remote server. We will be presented with SFTP prompt to enter subsequent commands.

Output:

Connected to usradmin@oracletestinst
sftp>

Most of the times remote SSH server will be running on 22 port. But this port may be changed in some remote server due to security issue. Then we can use the below command to specify the correct port number while using SFTP command

Syntax:

sftp -P <custom_port> <username>@<remote_server_name>

Example:

sftp -P 11101 usradmin@oracletestinst

This command will help you connect to the remote server using sftp protocol. To get list of all SFTP command use the command help,

Syntax:

sftp> help

Output :

Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
chgrp [-h] grp path                Change group of file 'path' to 'grp'
chmod [-h] mode path               Change permissions of file 'path' to 'mode'
get [-afpR] remote [local]         Download file
....

We can use the command get to pull file from remote server to local system. We can use the following syntax to download the file.

Syntax:

sftp> get tutorial.sh

Output :

Fetching /home/opc/tutorial.sh to tutorial.sh

The above command will download the file to our local system. The download location will be the one from which we executed SFTP command. If we want to change the local directory location when we are using SFTP prompt, then use the below command to change the directory path.

Syntax:

sftp> lcd <directory_path>

Example:

sftp> lcd /home/usradmin

It will change the current directory to /home/usradmin on the local system.

If we want to download all files within a directory then use the below syntax

Syntax:

sftp> get -r <remote_directory>
  • r : Using this option we can recursively download files from a remote directory

Example:

sftp> get -r /home/usradmin

It will download all the files in folder /home/usradmin/ from remote server to the local box.

We can also use public/private key authentication for connecting to remote server and download the files to our local system.

Summary:

In this guide, we have learnt multiple ways to download files from remote server to our local machine. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top