How to Download a File from a Server Using SSH

Downloading files from a remote server over SSH (Secure Shell) is a common task for system administrators and users who need to manage files on remote machines. This guide will walk you through various methods to download files from a server using SSH, ensuring clear and practical instructions.

Using scp (Secure Copy Protocol)

The scp command is a straightforward and secure way to copy files between hosts over SSH.

Basic Syntax:

scp username@remote_host:/path/to/remote/file /path/to/local/destination

Explanation:

  • username@remote_host: The SSH username and server address.
  • /path/to/remote/file: The path to the file on the remote server.
  • /path/to/local/destination: The local path where the file will be saved.

Example:

To download a file named example.txt from the remote server example.com and save it to your local /home/user/ directory:

scp user@example.com:/path/to/example.txt /home/user/

Using rsync

rsync is another powerful tool for file transfers and synchronization over SSH. It is especially useful for transferring large files or directories and for incremental backups.

Basic Syntax:

rsync -avz username@remote_host:/path/to/remote/file /path/to/local/destination

Explanation:

  • -a: Archive mode; preserves permissions, timestamps, and other attributes.
  • -v: Verbose output; shows detailed information about the transfer.
  • -z: Compresses data during transfer to reduce bandwidth.

Example:

To download a directory named project from the remote server example.com:

rsync -avz user@example.com:/path/to/project /home/user/

Using sftp (Secure File Transfer Protocol)

sftp provides an interactive interface for file transfers over SSH. It’s useful for browsing remote directories and transferring multiple files.

Start sftp Session:

sftp username@remote_host

Explanation:

  • Replace username@remote_host with your SSH username and server address.

Download a File:

Once connected to the remote server, use the get command to download files.

get /path/to/remote/file /path/to/local/destination

Example:

get /path/to/example.txt /home/user/

Download a Directory:

To download an entire directory, use the -r option with get.

get -r /path/to/remote/directory /path/to/local/destination

Using sshfs (SSH Filesystem)

sshfs allows you to mount a remote directory over SSH, making it accessible like a local directory. This method is useful for working with files directly as if they were on your local filesystem.

Install sshfs:

On Debian/Ubuntu:

sudo apt-get install sshfs

On CentOS/RHEL:

sudo yum install sshfs

On Fedora:

sudo dnf install sshfs

Mount the Remote Directory:

sshfs username@remote_host:/path/to/remote/directory /path/to/local/mountpoint

Explanation:

  • /path/to/local/mountpoint: The local directory where the remote directory will be mounted.

Example:

To mount the remote directory /remote/data to your local /mnt/remote directory:

sshfs user@example.com:/remote/data /mnt/remote

After mounting, you can copy files from the mounted directory using standard file operations.

Unmount the Directory:

When done, unmount the remote directory with:

fusermount -u /path/to/local/mountpoint

Example:

fusermount -u /mnt/remote

Conclusion

Downloading files from a server using SSH can be accomplished through various methods, including scp, rsync, sftp, and sshfs. Each method has its own use cases and advantages:

  • scp: Simple and straightforward for single file transfers.
  • rsync: Ideal for large file transfers and synchronization.
  • sftp: Provides an interactive interface for browsing and transferring files.
  • sshfs: Allows you to mount remote directories for seamless file operations.

By choosing the appropriate method based on your needs, you can efficiently manage and transfer files between your local machine and a remote server over SSH.