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/

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

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

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

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

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

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

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

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

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

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

scp -rp .\test_ceshi root@192.168.255.130:/root/test
root@192.168.255.130's password:
ceshi.txt                             100%  158    51.9KB/s   00:00
test.txt                              100%   76    38.0KB/s   00:00
163.repo                              100%  508   250.2KB/s   00:00
redhat.repo                           100%  505   249.1KB/s   00:00
rhce.repo                             100%  136    53.6KB/s   00:00

[root@server-a test]# ls test_ceshi/
ceshi.txt  test.txt  yum.repos.d

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

PS E:\test> scp -rp root@192.168.255.130:/etc/yum.repos.d ./
root@192.168.255.130's password:
rhce.repo                                                                             100%  136    67.0KB/s   00:00
redhat.repo                                                                           100%  505   338.0KB/s   00:00
163.repo                                                                              100%  508   517.9KB/s   00:00
PS E:\test> ls


    目录: E:\test


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          2024/1/9      1:33                yum.repos.d
-a----         2024/1/11      0:22            158 ceshi.txt
-a----         2024/1/11      0:10             76 test.txt


PS E:\test> cd .\yum.repos.d\
PS E:\test\yum.repos.d> ls


    目录: E:\test\yum.repos.d


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----          2024/1/9      1:33            508 163.repo
-a----          2024/1/9      0:48            505 redhat.repo
-a----         2023/1/28     19:41            136 rhce.repo

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

PS E:\test> scp test.txt root@192.168.255.130:/root/test
root@192.168.255.130's password:
test.txt                                100%    76    29.8KB/s   00:00

[root@server-a test]# ls
ceshi_01  ceshi_02  test  test1  test2  test.txt  yum.repos.d
[root@server-a test]# cat test.txt 
君生我未生,我生君已老。
恨不生同时,日日与君好。

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

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

  • 16
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值