SCP (Secure Copy) is a simple method for transferring files between two servers using SSH, providing the same level of security. This method requires access to both servers, so you won't be able to transfer files without having access to the remote servers.
Copying Files from a Local Server to a Remote Server
Below is the syntax for the command to copy local data to another server:
scp test_file.txt remote_server_username@remote_server_IP:/remote/directory
Explanation of Syntax:
scp
: Main command
test_file.txt
: File being transferred
remote_server_username
: User for connecting to the remote server (e.g., root)
remote_server_IP
: IP of the remote server
/remote/directory
: Directory path on the remote server where the file will be copied. Ensure it exists on the remote server.
Example:
scp 1G.test root@21x.xxx.xxx.x43:/home
This example shows how the file "1G.test" is copied to a remote server's /home
folder. After executing the command, you'll be asked to confirm the connection:
Are you sure you want to continue connecting (yes/no)?
Enter "yes" and you'll be prompted for the remote server password. After entering it, the file transfer begins, and you'll see a progress bar.
Copying Files from a Remote Server to a Local Server
This is the reverse of the previous command. The syntax is:
scp remote_server_username@remote_server_IP:/remote/file.txt /local/directory
Declare the remote server's file path and the local server's directory to copy the file.
Copying Files from One Remote Server to Another Remote Server
You can use the scp
command to copy files from one remote server to another without manually connecting to each server. The syntax is:
scp remote_server_username1@remote_server1_IP:/files/file.txt remote_server_username2@remote_server2_IP:/files
This example copies the file /files/file.txt
from server1 to the folder /files
on server2. You will need login details for both servers.
Additional Options for SCP
Copying Multiple Files
You can copy multiple files by listing them:
scp file1.txt file2.txt archive.gzip remote_server_username@remote_server_IP:/home
This example shows how to copy multiple files to a remote server.
Copying a Full Directory
Use the -**r**
option to copy an entire directory:
scp -r /home/backups/backup1 remote_server_username@remote_server_IP:/home
This example copies the folder /home/backups/backup1
to a remote server.
Using a Custom SSH Port
The scp
command can work with a custom SSH port using the **-P**
option:
scp -P 1234 file1.txt remote_server_username@remote_server_IP:/home
This example transfers the file to a remote server using custom SSH port 1234 with the **-P**
option. By default, the command uses the standard SSH port 22.
Note
SCP works on Windows as well; you will just need an SSH client.
For more information, refer to the SCP documentation.