[Shell command collection file management] Tutorial on using the scp command to copy files between remote hosts via Linux ssh


Shell command column: Full analysis of Linux Shell commands


describe


scpcommand is a command used in Linux to copy files between remote hosts. Its full name is "secure copy", which enables secure file transfer through the SSH protocol.

scpThe basic syntax of the command is as follows:

scp [选项] [源文件] [目标地址]

Commonly used options are:

  • -r: Recursively copies a directory and its contents.
  • -P:Specify the port number of the remote host.
  • -i:Specifies the private key file used for authentication.
  • -v: Display detailed debugging information.

The source file can be a local file or a file on a remote host. The destination address can be a local directory or a directory on the remote host.

Here are some common usage examples:

  1. Copy files from local to remote host:
scp localfile.txt user@remotehost:/path/to/destination

This command localfile.txtcopies files to a directory remotehoston the remote host /path/to/destination.

  1. Copy files from remote host to local:
scp user@remotehost:/path/to/file.txt .

This command copies files remotehoston the remote host /path/to/file.txtto the current directory.

  1. Copy an entire directory recursively:
scp -r user@remotehost:/path/to/directory .

This command recursively copies a directory and its contents remotehoston the remote host to the current directory./path/to/directory

scpCommands are transmitted using the SSH protocol, so the remote host needs to support the SSH service. It provides secure file transfer capabilities to easily copy files between different hosts.


Syntax format

scp [参数] [源文件] [目标地址]

Parameter Description

  • -r:Copy the entire directory recursively.
  • -P:Specify the port number.
  • -i: Specify a private key file for authentication.
  • -v: Display detailed debugging information.

error condition

  • If the source file does not exist, No such file or directoryan error will be displayed.
  • If the target address does not exist, No such file or directoryan error will be displayed.
  • If there are insufficient permissions to access the source file or destination address, Permission deniedan error will be displayed.

Precautions

There are some things to note when using the Linux Shell’s scp command:

  1. Ensure the correctness of the target address: When using the scp command, the target address can be the path of the local file system or the address of the remote host. If it is the address of a remote host, you need to ensure that the IP address or domain name of the target host is correct and has the correct access rights.

  2. Ensure the source file exists: When using the scp command, you need to ensure that the source file exists in the specified path. If the source file does not exist, the scp command will report an error and exit.

  3. Copy the directory using recursion: If you want to copy the entire directory, you need to use -rparameters for recursive copying. Otherwise, the scp command will only copy a single file.

  4. Specify port number: If the SSH service of the target host is listening on a non-default port, you need to use -Pparameters to specify the port number. For example, scp -P 2222 file.txt user@remote:/path/to/destination.

  5. Use private key for authentication: If the target host requires a private key for authentication, you can use the -iparameter to specify the path to the private key file. For example, scp -i ~/.ssh/id_rsa file.txt user@remote:/path/to/destination.

  6. Display debugging information: If you need to view detailed debugging information, you can use -vparameters. For example, scp -v file.txt user@remote:/path/to/destination.

  7. Pay attention to file permissions: When copying files, you need to ensure that the directories and files on the target host have the correct permissions. Without sufficient permissions, replication may fail.

  8. Pay attention to special characters in file names: If the source file or destination address contains special characters, such as spaces, quotation marks, etc., you need to use quotation marks or escape characters for correct processing. For example, scp "file name.txt" user@remote:"/path/with\ spaces".

  9. Pay attention to the stability of the network connection: When transferring files, you need to ensure the stability of the network connection to avoid transmission interruptions or errors.

  10. Pay attention to file size: When copying large files, you need to ensure that the disk space of the target host is sufficient to avoid copy failure or insufficient disk space of the target host.

In short, when using the scp command, you need to carefully check the parameters and path of the command, and ensure that the network connection is stable to ensure that the file can be safely and accurately transferred to the target address.


underlying implementation

The underlying scp command of Linux Shell transfers files through the SSH protocol. Specifically, the scp command uses the ssh command for authentication and encrypted communication, and uses SSH's SFTP subsystem for file transfers.

When executing the scp command, a secure connection with the target host will be established first through the SSH protocol. This process involves authentication and key exchange to ensure the security of communications.

Once a secure connection is established, the scp command creates an SFTP session between the local and remote hosts. SFTP (SSH File Transfer Protocol) is a subsystem of the SSH protocol and is used for file transfer and management.

In an SFTP session, the scp command uses the SFTP put and get commands to upload and download files. Specifically, when using the scp command to copy a local file to a remote host, it calls the SFTP put command to send the file from the local to the remote host. When using the scp command to copy a file from the remote host to the local, it will call the SFTP get command to obtain the file from the remote host and save it to the local.

During the file transfer process, the scp command uses the encryption and data integrity protection functions provided by the SSH protocol to ensure the security of the transfer. It can also take advantage of SSH's compression capabilities to speed up file transfers.

In summary, the scp command implements secure file transfer functions through the SSH protocol and SFTP subsystem. It provides a simple and efficient way to copy files between local host and remote host.


Example

Example 1

Copy files from local to a specified directory on the remote host:

scp localfile.txt user@remotehost:/path/to/destination

This command copies local files to a directory on localfile.txtthe remote host .remotehost/path/to/destination

Example 2

Copy files from remote host to local:

scp user@remotehost:/path/to/file.txt .

This command copies files remotehoston the remote host /path/to/file.txtto the current directory.

Example three

Copy the entire directory to the remote host:

scp -r /path/to/directory user@remotehost:/path/to/destination

/path/to/directoryThis command recursively copies a local directory and its contents to a directory remotehoston the remote host /path/to/destination.

Example 4

Copy files from the remote host to the specified local directory:

scp user@remotehost:/path/to/file.txt /path/to/local/destination

This command copies files remotehoston the remote host /path/to/file.txtto a local /path/to/local/destinationdirectory.

Example five

Specify the port number to copy files:

scp -P 2222 user@remotehost:/path/to/file.txt .

This command copies the files remotehoston the remote host /path/to/file.txtto the current directory while connecting using port number 2222.

Example 6

Authenticate using private key file:

scp -i private_key.pem user@remotehost:/path/to/file.txt .

This command copies files remotehoston the remote host /path/to/file.txtto the current directory while using private_key.pemthe private key file for authentication.

Example 7

Show detailed debugging information:

scp -v user@remotehost:/path/to/file.txt .

This command copies files remotehoston the remote host /path/to/file.txtto the current directory and displays detailed debugging information.



Conclusion

During our exploration, we have gained an in-depth understanding of the power and wide application of Shell commands. However, learning these techniques is just the beginning. The real power comes from how you integrate them into your daily routine to increase efficiency and productivity.

Psychology tells us that learning is a continuous and active process. So, I encourage you to not only read and understand these commands, but also practice them. Try creating your own commands and gradually master shell programming so that it becomes part of your daily routine.

Also, remember that sharing is a very important part of the learning process. If you found this blog helpful, please feel free to like and leave a comment. Sharing the problems or interesting experiences you encountered when using Shell commands can help more people learn from them.
In addition, I also welcome you to bookmark this blog and come back to check it anytime. Because review and repeated practice are also the keys to consolidating knowledge and improving skills.

Finally, remember: anyone can become a shell programming expert through continued study and practice. I look forward to seeing you make further progress on this journey!


Read my CSDN homepage and unlock more exciting content: Bubble’s CSDN homepage

Insert image description here

Guess you like

Origin blog.csdn.net/qq_21438461/article/details/131362741