linux之间的文件互传-scp命令

scp是secure copy的简写,用于在Linux下进行远程拷贝文件或目录的命令,基于ssh登陆进行安全的远程文件拷贝命令,因此其传输是加密的。scp占用资源非常少,并不会提高多少系统负荷,在这一点上,另一个命令rsync就远远不及它了。虽然 rsync比scp会快一点,但当小文件众多的情况下,rsync会导致硬盘I/O非常高,而scp基本不影响系统正常使用。

命令参数

参数 说明
-1 强制scp命令使用协议ssh1
-2 强制scp命令使用协议ssh2
-4 强制scp命令只使用IPv4寻址
-6 强制scp命令只使用IPv6寻址
-B 使用批处理模式(传输过程中不询问传输口令或短语)
-C 允许压缩。(将-C标志传递给ssh,从而打开压缩功能)
-p 保留原文件的修改时间,访问时间和访问权限。
-q 不显示传输进度条。
-r 递归复制整个目录。
-v 详细方式显示输出。scp和ssh(1)会显示出整个过程的调试信息。这些信息用于调试连接,验证和配置问题。
-c cipher 以cipher将数据传输进行加密,这个选项将直接传递给ssh。
-F ssh_config 指定一个替代的ssh配置文件,此参数直接传递给ssh。
-i identity_file 从指定文件中读取传输时使用的密钥文件,此参数直接传递给ssh。
-l limit 限定用户所能使用的带宽,以Kbit/s为单位。
-P port 注意是大写的P, port是指定数据传输用到的端口号。
-S program 指定加密传输时所使用的程序。此程序必须能够理解ssh(1)的选项。

应用实例

从本地服务器复制到远程服务器

复制文件
  • 命令格式scp local_file remote_username@remote_ip:remote_folder,复制到远程的文件夹中
# 本地使用命令
[root@virtue ~]# scp 1.txt [email protected]:/tmp/
The authenticity of host '172.16.20.38 (172.16.20.38)' can't be established.
ECDSA key fingerprint is SHA256:z8KeBlt/fTyXBAibRQk0kGevF9E8HgNvBB6bLJDw5zY.
ECDSA key fingerprint is MD5:b5:b6:90:c7:70:46:06:aa:44:3d:35:74:03:88:ec:f7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.20.38' (ECDSA) to the list of known hosts.
[email protected]'s password: 
1.txt                                                                                  100%   37    23.6KB/s   00:00

# 远程计算机中显示
[root@ide ~]# ll /tmp/1.txt 
-rw-r--r--. 1 root root 37 1月  17 13:40 /tmp/1.txt
  • 命令格式scp local_file remote_username@remote_ip:remote_file,指定复制的文件名
# 本地使用命令
[root@virtue ~]# scp 1.txt [email protected]:/tmp/2.txt
[email protected]'s password: 
1.txt                                                                                  100%   37    21.9KB/s   00:00    

# 远程计算机中显示
[root@ide ~]# ll /tmp/2.txt 
-rw-r--r--. 1 root root 37 1月  17 13:43 /tmp/2.txt
[root@ide ~]# diff /tmp/1.txt /tmp/2.txt 
[root@ide ~]# 
  • 命令格式scp local_file remote_ip:remote_folder,使用本机登录用户作为用户名传输文件,复制文件到指定文件夹
# 本地使用命令
[root@virtue ~]# scp 1.txt 172.16.20.38:/tmp/
[email protected]'s password: 
1.txt                                                                                  100%   37    50.8KB/s   00:00    

# 远程计算机中显示
[root@ide ~]# ll /tmp/1.txt 
-rw-r--r--. 1 root root 37 1月  17 13:47 /tmp/1.txt

可以看到,文件被直接进行了覆盖,没有提示

  • 命令格式scp local_file remote_ip:remote_file,使用本机登录用户作为用户名传输文件,指定复制的文件名
# 本地使用命令
[root@virtue ~]# scp 1.txt 172.16.20.38:/tmp/3.txt
[email protected]'s password: 
1.txt                                                                                  100%   37     7.5KB/s   00:00    

# 远程计算机中显示
[root@ide ~]# ll /tmp/3.txt 
-rw-r--r--. 1 root root 37 1月  17 13:52 /tmp/3.txt
[root@ide ~]# diff /tmp/1.txt /tmp/3.txt 
[root@ide ~]# 
复制目录

