Linux Command Series: scp

1. What is the scp Command?

scp (secure copy) is a file transfer command based on the SSH protocol, used to securely copy files between a local computer and a remote computer. Unlike traditional FTP or SFTP, scp uses SSH (Secure Shell) to encrypt all data transfers, ensuring the security of the transfer process, making it a very commonly used file transfer tool in Linux.

  • Core Function: Copy files or directories between hosts.
  • Advantages: Encrypted transfer, secure and reliable; supports recursive copying of directories; integrated with SSH, simple and easy to use.

2. Syntax of the scp Command

scp [options] source_file target_path
  • Source File: The file or directory to be transferred, formatted as <span><span>[user@host:]path</span></span>.
  • Target Path: The destination for the file transfer, formatted similarly to <span><span>[user@host:]path</span></span>.
  • Common Formats: 1. From local to remote: scp local_file user@remote_host:/remote/path
  • 2. From remote to local: scp user@remote_host:/remote/path local_path
  • 3. From remote to remote: scp user1@host1:/path1 user2@host2:/path2

3. Common Options

scp supports various options to enhance its functionality. Here are some common options:

  • -r: Recursively copy entire directories (including subdirectories and files).
  • -p: Preserve the original attributes of the file (such as timestamps and permissions).
  • -P: Specify the SSH port (default is 22, if the remote host uses a non-standard port).
  • -i: Specify the private key file for key-based authentication.
  • -C: Enable compression to reduce the amount of data transferred.
  • -l: Limit bandwidth (e.g., <span><span>-l 512</span></span> indicates a speed limit of 512Kbps).
  • -v: Show detailed transfer process (for debugging).
  • -q: Quiet mode, hides progress information.

4. Examples of Using scp

Below are specific scenarios demonstrating the use of scp, assuming the local user is <span><span>localhost</span></span>, and the remote host is <span><span>[email protected]</span></span>.

1. Transfer Local File to Remote Host

Transfer the local <span><span>file.txt</span></span> to the remote host’s <span><span>/home/kn/</span></span> directory:

scp file.txt [email protected]:/home/kn/

2. Download Remote File to Local

Download from the remote host’s <span><span>/home/kn/file.txt</span></span> to the current local directory:

scp [email protected]:/home/kn/file.txt .

Note: . indicates the current directory, a specific directory can also be specified.

3. Recursively Copy Directory

Transfer the local directory datadir to the remote host’s <span><span>/home/kn/</span></span>:

scp -r datadir [email protected]:/home/kn/

4. Specify Non-Standard SSH Port

If the remote host uses port 3333

scp -P 3333 file.txt [email protected]:/home/kn/

5. Use Key Authentication

Authenticate by specifying the private key file (<span><span>~/.ssh/id_rsa</span></span>):

scp -i ~/.ssh/id_rsa file.txt [email protected]:/home/kn/

6. Transfer Files Between Remote Hosts

Transfer files between two remote hosts (must be initiated from local):

scp user1@host1:/path/to/file1 user2@host2:/path/to/dest

Note: Data will be routed through the local machine, ensure local access to both hosts.

7. Limit Transfer Speed

Limit transfer speed to 1024Kbps:

scp -l 1024 file.txt [email protected]:/home/kn/

8. Preserve File Attributes

Preserve the original timestamps and permissions of the file:

scp -p file.txt [email protected]:/home/kn/

5. Advantages and Disadvantages of the scp Command

Advantages:
  • High Security: Data is encrypted during transfer via the SSH protocol, ensuring security.
  • Simplicity: The command format is straightforward and easy to use.
  • Supports File and Directory Transfer: Can transfer not only single files but also entire directories.
  • Cross-Platform: Supports multiple operating systems including Linux, Mac, and Windows (via OpenSSH installation).
Disadvantages:
  • Does Not Support Resuming Transfers: If a file transfer is interrupted, it must be restarted.
  • No Graphical Interface:<span><span>scp</span></span> is a command-line tool, lacking a graphical interface, which may not suit all users.

6. Conclusion

<span><span>scp</span></span> is a very commonly used file transfer command in Linux systems, especially suitable for quickly and securely transferring files from a local computer to a remote server or downloading files from a remote server to local. Although it lacks some advanced features (like resuming transfers), its encryption via the SSH protocol ensures the security of the file transfer process. Mastering the <span><span>scp</span></span> command can greatly enhance your efficiency when working on Linux systems.

If anyone has further questions or doubts about the <span><span>scp</span></span> command, feel free to leave a comment! Let’s grow and improve together!

Leave a Comment