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_ipReplace
usernamewith your username andserver_ipwith 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/directoryTo 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/directoryThe
-rflag 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:
-
Connect to the Destination Server: Use SSH to log into the server where files were transferred.
-
Navigate to the Directory: Change to the directory where the files were sent.
-
List the Files: Use
lsorls -lto view the files and their details. -
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
chmodorchown. These commands can modify file permissions and ownership, allowing you to manage access rights effectively. For instance:chmodchanges the file mode bits. Example:chmod 644 filenamesets read and write permissions for the owner and read-only for others.chownchanges the file owner and group. Example:chown username:groupname filenameassigns 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 serverHere's how you can enhance this process:
-
Initial SSH Connection: Use SSH to connect to the first server. For example,
ssh user@first-server-ip. -
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/destinationConversely, to transfer a file from the second server to the first, you would reverse the paths.
-
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.
-
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.
-
Verbose Mode for Troubleshooting: If you encounter issues, use the
-vflag with the SCP command for a verbose output. This can provide insights into connection or transfer errors. -
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.