scp
(Secure Copy Protocol) is a secure and efficient method widely used in system administration and web development. This method encrypts the data transferred, ensuring security over the network. Below is a comprehensive guide on how to perform file transfers between two servers using SSH and scp
.Prerequisites
- SSH access to both the source and destination servers.
- The IP address or hostname of the destination server.
- A user account with sufficient permissions to read/write files on both servers.
- OpenSSH installed on both servers (commonly pre-installed on most Unix-like systems).
Understanding scp
Syntax
The basic syntax of the scp
command is:
scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
- OPTION:
scp
options such as-p
(preserves file modification and access times) or-r
(recursively copy entire directories). - SRC_HOST, DEST_HOST: Hostnames or IP addresses of the source and destination servers. If omitted,
scp
assumes the files are on the local host. - user@: Specifies the username to log in as on the remote server. If omitted,
scp
uses the current user’s username. - file1, file2: The source and destination file paths. If a directory is specified,
-r
needs to be used for recursive copying.
Transferring Files from Local to Remote Server
To transfer a file from your local machine to a remote server, use the following command:
scp /path/to/local/file.txt user@remote_server:/path/to/remote/directory
This command will copy file.txt
from the local machine to the specified directory on remote_server
using the credentials of user
.
Transferring Files from Remote to Local Server
To transfer a file from a remote server to your local machine, simply reverse the source and destination paths:
scp user@remote_server:/path/to/remote/file.txt /path/to/local/directory
This command will copy file.txt
from the remote server to the specified local directory.
Transferring Files Between Two Remote Servers
You can also use scp
to transfer files between two remote servers from your local machine:
scp user@source_server:/path/to/source/file.txt user@destination_server:/path/to/destination/directory
This command transfers file.txt
from source_server
to destination_server
. Note that this process will route the data through the local machine, potentially impacting transfer speed.
Additional Options and Tips
- Use the
-r
option to recursively copy directories:scp -r user@remote_server:/path/to/remote/directory /path/to/local/directory
- To increase verbosity for troubleshooting, add the
-v
option. - If your SSH server uses a different port than the default (22), use the
-P
option to specify it:scp -P 2222 /path/to/local/file.txt user@remote_server:/path/to/remote/directory
- For secure key-based authentication, ensure your SSH key is added to the
ssh-agent
and the public key is in the remote server’s~/.ssh/authorized_keys
file.
Conclusion
Transferring files between servers using scp
is a straightforward and secure method that leverages the SSH protocol. By understanding and utilizing the command syntax and options provided in this guide, administrators and developers can efficiently manage file transfers between servers, enhancing their workflow and ensuring data security.
Inspired by https://wiredgorilla.com/scp-moving-large-files-linux-server-linux-server/
And https://fastdot.com/server-administration/how-to-transfer-files-between-servers-using-ssh/