How to Use SCP Command for File Transfer ?
SCP command
The SCP command, which stands for secure copy protocol, is a network file transfer protocol that enables easy and secure file transfer between a remote server (VPS) and a local system or two remote systems. It uses the SSH mechanism to provide authentication and encryption features for your information and credentials.
Administrators use the SCP Linux command to simplify the process of transferring data between Linux servers because it does not require logging into the server. It also provides better security than File Transfer Protocol (FTP) and complies with PCI Security Council standards.
This article will teach you how to use SCP commands on a remote system, such as a VPS. First, we’ll break down the basic SCP command and provide examples to help you better understand Linux commands.
SCP command syntax
The main syntax of the SCP command to transfer files between two remote systems is as follows:
SCP [other options] [source username @ IP]: / [directory and file name] [destination username @ IP]: / [destination directory]
- [other options]: Modifiers you can add to the SCP command. We will discuss the most popular ones later.
- [source username@IP]: Username and IP of the device where the desired file is located.
- [directory and file name]: location and file name/
- [destination username @ IP]: username and IP of the destination system.
- [destination directory]: The destination directory where the file will be saved.
A real example of the SCP command:
SCP -p root@162.168.1.1:/media/scp.pngDed9com@162.168.1.2:/desktop/destination
If you are copying to a localhost (or copying from a localhost), you do not need the IP address and destination or source path such as /desktop/folder_name.
[highlight color=” yellow”]Note: Use the -r option to copy entire directories.
Copy files using the SCP command
The most important thing about SCP is that it allows you to transfer files between two remote servers or a remote machine and a local system. The following sections explain how SCP commands work for each transfer type.
[highlight color=” yellow”]Regardless of the type of transfer you do with SCP, you should not connect to the remote server via SSH.
Copying files from a local server to a VPS (remote system)
In this example, we copy a local file named scp.zip to a remote server (vps) named root. The IP address of the server is after the username:
scp /users/Ded9com/desktop/scp.zip root@191.162.0.2:/writing/article
If you have not set up SSH client auto-authentication, you will be prompted for the VPS user password. It will be like this:
root@191.162.0.2’s password:
novel3.zip 100% 0 0.0KB / s 00:00
Suppose the server is configured to handle SSH connections on a port other than the default SSH port 22. In this case, you must specify that port using the -P option.
scp -P 2322 /users/Ded9com/desktop/scp.zip root@191.162.0.2: / writing / article
If you also want to rename the file during the transfer operation, your command would look like this (if you’re not using the default port, add –P followed by the appropriate port number):
scp /users/Ded9com/desktop/scp.zip root@191.162.0.2:/writing/article/howtoscp.zip
To copy a directory with multiple files or subdirectories, use the –r option followed by the same command line:
scp -r /users/Hostinger/desktop root@191.162.0.2:/writing/article
Transfer files from VPS to a local machine
This time we want to copy the scp.zip file from the same remote host to our local computer:
scp root@191.162.0.2:/writing/articles/SCP.zip Users/Hostinger/Desktop
By executing this command, it will show you the SSH login output that requires a password. It will only appear if you have disabled authentication or forced the SSH client to use a private key on your device.
Securely transfer a file between two VPS
To copy files from one server to another, you must enter the password for both after running the following command in your terminal:
scp root@191.162.0.2:/writing/article/scp.zip Ded9com@11.10.0.1:/publishing
The above command copies the source file /writing/article/scp.zip from the first host to the second host. To copy the folders, we use the -r option and specify the path of the folder instead of the file inside it.
Under normal circumstances, the file goes directly from the first remote server to the second server. However, if you want to redirect the operation through your device, add the -3 option as in the following command:
scp -3 root@191.162.0.2:/writing/article/scp.zip hostinger@11.10.0.1:/publishing
Conclusion
The SCP protocol makes it easier to transfer files between multiple servers on a network. It also allows you to copy files from one remote server to another without having to log in through an encrypted connection.
We hope this article has helped you understand how the SCP command works and how to use it correctly on Linux servers.