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.
What is SCP?
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
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).
SCP Command Examples
Let's look at the usage examples to better understand how the SCP command works.
Copy Files from Local to Remote
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 user@123.456.789.0:/home/user/documents
Copy Files from Remote to Local
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 user@123.456.789.0:/home/user/documents/myfile.txt /home/user/downloads
Copy Directories
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 user@123.456.789.0:/home/user/backups
In this example:
-r
enables recursive copying./home/user/docs
is the local directory you want to copy.user@123.456.789.0
is the username and IP address of the remote server./home/user/backups
is the destination directory on the remote server.
Copy Files Using a Specific Port
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_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
.
Copy Files with Compression to Speed Up Transfer
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.
Copy Multiple Files with SCP
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.
Copy Files from One Remote Server to Another
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/Desktop
This 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
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_keys
file. - Ensure correct permissions for
.ssh
directory (700) andauthorized_keys
file (600).
On Hostman, you can copy SSH keys to servers using the control panel interface.
Verifying File Integrity
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
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.
Common SCP Error Messages
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
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.