当前位置:   article > 正文

scp命令详解

scp命令

1、说明:

scp命令是secure copy的缩写,用于在linux下进行远程文件拷贝,类似的命令还有cp、rcp,不过cp只能在本机进行文件拷贝不支持跨服务器拷贝,rcp虽然支持远程文件拷贝,不过它不会对传输数据进行加密。

scp命令是用于通过ssh协议安全地将文件复制到远程系统和从远程系统复制文件到本地的命令。
使用ssh意味着它享有与ssh相同级别的数据加密,因此被认为是跨两个远程主机传输文件的安全方式。scp命令每一次复制都是完全复制,复制的效率不高,仅是用于第一次复制的时候使用,增量备份推荐使用rsync命令。

scp不仅可以在linux之间拷贝文件,还可以在windows和linux之间拷贝文件。

2、语法格式:

scp [可选参数] 本地主机文件 远程主机文件
scp [可选参数] 远程主机文件 本地主机文件

3、参数说明:

-C 压缩传输
-r 目录/递归复制整个目录
-P 大写P,指定传输的端口号
-p 小写p,传输后保留文件的原始属性
-q 不显示传输的进度条
-l 指定传输的时候占用的带宽。Kbit/s

4、示例(linux之间的复制):

拉取目录:scp -rp /etc/yum.repos.d root@192.168.255.129:/tmp/

  1. [root@server-a ~]# scp -rp root@192.168.255.129:/etc/yum.repos.d ./test
  2. root@192.168.255.129's password:
  3. redhat.repo 100% 358 229.0KB/s 00:00
  4. [root@server-a ~]# cd test/
  5. [root@server-a test]# ls
  6. test test1 test2 yum.repos.d

推送目录:scp -rp /etc/yum.repos.d root@192.168.255.129:/root/test

  1. [root@server-a test]# scp -rp /etc/yum.repos.d root@192.168.255.129:/root/test
  2. root@192.168.255.129's password:
  3. rhce.repo 100% 136 53.3KB/s 00:00
  4. redhat.repo 100% 505 119.2KB/s 00:00
  5. 163.repo 100% 508 317.4KB/s 00:00
  6. [root@localhost test]# ls
  7. yum.repos.d

拉取文件:scp root@192.168.255.129:/root/test/ceshi_02 /root/test/

  1. [root@server-a test]# scp root@192.168.255.129:/root/test/ceshi_02 /root/test/
  2. root@192.168.255.129's password:
  3. ceshi_02 100% 98 77.9KB/s 00:00
  4. [root@server-a test]# ls
  5. ceshi_01 ceshi_02 test test1 test2 yum.repos.d
  6. [root@server-a test]# cat ceshi_02
  7. 桃花坞里桃花庵,桃花庵下桃花仙。
  8. 桃花仙人种桃树,又摘桃花换酒钱。

推送文件:scp /root/test/ceshi_01 root@192.168.255.129:/root/test/

  1. [root@server-a test]# scp /root/test/ceshi_01 root@192.168.255.129:/root/test/
  2. root@192.168.255.129's password:
  3. ceshi_01 100% 49 28.3KB/s 00:00
  4. [root@localhost test]# cat ceshi_01
  5. 年年岁岁花相似,岁岁年年人不同。

5、示例(windows和linux之间的复制):

注:在目录所在文件夹中按shift+右击,点击“在此处打开Powershell窗口”

推送目录:scp -rp .\test_ceshi root@192.168.255.130:/root/test

  1. scp -rp .\test_ceshi root@192.168.255.130:/root/test
  2. root@192.168.255.130's password:
  3. ceshi.txt 100% 158 51.9KB/s 00:00
  4. test.txt 100% 76 38.0KB/s 00:00
  5. 163.repo 100% 508 250.2KB/s 00:00
  6. redhat.repo 100% 505 249.1KB/s 00:00
  7. rhce.repo 100% 136 53.6KB/s 00:00
  8. [root@server-a test]# ls test_ceshi/
  9. ceshi.txt test.txt yum.repos.d

拉取目录:scp -rp root@192.168.255.130:/etc/yum.repos.d ./

  1. PS E:\test> scp -rp root@192.168.255.130:/etc/yum.repos.d ./
  2. root@192.168.255.130's password:
  3. rhce.repo 100% 136 67.0KB/s 00:00
  4. redhat.repo 100% 505 338.0KB/s 00:00
  5. 163.repo 100% 508 517.9KB/s 00:00
  6. PS E:\test> ls
  7. 目录: E:\test
  8. Mode LastWriteTime Length Name
  9. ---- ------------- ------ ----
  10. d----- 2024/1/9 1:33 yum.repos.d
  11. -a---- 2024/1/11 0:22 158 ceshi.txt
  12. -a---- 2024/1/11 0:10 76 test.txt
  13. PS E:\test> cd .\yum.repos.d\
  14. PS E:\test\yum.repos.d> ls
  15. 目录: E:\test\yum.repos.d
  16. Mode LastWriteTime Length Name
  17. ---- ------------- ------ ----
  18. -a---- 2024/1/9 1:33 508 163.repo
  19. -a---- 2024/1/9 0:48 505 redhat.repo
  20. -a---- 2023/1/28 19:41 136 rhce.repo

推送文件:scp test.txt root@192.168.255.130:/root/test

  1. PS E:\test> scp test.txt root@192.168.255.130:/root/test
  2. root@192.168.255.130's password:
  3. test.txt 100% 76 29.8KB/s 00:00
  4. [root@server-a test]# ls
  5. ceshi_01 ceshi_02 test test1 test2 test.txt yum.repos.d
  6. [root@server-a test]# cat test.txt
  7. 君生我未生,我生君已老。
  8. 恨不生同时,日日与君好。

拉取文件:scp root@192.168.255.130:/root/test/ceshi.txt ./

  1. PS E:\test> scp root@192.168.255.130:/root/test/ceshi.txt ./
  2. root@192.168.255.130's password:
  3. ceshi.txt 100% 158 77.9KB/s 00:00
  4. PS E:\test> cat .\ceshi.txt
  5. 我住长江头,君住长江尾。日日思君不见君,共饮长江水。
  6. 此水几时休,此恨何时已。只愿君心似我心,不负相思意。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/748368
推荐阅读
相关标签
  

闽ICP备14008679号