Linux(Centos 7.6)命令详解:scp
安全复制(远程文件复制程序)(secure copy (remote file copy program))。scp用于网络联通主机间的文件复制,它使用ssh进行数据传输,也使用ssh进行安全验证,所以scp在没有配置ssh互信时需要输入密码或口令它们用于身份验证。
·
1.命令作用
安全复制(远程文件复制程序)(secure copy (remote file copy program))。scp用于网络联通主机间的文件复制,它使用ssh进行数据传输,也使用ssh进行安全验证,所以scp在没有配置ssh互信时需要输入密码或口令它们用于身份验证。
- 可以本地文件目录进行复制
- 可以本地到远程的文件目录复制
- 可以远程到本地的文件目录复制
2.命令语法
Usage: scp [OPTION] [[user@]host1:]file1 ... [[user@]host2:]file2
or: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2
3.参数详解
OPTION:
- user@host1:file1,host1是复制源文件的主机名称或者IP地址,user是其用户名,如不指定则为执行命令的用户名称,file1是具体复制的源文件或者源目录;如果没有user@host1,仅源文件目录则为执行命令的本地文件
- user@host1:,host1是复制源文件的主机名称或者IP地址,user是其用户名,如不指定则为执行命令的用户名称,file2是具体复制的目标文件或者目标目录;如果没有user@host2,仅目标文件目录则为执行命令的本地文件(注: 源和目标都不指定user@host,则为本地复制与cp命令类似)
- -1,强制scp使用协议1
- -2,强制scp使用协议2
- -3,两台远程主机之间的副本通过本地主机传输复制。如果没有这个选项,数据文件将直接在两个远程主机之间复制(注意此参数选项禁用进度计)
- -4,强制scp只使用IPv4地址
- -6,强制scp只使用IPv6地址(以上协议参数一般不会使用到,了解即可)
- -B,选择批处理模式(防止询问密码或密码短语)(互信模式下多文件远程拷贝复制使用)
- -C,启用压缩,将-C标志传递给ssh以启用压缩
- -c cipher,选择要用于加密数据传输的方式,加密方式可指定"aes256-ctr"。这个选项直接传递给ssh
- -F ssh_config,为ssh指定另一个用户配置文件,默认指定的是/etc/ssh/ssh_config文件。这个选项直接传递给ssh
- -i identity_file,选择从中读取用于公钥身份验证的标识(私钥)的文件(验证时还是需要输入密码,效果不明显)。这个选项直接传递给ssh
- -l limit,限制使用的带宽,单位为Kbit/s,如4000Kbit/8=500KB(8Kbit/s=1KB/s)
- -o ssh_option,可用于以ssh_config中使用的格式向ssh传递选项。这对于指定没有单独scp命令行标志的选项非常有用。有关下面列出的选项的详细信息及其可能的值,请参见ssh_config。
- -P port,指定要在远程主机上连接的端口
- -p,保留原始文件的修改次数、访问时间和模式/访问权限
- -q,安静模式:禁用进度表以及来自ssh的警告和诊断消息
- -r,递归地复制整个目录
- -S program,用于加密连接的程序名称。程序必须理解ssh选项
- -v,详细模式,使得scp和ssh打印进度调试信息,有助于调试连接、身份验证和配置等问题
4.常用用例
4.1.常规远程复制
## 没有互信情况下,第一次复制会提示是否继续链接,之后会提示输入密码
[root@node2 Desktop]# scp file1.txt 192.168.118.201:/root/Desktop/
The authenticity of host '192.168.118.201 (192.168.118.201)' can't be established.
ECDSA key fingerprint is SHA256:9wqeNxYweSjzUT6jPi7PZP7s/4Rt7EG3Jm5QW8UHe90.
ECDSA key fingerprint is MD5:b3:08:5d:e3:2f:3a:4a:d9:1e:68:a5:9d:81:a6:fe:1d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.118.201' (ECDSA) to the list of known hosts.
root@192.168.118.201's password:
file1.txt 100% 50 22.5KB/s 00:00
[root@node2 Desktop]#
## 之后仅提示输入密码
[root@node2 Desktop]# scp file2.txt 192.168.118.201:/root/Desktop/
root@192.168.118.201's password:
file2.txt 100% 50 22.0KB/s 00:00
[root@node2 Desktop]#
4.2.批量远程复制(-B)
## 非互信场景
[root@node2 Desktop]# scp -B file2.txt 192.168.118.201:/root/Desktop/
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
lost connection
[root@node2 Desktop]#
## 互信场景
[root@node2 Desktop]# scp -B file2.txt file3.txt file4.txt file5.txt 192.168.118.201:/root/Desktop/
file2.txt 100% 50 31.9KB/s 00:00
file3.txt 100% 50 20.6KB/s 00:00
file4.txt 100% 50 41.2KB/s 00:00
file5.txt 100% 50 62.9KB/s 00:00
[root@node2 Desktop]#
4.3.保留文件基本属性(-p)
## 指定-p参数保留文件的属性进行远程拷贝
[root@node2 Desktop]# ll
total 214580
drwxr-xr-x. 2 root root 40 Feb 28 21:33 dir2
-rw-r--r--. 1 qwer qwer 219712350 Mar 1 17:34 file1.txt
-rw-r--r--. 1 qwer qwer 50 Mar 1 18:26 file2.txt
-rw-r--r--. 1 root root 50 Feb 28 21:47 file3.txt
-rw-r--r--. 1 qwer qwer 50 Feb 28 21:49 file4.txt
-rw-r--r--. 1 root root 50 Mar 1 15:24 file5.txt
[root@node2 Desktop]# scp -p file* 192.168.118.201:/root/Desktop/
root@192.168.118.201's password:
file1.txt 100% 210MB 104.7MB/s 00:02
file2.txt 100% 50 32.4KB/s 00:00
file3.txt 100% 50 36.4KB/s 00:00
file4.txt 100% 50 61.5KB/s 00:00
file5.txt 100% 50 36.3KB/s 00:00
[root@node2 Desktop]#
## 注意拷贝到nose1后的文件时间
[root@node1 Desktop]# ll
total 214580
-rw-r--r--. 1 root root 219712350 Mar 1 17:34 file1.txt
-rw-r--r--. 1 root root 50 Mar 1 18:26 file2.txt
-rw-r--r--. 1 root root 50 Feb 28 21:47 file3.txt
-rw-r--r--. 1 root root 50 Feb 28 21:49 file4.txt
-rw-r--r--. 1 root root 50 Mar 1 15:24 file5.txt
[root@node1 Desktop]#
4.4.限制带宽远程复制(-l)
## 不指定限制带宽
[root@node2 Desktop]# scp file* 192.168.118.201:/root/Desktop/
root@192.168.118.201's password:
file1.txt 100% 210MB 132.8MB/s 00:01
file2.txt 100% 50 17.6KB/s 00:00
file3.txt 100% 50 16.9KB/s 00:00
file4.txt 100% 50 20.4KB/s 00:00
file5.txt 100% 50 19.5KB/s 00:00
[root@node2 Desktop]#
## 指定限制带宽为5MB每秒,与上面的效率做一个对比,复制的效率及用时不同
[root@node2 Desktop]# scp -l 40000 file* 192.168.118.201:/root/Desktop/
root@192.168.118.201's password:
file1.txt 100% 210MB 5.0MB/s 00:42
file2.txt 100% 50 16.6KB/s 00:00
file3.txt 100% 50 20.0KB/s 00:00
file4.txt 100% 50 16.7KB/s 00:00
file5.txt 100% 50 16.5KB/s 00:00
[root@node2 Desktop]#
4.5.指定端口远程复制(-P)
## 指定端口,一般默认端口是22,如果是22可以不指定,如果是其他需要使用-P参数指定
[root@node2 Desktop]# scp -P 21 file* 192.168.118.201:/root/Desktop/
ssh: connect to host 192.168.118.201 port 21: No route to host
lost connection
[root@node2 Desktop]# scp -P 22 file1.txt 192.168.118.201:/root/Desktop/
root@192.168.118.201's password:
file1.txt 100% 210MB 137.3MB/s 00:01
[root@node2 Desktop]#
4.6.递归远程复制(-r)
[root@node2 Desktop]# tree dir1/
dir1/
├── dir12
│ ├── file1.txt
│ ├── file5.txt
│ └── ln_file1.txt
├── file1.txt
├── file2.txt
├── file3.txt
├── file4.txt
├── file5.txt
└── ln_file1.txt -> file1.txt
1 directory, 9 files
[root@node2 Desktop]# scp -r dir1/ 192.168.118.201:/root/Desktop/
root@192.168.118.201's password:
file1.txt 100% 210MB 128.6MB/s 00:01
file2.txt 100% 50 41.6KB/s 00:00
file3.txt 100% 50 56.0KB/s 00:00
file4.txt 100% 50 65.2KB/s 00:00
file5.txt 100% 50 52.4KB/s 00:00
ln_file1.txt 100% 210MB 127.2MB/s 00:01
file5.txt 100% 50 20.0KB/s 00:00
file1.txt 100% 210MB 112.8MB/s 00:01
ln_file1.txt 100% 210MB 114.9MB/s 00:01
[root@node2 Desktop]#
## 递归复制后,注意符号链接(软链接)复制后是普通文件
[root@node1 Desktop]# ll dir1/
total 429144
drwxr-xr-x. 2 root root 60 Mar 2 02:38 dir12
-rw-r--r--. 1 root root 219712350 Mar 2 02:37 file1.txt
-rw-r--r--. 1 root root 50 Mar 2 02:37 file2.txt
-rw-r--r--. 1 root root 50 Mar 2 02:37 file3.txt
-rw-r--r--. 1 root root 50 Mar 2 02:37 file4.txt
-rw-r--r--. 1 root root 50 Mar 2 02:37 file5.txt
-rw-r--r--. 1 root root 219712350 Mar 2 02:37 ln_file1.txt
[root@node1 Desktop]# ll dir1/dir12/
total 429132
-rw-r--r--. 1 root root 219712350 Mar 2 02:37 file1.txt
-rw-r--r--. 1 root root 50 Mar 2 02:37 file5.txt
-rw-r--r--. 1 root root 219712350 Mar 2 02:38 ln_file1.txt
[root@node1 Desktop]#
4.7.软链接远程复制
scp对软链接的远程拷贝不是很友好,可以先进行tar压缩打包,然后再进行远程拷贝,再然后在远程机器上进行解压
4.8.指定参数远程复制(-o)
注意:-o参数后可设置的参数见man scp输出的-o下面的说明内容,具体含义这里不做过多说明
[root@node2 Desktop]# scp -o LogLevel=DEBUG2 file1.txt 192.168.118.201:/root/Desktop/
... ...
debug1: Next authentication method: password
root@192.168.118.201's password:
... ...
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 219978300, received 40632 bytes, in 2.5 seconds
Bytes per second: sent 88266606.0, received 16303.6
debug1: Exit status 0
[root@node2 Desktop]#
更多推荐
所有评论(0)