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.
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.
The basic syntax of the SCP command is as follows:
scp [options] source destination
Here, 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).
Let's look at the usage examples to better understand how the SCP command works.
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/destination
Replace /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 [email protected]:/home/user/documents
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/destination
Replace username
, remote_host
, /path/to/remote/file
, and /path/to/local/destination
with the appropriate values.
Example:
scp [email protected]:/home/user/documents/myfile.txt /home/user/downloads
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/destination
Example:
scp -r /home/user/docs [email protected]:/home/user/backups
In this example:
-r
enables recursive copying./home/user/docs
is the local directory you want to copy.[email protected]
is the username and IP address of the remote server./home/user/backups
is the destination directory on the remote server.If the remote server uses a non-default SSH port, specify it with the -P
option:
scp -P 2222 /home/user/sample_example.tx [email protected]:/home/user/remote_dir
This command:
Uses port 2222
for the connection.
Transfers sample_example.txt
from the local user to the remote directory /home/user/remote_dir
.
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_dir
Here:
-C
enables compression during the transfer.
sample_example.txt
is the file being transferred to the remote server.
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_dir
Breaking down this command:
example/file1.txt
is the path and name of the first file you want to transfer.
example/file2.txt
is the path and name of the second file.
user@remote_host
represents the username and IP address or hostname of the remote server.
/home/user/remote_dir
is the directory on the remote server where the files will be copied.
To transfer a file directly from one remote server to another, use the following SCP command:
scp [email protected]:/home/alice/file.txt [email protected]:/home/bob/Desktop
This command includes:
[email protected]
: The username and IP address of the source remote server.
/home/alice/file.txt
: The file path and name on the source server.
[email protected]
: 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.
To set up SSH keys for authentication with SCP, follow these steps:
id_rsa.pub
) to the remote server's ~/.ssh/authorized_keys
file..ssh
directory (700) and authorized_keys
file (600).On Hostman, you can copy SSH keys to servers using the control panel interface.
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.
To optimize SCP transfer speed, consider the following tips:
Use compression with -C
option 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.
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.
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.