How to transfer files in Linux quickly and safely? Classmates, learn about the scp command!

When using Linux, transferring files is often done. There are many ways and protocols for transferring files. The most commonly used ones are rsync, scp and sftp. In this article, Ruige will introduce scp to you. Whether you support Whether it's an engineer, system administrator, or developer, you may need to use scp to transfer files.

Let's get straight to it!

What is scp?

scp, English full name: Secure Copy Protocol, Chinese name: Secure Copy Protocol, used to securely transfer files between hosts on a computer network.

scp ensures data authenticity, encryption, and confidentiality by transferring files using the SSH (Secure Shell) mechanism. Therefore, data in transit is protected from snooping attacks. Clients can use this protocol to upload and download files and directories to the server. It requires a password or key for authentication. The default port for scp is TCP port 22, which is also the port for SSH . Default port.

The nice thing about the scp protocol is that you don't need to start an FTP session or explicitly log in to the remote host for file transfers.

How to use scp?

scp syntax

Copy files from local to remote host:

scp [选项] 源文件 用户名@目的主机:目的路径

Copy files from remote host to local:

scp [选项] 用户名@源主机:源文件路径 目标文件

There are many options here, let's execute the command scp --helpto see:

This looks incomplete, we execute the command man scp:

Here I choose a few commonly used explanations:

  • -C : Enable compression to improve transfer speed when copying.
  • -c <password>: password, by default, SCP uses "AES-128" to encrypt files, if you want to change the password, you need to use the -c option followed by the password name.
  • -i <identity_file>: Identification file or private key, typically key-based authentication is selected in a Linux environment.
  • -l <limit>: limit bandwidth, you can set the maximum bandwidth to be used, in Kbits/s.
  • -B: Use batch mode when copying.
  • -F <ssh_config>: Use to use a different ssh_config file when copying if you need to use a different network connection to the Linux system.
  • -P <port>: If the ssh port number of the destination host is different from the default port number 22, you need to use the -P option to specify the port number.
  • -p: Used to preserve file permissions, modification and access times when copying.
  • -q: Will execute SCP commands in quiet mode, will turn off the progress meter, and will not display ssh transfer progress, warning or diagnostic messages on the Linux terminal screen.
  • -r: Used to recursively copy files and directories.
  • -S <program>: Used to specify the program to use for the connection.
  • -v: v stands for verbose, which will gradually display the progress of the SCP command execution on the terminal screen. This is very helpful for debugging.

scp example

I created a new wljlsmz folder on the remote server, and created a new file hello_wljslmz in this folder, the file content is "hello, i am wljlsmz":

root@ecs-adf0-0003:/tmp# mkdir  wljslmz
root@ecs-adf0-0003:/tmp# cd wljslmz/
root@ecs-adf0-0003:/tmp/wljslmz# ls
root@ecs-adf0-0003:/tmp/wljslmz# touch hello_wljslmz
root@ecs-adf0-0003:/tmp/wljslmz# vim hello_wljslmz
root@ecs-adf0-0003:/tmp/wljslmz# ls
hello_wljslmz
root@ecs-adf0-0003:/tmp/wljslmz# more hello_wljslmz
hello , i am wljlsmz

Now I will download this file to the local through the scp command, and enter locally:

scp [email protected]:/tmp/wljslmz/hello_wljslmz 

Implementation process:

As shown in the above figure, the remote file has been successfully copied to the local, is it very simple?

Summarize

scp is a very important and basic command. This article introduces the basic knowledge, syntax, parameter options and examples of scp. I hope this article will help you understand and learn scp. If you have any questions, welcome to discuss with me in the comment area below , Finally, thank you for reading, your likes and forwarding are the driving force for my continuous creation!

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132145240