Get Rewarded! We will reward you with up to €50 credit on your account for every tutorial that you write and we publish!

How to transfer files using the scp command

profile picture
Author
ChiYun Wei
Published
2024-01-12
Time to read
6 minutes reading time

Introduction

Secure Copy Protocol (SCP) is a quick and secure way to transfer files between Unix-like systems. It uses Secure Shell (SSH) for data transfer and provides strong authentication and secure encrypted data communication. This tutorial will guide you through the process of transferring files between servers using SCP.

Since SCP uses SSH to secure the process, we can easily use SCP to transfer files between servers once SSH is set up and enabled.

Prerequisites

  • Knowledge of server addresses, usernames, and passwords or SSH keys for authentication.

  • Access to two servers with SCP installed and configured.

Step 1 - Connect to the Server

Before using SCP, ensure you have SSH access to both servers. To establish an SSH connection, open a terminal and use the SSH command to connect to the server:

ssh username@server_ip

Replace username with your username and server_ip with the server's IP address or hostname.

Provide the password or use your SSH key for authentication.

Once connected, you're ready to use SCP.

Step 2 - Use scp to Transfer Single Files and Multiple Files

  • Transferring a Single File

    To transfer a single file from your local machine to the server:

    scp /path/to/local/file username@server_ip:/path/to/remote/directory

    To transfer from the server to your local machine:

    scp username@server_ip:/path/to/remote/file /path/to/local/directory
  • Transferring Multiple Files or Directories

    For multiple files or directories, use:

    scp -r /path/to/local/directory username@server_ip:/path/to/remote/directory

    The -r flag is used to recursively copy entire directories.

Options and Flags:

Verbose Mode (-v): Gives detailed information about the transfer process.
Preserve File Attributes (-p): Keeps the original file attributes like modification time and permissions.

Step 3 - Check the Files

After transferring, verify the integrity of the files:

  1. Connect to the Destination Server: Use SSH to log into the server where files were transferred.

  2. Navigate to the Directory: Change to the directory where the files were sent.

  3. List the Files: Use ls or ls -l to view the files and their details.

  4. Check File Integrity: Optionally, compare the file sizes or use checksums (e.g., using md5sum) to ensure the files are intact.

Simple tips

  • Access permissions

    For Hetzner cloud servers, the default user is typically the root user. If you encounter permission issues while using a non-root user, consider employing commands like chmod or chown. These commands can modify file permissions and ownership, allowing you to manage access rights effectively. For instance:

    • chmod changes the file mode bits. Example: chmod 644 filename sets read and write permissions for the owner and read-only for others.
    • chown changes the file owner and group. Example: chown username:groupname filename assigns a new owner and group to a file.
  • Transfer files between two servers

    If you are attempting to use SCP for transferring files between two servers, an efficient approach involves connecting to one of the servers via SSH initially, and then executing the SCP command from within that server to connect to the second server. This method, often referred to as "hopping" between servers, can be particularly useful in scenarios where direct SCP access from your local machine to one of the servers is restricted or not feasible:
    local machine » first server » second server

    Here's how you can enhance this process:

    1. Initial SSH Connection: Use SSH to connect to the first server. For example, ssh user@first-server-ip.

    2. SCP Command Execution: Once logged into the first server, use the SCP command to transfer files to or from the second server. For example, to transfer a file from the first server to the second, you would use:

      scp /path/to/file user@second-server-ip:/path/to/destination

      Conversely, to transfer a file from the second server to the first, you would reverse the paths.

    3. Using SSH Keys for Seamless Authentication: If you set up SSH keys for authentication, you can avoid entering passwords manually during this process. Ensure that the first server has an SSH key that is authorized on the second server.

    4. Port Forwarding (if needed): If direct connections are restricted due to firewall rules or other network policies, consider setting up SSH port forwarding or tunneling. This technique can securely route traffic through the first server to reach the second server. For more information, see "Step 6" in this getting started.

    5. Verbose Mode for Troubleshooting: If you encounter issues, use the -v flag with the SCP command for a verbose output. This can provide insights into connection or transfer errors.

    6. Automating with Scripts: For frequent transfers, consider writing a shell script to automate this process. This can save time and reduce the likelihood of errors during manual input.

Conclusion

In this tutorial, you learned how to transfer files between servers using SCP. SCP provides a secure and efficient way to transfer files, and with these simple steps, you can easily move files between servers in a secure manner. It's a valuable tool for system administrators and anyone needing to move data securely across networks. Remember, always verify the integrity of your files after transfer to ensure they are complete and uncorrupted.

License: MIT
Want to contribute?

Get Rewarded: Get up to €50 in credit! Be a part of the community and contribute. Do it for the money. Do it for the bragging rights. And do it to teach others!

Report Issue
Try Hetzner Cloud

Get 20€ free credit!

Valid until: 31 December 2024 Valid for: 3 months and only for new customers
Get started
Want to contribute?

Get Rewarded: Get up to €50 credit on your account for every tutorial you write and we publish!

Find out more