Rabikant

Posted on November 15th

How to transfer files from remote server to local using SCP (SSH)

"Let's learn about how to transfer files from remote server to local using SCP (SSH)"

Copying files from a distant server to personal workstation is a very frequent operation in the work of an administrator, webmaster, programmer, and other IT professionals. Of the various applications that could be used to consolidate this aim, one of the safest and most effective is SCP a copy protocol that is based on SSH a shell that is secure. SCP helps one to transfer files over networks while hence preserving their privacy and completeness.

This tutorial will enable us look at how one can ‘SCP’ files from a remote server to their local computer. In this tutorial, we will introduce the SCP and then explain the protocol in detail and provide detailed instructions for using SCP as well as some options thought to be useful in some specific circumstances.

Understanding SCP and SSH

SSH is a network protocol used for the secure communication between two computers using a network connection which can be insecure. It offers confidentiality, data integrity and authentication hence making it the best protocol for accessing other servers remotely. SSH is used for login remotely, running a command, and transferring files securely.

SCP stands for Secure Copy Protocol SCP is a utility that using SSH protocol copies files from/to the local or remote host. SCP guarantee that all data transferred can only be transferred in encrypted form thereby avoiding cases where the files may be intercepted, or interfered with in the course of transmission.

Prerequisites

Before you can transfer files using SCP, you need to ensure the following:Before you can transfer files using SCP, you need to ensure the following:

  1. SSH Access: It is required to have ssh access to the remote server. This often is as simple as having a fully functional account on the server and the ability to remotely log into the Server shell via SSH.
  2. SCP Installed: SCP is typically bundled with the SSC package, meaning that if you have instituted the SSH on your system, then the SCP is part of the system as well. You can verify this by typing ‘scp’ in your terminal.
  3. Network Connection: Make sure you have proper connection with your local machine and server which you are accessing remotely.

Transferring a Single File

To transfer a single file from a remote server to your local machine, use the following command:To transfer a single file from a remote server to your local machine, use the following command:

 scp user@remote_host:/path/to/remote_file /path/to/local_directory/
  • Example: Suppose you want to copy a file named backup.zip from the /var/www/html/ directory on a remote server to your local /home/user/ directory:

    scp user@192.168.1.10:/var/www/html/backup.zip /home/user/
    

Transferring Multiple Files

You can also transfer multiple files in a single command by specifying the files one after the other:

scp user@remote_host:/path/to/file1 user@remote_host:/path/to/file2 /path/to/local_directory/
  • Example: Copy two files backup1.zip and backup2.zip from the remote server:

    scp user@192.168.1.10:/var/www/html/backup1.zip user@192.168.1.10:/var/www/html/backup2.zip /home/user/
    

Transferring Directories

To transfer an entire directory, use the -r option, which stands for "recursive":

scp -r user@remote_host:/path/to/remote_directory /path/to/local_directory/
  • Example: To copy the entire /var/www/html/ directory from the remote server to your local machine:

    scp -r user@192.168.1.10:/var/www/html/ /home/user/
    

SCP Options and Flags

SCP offers several options and flags that allow you to customize your file transfer process.

Specifying a Port Number

If your remote SSH server is running on a port other than the default port 22, you can specify the port number using the -P flag:

scp -P 2222 user@remote_host:/path/to/remote_file /path/to/local_directory/
  • Example: Copying a file using a custom SSH port:

    scp -P 2222 user@192.168.1.10:/var/www/html/backup.zip /home/user/
    

Using Compression

To speed up the transfer of large files, you can enable compression using the -C flag:

scp -C user@remote_host:/path/to/remote_file /path/to/local_directory/
  • Example: Enabling compression for faster transfer:

    scp -C user@192.168.1.10:/var/www/html/backup.zip /home/user/
    

Preserving File Attributes

If you want to preserve the original file attributes (such as modification time, access time, and modes), use the -p flag:

scp -p user@remote_host:/path/to/remote_file /path/to/local_directory/
  • Example: Preserving file attributes during transfer:

    scp -p user@192.168.1.10:/var/www/html/backup.zip /home/user/
    

Verbose Mode

To see detailed information about the file transfer process, you can enable verbose mode with the -v flag:

scp -v user@remote_host:/path/to/remote_file /path/to/local_directory/
  • Example: Enabling verbose mode for detailed output:

    scp -v user@192.168.1.10:/var/www/html/backup.zip /home/user/
    

Advanced SCP Usage

Automating File Transfers with Scripts

For regular file transfers, you can create a shell script to automate the process. Here’s a basic example:

#!/bin/bash
# Script to transfer files from remote server to local machine

REMOTE_USER="user"
REMOTE_HOST="192.168.1.10"
REMOTE_PATH="/var/www/html/backup.zip"
LOCAL_PATH="/home/user/"

scp $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH $LOCAL_PATH

Save the script as transfer_files.sh, make it executable (chmod +x transfer_files.sh), and run it whenever you need to transfer the file.

Using SCP in Cron Jobs

To automate file transfers on a schedule, you can use cron jobs. For example, to run the script every day at midnight:

0 0 * * * /path/to/transfer_files.sh

Transferring Large Files Efficiently

For very large files, it might be more efficient to split the file into smaller chunks before transferring. After transfer, you can reassemble the chunks on the local machine.

Splitting a large file:

split -b 500M largefile.zip chunk_

Transferring the chunks:

scp user@remote_host:/path/to/chunk_* /path/to/local_directory/

Reassembling the file:

cat chunk_* > reassembled_file.zip

Common SCP Issues and Troubleshooting

Permission Denied Errors

In the case of getting “Permission Denied” error then it might be the issue of inadequate permission on the end-server. Check whether you have appropriate privileges on your user account to read or access the files or directories in question.

Connection Timeouts

This can be due to network problem or due to the settings on SSH connection. To fix, ascertain your network connectivity, confirm that the remote server has the SSH service running, and you are using correct IP address and port number.

File Overwriting Concerns

What is more, SCP sends the files to the destination directory and if there are files with the same name in the destination directory, then SCP replaces them. To avoid accidental overwrites, consider using the -n flag, which prevents overwriting existing files:To avoid accidental overwrites, consider using the -n flag, which prevents overwriting existing files:

scp -n user@remote_host:/path/to/remote_file /path/to/local_directory/

Best Practices for Secure and Efficient File Transfers

scp -l 1000 user@remote_host:/path/to/remote_file /path
  1. Use Key-Based Authentication: In addition to that, the security can be improved by avoiding the use of passwords and instead go for SSH key-based authentication. This decreases the likelihood of the so-called brute-force attacks.
  2. Enable Compression: When copying large files use the C flag to compress the transfer and make it quicker.
  3. Check File Integrity: Subsequent to file transfer the checksum can be used to review the integrity or the files that have been transferred (for instance with the help of md5sum or sha256sum commands).
  4. Limit Bandwidth Usage: If for some reason you want to set a tight SCP bandwidth (for example to prevent it from becoming a network hog), you should use the ‘l’ flag followed by the desired bandwidth limit in Kbit/s.

Comments

Leave a comment.

Share your thoughts or ask a question to be added in the loop.