- Edited
SCP (Secure Copy Protocol) is a straightforward method for transferring files between two servers using SSH, ensuring the same level of security. To use SCP, you need access to both servers involved, as transferring files requires authorization on the remote servers.
Copying Files from a Local Server to a Remote Server
To copy files from your local server to a remote server, use the following command syntax:
scp test_file.txt remote_server_username@remote_server_IP:/remote/directory
Explanation:
scp - The command used for secure copying.
test_file.txt - The file being transferred.
remote_server_username - The username for accessing the remote server (e.g., root).
remote_server_IP - The IP address of the remote server.
/remote/directory - The destination directory path on the remote server. Ensure this directory exists.
Example:
scp 1G.test root@21x.xxx.xxx.x43:/home
This example shows how to copy the file 1G.test to the /home directory on the remote server. After executing the command, you will be asked to confirm the connection:
Are you sure you want to continue connecting (yes/no)?
Type yes to proceed, then enter the remote server password. The file transfer will start, and you will see a progress indicator.
Copying Files from a Remote Server to a Local Server
To copy files from a remote server to your local server, use the following command:
scp remote_server_username@remote_server_IP:/remote/file.txt /local/directory
Specify the path of the file on the remote server and the directory on your local server where it will be copied.
Copying Files Between Two Remote Servers
If you need to transfer files between two remote servers, use:
scp remote_server_username1@remote_server1_IP:/files/file.txt remote_server_username2@remote_server2_IP:/files
This command copies /files/file.txt from server1 to the /files directory on server2. You'll need login details for both servers.
Additional SCP Options
1. Copy Multiple Files:
scp file1.txt file2.txt archive.gzip remote_server_username@remote_server_IP:/home
This command copies multiple files to the remote server.
2. Copy a Directory:
scp -r /home/backups/backup1 remote_server_username@remote_server_IP:/home
Use the -r option to copy an entire directory.
3. Custom SSH Port:
scp -P 1234 file1.txt remote_server_username@remote_server_IP:/home
Use the -P option to specify a custom SSH port (e.g., 1234). By default, SCP uses port 22.
Note: SCP is also compatible with Windows, requiring an SSH client.
For more details, refer to the SCP documentation.