一、作用

  1. Linux下进行远程拷贝文件的命令

二、选项

  • -r 递归复制整个目录
  • -p 保留原文件的修改时间,访问时间和访问权限
  • -v 详细显示传输过程
  • -q 不显示传输进度条

    三、实例

    ```shell

    主机1 配置 IP地址:192.168.11.102/24

    [root@localhost ~]# mkdir /jhl/data [root@localhost ~]# [root@localhost ~]# cat >> /jhl/data/test1.txt <<EOF

    12321312 12312432 23131242 321321312 EOF

    主机2 IP地址:192.168.11.105/24 下载

    [root@localhost data]# scp -rvp root@192.168.11.102:/jhl /data/ Executing: program /usr/bin/ssh host 192.168.11.102, user root, command scp -v -r -p -f /jhl OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 58: Applying options for debug1: Connecting to 192.168.11.102 [192.168.11.102] port 22. debug1: Connection established. debug1: permanently_set_uid: 0/0 debug1: SELinux support enabled debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_rsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_rsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_dsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_dsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_ecdsa type -1 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_ecdsa-cert type -1 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_ed25519 type -1 debug1: key_load_public: No such file or directory debug1: identity file /root/.ssh/id_ed25519-cert type -1 debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_7.4 debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4 debug1: match: OpenSSH_7.4 pat OpenSSH compat 0x04000000 debug1: Authenticating to 192.168.11.102:22 as ‘root’ debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: algorithm: curve25519-sha256 debug1: kex: host key algorithm: ecdsa-sha2-nistp256 debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: compression: none debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: compression: none debug1: kex: curve25519-sha256 need=64 dh_need=64 debug1: kex: curve25519-sha256 need=64 dh_need=64 debug1: expecting SSH2_MSG_KEX_ECDH_REPLY debug1: Server host key: ecdsa-sha2-nistp256 SHA256:JJSDYXSIw+FzLE5rnO8zRwMUq4h+0wgJu3hwpfeMXyg The authenticity of host ‘192.168.11.102 (192.168.11.102)’ can’t be established. ECDSA key fingerprint is SHA256:JJSDYXSIw+FzLE5rnO8zRwMUq4h+0wgJu3hwpfeMXyg. ECDSA key fingerprint is MD5:59:e5:6b:d1:62:fd:2f:02:c6:c6:73:a4:c2:52:dc:b5. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘192.168.11.102’ (ECDSA) to the list of known hosts. debug1: rekey after 134217728 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: rekey after 134217728 blocks debug1: SSH2_MSG_EXT_INFO received debug1: kex_input_ext_info: server-sig-algs= debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey,password debug1: Next authentication method: publickey debug1: Trying private key: /root/.ssh/id_rsa debug1: Trying private key: /root/.ssh/id_dsa debug1: Trying private key: /root/.ssh/id_ecdsa debug1: Trying private key: /root/.ssh/id_ed25519 debug1: Next authentication method: password root@192.168.11.102’s password: debug1: Authentication succeeded (password). Authenticated to 192.168.11.102 ([192.168.11.102]:22). debug1: channel 0: new [client-session] debug1: Requesting no-more-sessions@openssh.com debug1: Entering interactive session. debug1: pledge: network debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0 debug1: Sending environment. debug1: Sending env LANG = zh_CN.UTF-8 debug1: Sending command: scp -v -r -p -f /jhl File mtime 1655259153 atime 1655259146 Sending file timestamps: T1655259153 0 1655259146 0 Sink: T1655259153 0 1655259146 0 Entering directory: D0755 0 jhl Sink: D0755 0 jhl File mtime 1655259214 atime 1655259153 Sending file timestamps: T1655259214 0 1655259153 0 Sink: T1655259214 0 1655259153 0 Entering directory: D0755 0 data Sink: D0755 0 data File mtime 1655259214 atime 1655259214 Sending file timestamps: T1655259214 0 1655259214 0 Sink: T1655259214 0 1655259214 0 Sending file modes: C0644 37 test1.txt Sink: C0644 37 test1.txt test1.txt 100% 37 24.6KB/s 00:00
    Sink: E Sink: E debug1: client_input_channel_req: channel 0 rtype exit-status reply 0 debug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0 debug1: channel 0: free: client-session, nchannels 1 debug1: fd 0 clearing O_NONBLOCK debug1: fd 1 clearing O_NONBLOCK Transferred: sent 2216, received 3312 bytes, in 0.1 seconds Bytes per second: sent 24991.2, received 37351.5 debug1: Exit status 0 [root@localhost data]# ls jhl [root@localhost data]# tree . └── jhl └── data

      └── test1.txt
    

2 directories, 1 file

```shell
主机1IP :192.168.11.101
 下载路径:/a

主机2IP:192.168.11.102
[root@localhost ~]# scp -rpq root@192.168.11.101:/a /data/
root@192.168.11.101's password: 
[root@localhost ~]# ll /data/
总用量 0
drwxr-xr-x. 4 root  root  30 6月  17 14:41 a
主机1IP :192.168.11.101
 上传路径:/a
主机2IP:192.168.11.102
  上传文件/data/a
主机2
[root@localhost ~]# scp -rp /data/a  root@192.168.11.101:/a
root@192.168.11.101's password: 
pir.txt                                                                                                                    100%    0     0.0KB/s   00:00    
pir.txt                                                                                                                    100%    0     0.0KB/s   00:00    
pir                                                                                                                        100%    0     0.0KB/s   00:00    
pir.txt                                                                                                                    100%    0     0.0KB/s   00:00    
lk   

主机1
[root@localhost a]# ll 
总用量 0
drwxr-xr-x 4 root root 30 6月  17 14:41 a
drwxr-xr-x 4 root root 35 6月  17 14:41 mysq
drwxr-xr-x 3 root root 20 6月  17 14:53 ngin