SCP (secure copy) is a command (command-line) used to transfer files between two servers quickly and securely. By using SCP in conjunction with SSH, you can copy and move files and folders between Linux/Ubuntu servers. SCP uses SSH encryption and authentication to ensure the security of file transfers. This is a reliable method for sharing and transferring files between two server locations.
How to use SCP in Linux/Ubuntu to transfer files between two servers
As ItsmeIT mentioned earlier, you can transfer any data, files or folders from loalhost to server (VPS) or from one server to another.
In this tutorial, I will provide 3 use cases of SSH in combination with SCP: using default SSH port 21/22, or private port and server using public_key.
Method 1: Copy File from Localhost to server using SSH
For example, if you want to transfer the file “file.txt” from localhost to the server via the default port 21/22 and without using the public key:
scp file.txt user@<IP_server B>:<destination_file_path>
- file.txt is the file or directory you need to transfer to server B.
- <path_file_destination> is the path on server B to receive files/data from A.
Method 2: The SCP command transfers data to the Server using SSH key
If your server uses SSH keys you can use the option
-i to specify the path to your private key in the SCP command to transfer files between two Linux servers or from loalhost to the server.
scp -i <private_key> <source_file_path> <username>@<địa server_IP_address>:<destination_file_path>
- <path_private_key>: is the path to your SSH key
- <path_file_source>: Is the path to the file to be transferred/send to the server
- <destination_file_path>: Is the exact path of the receiving server
In the screenshot above, ItsmeIT has successfully transferred the zip file from Ubuntu (localhost) to the Server via SCP.
Method 3: Using SCP to transfer FILE to Server with SSH key + port
If your server uses a dedicated port for SSH connections instead of the default port (port 22), you can use the option
-P to specify the port for the SCP command. Here is the command syntax to transfer files between two servers or computers to the server:
scp -P <port_number> -i <path_private_key> <đường_dẫn_file_nguồn> <username>@<server_IP_address>:<destination_file_path>
Method 4: Using SCP to Move a Folder from Desktop to Server in Ubuntu/Linux
In the previous method, you were able to transfer or copy files from server to server or from localhost to server. What if I want to move folder from desktop to server? Below is the command for you to do.
scp -r <source_directory_path> <username>@<server_IP_address>:<target_directory>
If your server uses SSH keys for authentication and also uses a private port, you can combine options
-P to specify the private key and port for the SCP command. Here is the command syntax to be able to transfer directories to the Linux Server:
scp -P <port_number> -i <private_key_path> -r <source_directory_path> <username>@<địa server_IP_address>:<destination_directory_path>
Where -P defaults to 22, change the port your server is using.
SCP (Secure Copy) provides effective methods for you to save time in sending data from computer to server and transferring files and folders between servers. By combining with the SSH (Secure Shell) protocol, SCP allows you to perform safe and secure copying tasks while protecting your data in the best way.
It not only ensures fast data transfer speed but also allows you to copy the entire folder structure, including subfolders and files inside, easily and conveniently. This helps you to manage data and share information between servers efficiently and reliably, saving time and effort in network work.