Concept

scp yes secure copy Abbreviation , yes linux Based on ssh Log in for secure remote file copy command .scp It's encrypted. ,rcp It's not encrypted ,scp yes rcp The enhanced .

because scp The transmission is encrypted , It may affect the speed a little . in addition ,scp It doesn't take up resources , It will not increase the system load much , At this point ,rsync It's far less than that . although rsync Than scp It will be faster , But when there are lots of small files ,rsync It will cause the hard disk I/O Very high , and scp Basically does not affect the normal use of the system .

grammar

scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file][-l limit] [-o ssh_option] [-P port] [-S program][[[email protected]]host1:]file1 [...] [[[email protected]]host2:]file2

Simple writing :

scp [ Optional parameters ] file_source file_target

Parameter description :

-1:  mandatory scp Command usage protocol ssh1
-2:  mandatory scp Command usage protocol ssh2
-4:  mandatory scp The command only uses IPv4 Addressing
-6:  mandatory scp The command only uses IPv6 Addressing
-B:  Use batch mode ( Do not ask for transmission password or phrase during transmission )
-C:  Allow compression .( take -C The sign is passed on to ssh, To turn on compression )
-p:  Keep the modification time of the original document , Access time and access rights .
-q:  Do not show transfer progress bar .
-r:  Recursively copy the entire directory .
-v:  Detailed display output .scp and ssh(1) The debugging information of the whole process will be displayed . This information is used to debug connections , Validation and configuration issues .
-c cipher:         With cipher Encrypt the data transmission , This option will be passed directly to ssh.
-F ssh_config:     Designate an alternative ssh The configuration file , This parameter is passed directly to ssh.
-i identity_file:  The key file used to read the transfer from the specified file , This parameter is passed directly to ssh.
-l limit:          Limit the bandwidth that users can use , With Kbit/s In units of .
-o ssh_option:     If you are used to using ssh_config(5) Parameter passing method in ,
-P port:           Notice that it's capital P, port Is the port number used for data transmission
-S program:        Specify the program to use when encrypting the transmission . This procedure must be able to understand ssh(1) The option to .

Command instance

Preset conditions :

In this example vmware Opened two ubuntu.
ubuntu A Address :192.168.6.186, And turned on ssh The server
ubuntu B Address :192.168.6.111

install ssh The order is as follows :

apt-get install openssh-server

ssh Please refer to blog for detailed installation steps :《Ubuntu18.04 build ssh The server 》

Turn on ssh Server instructions 【ubuntu 192.168.6.186】:

  /etc/init.d/ssh restart

The results are as follows :
 Insert picture description here

example 1 Download the file

hold 192.168.6.186 Server's t.log Copy to the current directory of the current server ;

scp -r [email protected]:/home/peng/test/t.log ./

The results are as follows :
 Insert picture description here

example 2 Upload files

scp icons.png [email protected]:/root/ download    # Native file ,copy remote server 

Execution results :
 Insert picture description here

example 3 Download directory

  1. With root The user login ubuntu A Of ssh The server , And add the copy To local
 scp -r [email protected]:/home/peng/driver/list  /home/peng/test

Execution results 【ubuntu 192.168.6.111】:
 Insert picture description here

example 4 Upload directory

Put the local file copy To the server , Add user , Default current user

scp /home/peng/test/t.log 192.168.6.186:/home/peng/test

Execution results 【ubuntu 192.168.6.111】:
 Insert picture description here See the error message :Permission denied, This error is because the server A Folder /home/peng/test No permission to modify .
Get into ubuntu A
 Insert picture description here
Execute command again 【ubuntu 192.168.6.111】:
 Insert picture description here
【ubuntu 192.168.6.186】
 Insert picture description here

example 5 Designated port

Copy from local to remote server using specified port

scp -P 20022 -r /home/peng/test/list [email protected]:/home/peng/test

example 6 Use certificate

Login with certificate , And recursively download the remote directory to the local , It also specifies the required port number .

scp -r -i /Users/test/wx_Document/private.crt -P 2323 [email protected]:/data/htdocs/www/logs /Users/local