scp 安全的主机间复制文件

作用

scp命令来自于英文词组“secure copy”的缩写,其功能是用于基于 SSH 协议远程拷贝文件。

scp 命令可以在多台 Linux 系统之间复制文件或目录 ,有些类似于 cp 命令的功能,但复制的范围却不是本地,而是网络上另一台主机。

scp 命令通过 ssh 隧道来传输文件,认证机制也和 ssh 一致,因此该命令可以提供与 ssh 相同的安全性。

注意:如果需要,scp 命令会请求用户输入密码。

语法

scp [参数] [[用户@]来源主机:] 来源文件 [[用户@]目的主机:][目的文件]

参数

  • -1 使用ssh协议版本1-2

  • -2 使用ssh协议版本2

  • -4 使用ipv4

  • -6 使用ipv6

  • -B 以批处理模式运行

  • -C 使用压缩

  • -F 指定ssh配置文件

  • -l 指定宽带限制

  • -o 指定使用的ssh选项

  • -P 指定远程主机的端口号

  • -p 保留文件的修改时间,访问时间和权限模式

  • -q 不显示复制进度

  • -r 以递归方式复制

示例

(1)将本地当前目录的 ping.txt 文件复制到远程主机 192.168.116.51 的 /root/tmp 目录下

[root@hxstrive ~]# scp ping.txt 192.168.116.51:/root/tmp
The authenticity of host '192.168.116.51 (192.168.116.51)' can't be established.
ED25519 key fingerprint is SHA256:SgZXx0DQzxZVKm4QbyYW0F64Mb8ZUdJgZdVhU40Hzak.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.116.51' (ED25519) to the list of known hosts.
root@192.168.116.51's password:
ping.txt                                                                                        100%    0     0.0KB/s   00:00

(2)将本地当前目录中所有 txt 文件复制到远程主机 192.168.116.51 的 /root/tmp 目录下

[root@hxstrive ~]# scp *.txt 192.168.116.51:/root/tmp
root@192.168.116.51's password:
demo.txt                                                                                        100%   11    12.7KB/s   00:00
ping.txt                                                                                        100%    0     0.0KB/s   00:00
test.txt                                                                                        100%    5     6.3KB/s   00:00

(3)将远程主机 192.168.116.51 的 /root 目录下的 hello.c 文件复制到本地当前目录下

[root@hxstrive ~]# scp 192.168.116.51:/root/hello.c .
root@192.168.116.51's password:
hello.c                                                                                         100%    6     5.3KB/s   00:00

(4)将本地 test 目录复制到远程主机 192.168.116.51 的 /root 目录中

[root@hxstrive ~]# scp -r ./test 192.168.116.51:/root
root@192.168.116.51's password:
demo.txt                                                                                        100%   11    11.5KB/s   00:00
Hello.clss                                                                                      100%    0     0.0KB/s   00:00
Demo.java                                                                                       100%    0     0.0KB/s   00:00

(5)将远程主机 192.168.116.51 中的 /root/bak 目录复制到本地目录中

[root@hxstrive ~]# scp -r 192.168.116.51:/root/bak .
root@192.168.116.51's password:
demo.txt                                                                                        100%   11     6.4KB/s   00:00

(6)将本地 anaconda-ks.cfg 文件复制到远程主机 192.168.116.51 的 /root/tmp 目录中,指定要使用的传输用户身份,并保留原始文件的权限属性

[root@hxstrive ~]# scp -p anaconda-ks.cfg root@192.168.116.51:/root/tmp
root@192.168.116.51's password:
anaconda-ks.cfg                                                                                 100%  826   818.2KB/s   00:00

更多关于命令详细参考手册,请使用 man 命令或者 --help 参数获取帮助信息

关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号