Mastering SCP Command on Linux
Secure Copy Protocol (SCP) is a command-line utility in Linux used for securely transferring files and directories between local and remote systems. This tutorial will provide a detailed guide on how to effectively use SCP on a Linux-based environment. Hostman offers a reliable managed Linux VPS for your projects.
What is SCP? Copy link
SCP, short for Secure Copy Protocol, is a command-line utility that allows users to securely transfer files and directories between local and remote systems using SSH (Secure Shell) protocol. It provides a secure and encrypted method for file transfer over a network.
Syntax of SCP Command Copy link
The basic syntax of the SCP command is as follows:
scp [options] source destinationHere, source refers to the file or directory you want to copy from, and destination is the location where you want to copy the files to, either locally or on a remote server.
Here are most common SCP parameters:
-
-r: Recursively copy entire directories. -
-P: Specify a custom SSH port for the connection. -
-i: Specify the identity file (private key) for SSH authentication. -
-v: Enable verbose mode to display detailed information during transfer. -
-C: Compress files during transfer to improve speed. -
-c: Select cipher to encrypt the data (e.g., aes128-ctr).
SCP Command Examples Copy link
Let's look at the usage examples to better understand how the SCP command works.
Copy Files from Local to Remote Copy link
To copy a file from your local machine to a remote server using SCP, use the following command:
scp /path/to/local/file username@remote_host:/path/to/destinationReplace /path/to/local/file with the actual path of the file on your local system, username with your username on the remote server, remote_host with the IP address or hostname of the remote server, and /path/to/destination with the desired location on the remote server.
Example:
scp /home/user/myfile.txt user@123.456.789.0:/home/user/documentsCopy Files from Remote to Local Copy link
To copy a file from a remote server to your local machine, use the following SCP command:
scp username@remote_host:/path/to/remote/file /path/to/local/destinationReplace username, remote_host, /path/to/remote/file, and /path/to/local/destination with the appropriate values.
Example:
scp user@123.456.789.0:/home/user/documents/myfile.txt /home/user/downloadsCopy Directories Copy link
To copy entire directories recursively using SCP, add the -r option to the command:
scp -r /path/to/local/directory username@remote_host:/path/to/destinationExample:
scp -r /home/user/docs user@123.456.789.0:/home/user/backupsIn this example:
-renables recursive copying./home/user/docsis the local directory you want to copy.user@123.456.789.0is the username and IP address of the remote server./home/user/backupsis the destination directory on the remote server.
Copy Files Using a Specific Port Copy link
If the remote server uses a non-default SSH port, specify it with the -P option:
scp -P 2222 /home/user/sample_example.tx user@123.456.789.0:/home/user/remote_dirThis command:
-
Uses port
2222for the connection. -
Transfers
sample_example.txtfrom the local user to the remote directory/home/user/remote_dir.
Copy Files with Compression to Speed Up Transfer Copy link
To speed up the file transfer by compressing the data, use the -C option:
scp -C Desktop/sample_example.txt user@remote_host:/home/user/remote_dirHere:
-
-Cenables compression during the transfer. -
sample_example.txtis the file being transferred to the remote server.
Copy Multiple Files with SCP Copy link
You can use SCP to transfer multiple files in one command. For instance, to send two files from your local machine to a remote server, you would use:
scp example/file1.txt example/file2.txt user@remote_host:/home/user/remote_dirBreaking down this command:
-
example/file1.txtis the path and name of the first file you want to transfer. -
example/file2.txtis the path and name of the second file. -
user@remote_hostrepresents the username and IP address or hostname of the remote server. -
/home/user/remote_diris the directory on the remote server where the files will be copied.
Copy Files from One Remote Server to Another Copy link
To transfer a file directly from one remote server to another, use the following SCP command:
scp alice@192.168.1.10:/home/alice/file.txt bob@192.168.1.20:/home/bob/DesktopThis command includes:
-
alice@192.168.1.10: The username and IP address of the source remote server. -
/home/alice/file.txt: The file path and name on the source server. -
bob@192.168.1.20: The username and IP address of the destination remote server. -
/home/bob/Desktop: The target directory on the destination server where the file will be stored.
Using SCP with SSH Keys Copy link
To set up SSH keys for authentication with SCP, follow these steps:
- Generate SSH key pair on your local machine using ssh-keygen.
- Copy the public key (
id_rsa.pub) to the remote server's~/.ssh/authorized_keysfile. - Ensure correct permissions for
.sshdirectory (700) andauthorized_keysfile (600).
On Hostman, you can copy SSH keys to servers using the control panel interface.
Verifying File Integrity Copy link
You can verify the integrity of copied files using checksums. Generate checksums for files using md5sum or sha256sum and compare them between source and destination.
SCP Transfer Speed Optimization Copy link
To optimize SCP transfer speed, consider the following tips:
-
Use compression with
-Coption for faster transfer of large files. -
Optimize encryption settings by using faster ciphers (e.g.,
aes128-ctr) for better performance. -
Optimize network settings for better throughput.
Common SCP Error Messages Copy link
Encountering errors while using SCP is common. Some common error messages include:
-
Permission denied (publickey): Check SSH key permissions and authentication. -
Connection refused: Ensure SSH service is running on the remote server. -
File not found: Verify paths and filenames for accuracy.
Conclusion Copy link
In conclusion, mastering the SCP command on Linux is crucial for efficient and secure file transfers between systems. By understanding its syntax, options, and best practices, users can streamline their file management tasks effectively.
By the way, with Hostman, you can run your workloads on efficient NL VPS that support low latency for EU-based users. Check this out, we have plenty of budget VPS hosting options for your projects.