与复制文件的命令格式基本相同,只不过需要增加-r参数

  • 命令格式scp -r local_folder remote_username@remote_ip:remote_folder,目录整体复制到远程目录内。
# 本地使用命令
[root@virtue ~]# scp -P 22 -r script [email protected]:/tmp
[email protected]'s password: 
kernel.txt                                                                             100%   59MB  80.0MB/s   00:00    
modify.py                                                                              100%  171   312.5KB/s   00:00    
ping.sh                                                                                100%  116   164.5KB/s   00:00    
setup.sh                                                                               100%  443   578.1KB/s   00:00    

# 远程计算机中显示
[root@ide ~]# ll /tmp/script/
总用量 60428
-rw-r--r--. 1 root root 61865984 1月  17 13:58 kernel.txt
-rw-r--r--. 1 root root      171 1月  17 13:58 modify.py
-rw-r--r--. 1 root root      116 1月  17 13:58 ping.sh
-rw-r--r--. 1 root root      443 1月  17 13:58 setup.sh
[root@ide ~]# 
  • 命令格式scp local_file remote_ip:remote_folder,使用本机登录用户作为用户名传输文件,复制目录到远程目录内。
# 本地使用命令
[root@virtue ~]# scp -P 22 -r script 172.16.20.38:/tmp
[email protected]'s password: 
kernel.txt                                                                             100%   59MB  79.4MB/s   00:00    
modify.py                                                                              100%  171   294.9KB/s   00:00    
ping.sh                                                                                100%  116   205.7KB/s   00:00    
setup.sh                                                                               100%  443   991.4KB/s   00:00    

# 远程计算机中显示
[root@ide ~]# ll /tmp/script/
总用量 60428
-rw-r--r--. 1 root root 61865984 1月  17 14:01 kernel.txt
-rw-r--r--. 1 root root      171 1月  17 14:01 modify.py
-rw-r--r--. 1 root root      116 1月  17 14:01 ping.sh
-rw-r--r--. 1 root root      443 1月  17 14:01 setup.sh

可以看到,目录被直接进行了覆盖,也没有提示

从远程服务器复制到本地服务器

从远程复制到本地的scp命令与上面的命令雷同,只要将从本地复制到远程的命令后面2个参数互换顺序就行了

复制文件
# 指定文件名
[root@virtue ~]# scp 172.16.20.38:/tmp/1.txt 1.txt 
[email protected]'s password: 
1.txt                                                                                  100%   37    47.1KB/s   00:00    
[root@virtue ~]# ll 1.txt 
-rw-r--r--. 1 root root 37 1月  17 14:02 1.txt

# 指定保存路径(本地路径是当前目录,命令最后有一个.目录符号)
[root@virtue ~]# scp 172.16.20.38:/tmp/1.txt .
[email protected]'s password: 
1.txt                                                                                  100%   37    32.6KB/s   00:00    
[root@virtue ~]# ll 1.txt 
-rw-r--r--. 1 root root 37 1月  17 14:03 1.txt

可以看到,无论那种复制方式都是直接将本地的文件进行了覆盖,没有提示信息。

复制目录
[root@virtue ~]# scp -P 22 -r 172.16.20.38:/tmp/script .
[email protected]'s password: 
kernel.txt                                                                             100%   59MB  52.8MB/s   00:01    
modify.py                                                                              100%  171   280.1KB/s   00:00    
ping.sh                                                                                100%  116   189.0KB/s   00:00    
setup.sh                                                                               100%  443   883.6KB/s   00:00    
[root@virtue ~]# ll script/
总用量 60428
-rw-r--r--. 1 root root 61865984 1月  17 14:09 kernel.txt
-rw-r--r--. 1 root root      171 1月  17 14:09 modify.py
-rw-r--r--. 1 root root      116 1月  17 14:09 ping.sh
-rw-r--r--. 1 root root      443 1月  17 14:09 setup.sh

可以看到目录也是直接进行了覆盖,没有提示信息,所以scp使用的时候一定要确定无误再使用,否则容易追悔莫及啊~~~

你可能感兴趣的:(linux之间的文件互传-scp命令)