天天看点

Linux命令之scp命令一、命令简介二、使用示例三、参数解释

一、命令简介

  scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的。可能会稍微影响一下速度。当你服务器硬盘变为只读 read only system时,用scp可以帮你把文件移出来。另外,scp还非常不占资源,不会提高多少系统负荷,在这一点上,rsync就远远不及它了。虽然 rsync比scp会快一点,但当小文件众多的情况下,rsync会导致硬盘I/O非常高,而scp基本不影响系统正常使用。

  scp命令格式:scp [参数] [原路径] [目标路径]

二、使用示例

1、将本地文件传送到远端服务器

#scp file remote_host_ip:/dir

[[email protected] opt]# scp wordpress-5.3.2.zip 192.168.0.125:/opt/

[email protected]’s password:

wordpress-5.3.2.zip 100% 13MB 70.5MB/s 00:00

2、将本地文件夹传送到远端服务器

#scp -r dir remote_host_ip:/dir

[[email protected] opt]# scp -r test 192.168.0.125:/opt/

[email protected]’s password:

hi.txt 100% 0 0.0KB/s 00:00

[[email protected] ~]# ll /opt/test/

total 0

-rw-r–r-- 1 root root 0 Jan 13 17:01 hi.txt

[[email protected] ~]# ll /opt/

total 13116

drwxr-xr-x. 2 root root 6 Oct 31 2018 rh

drwxr-xr-x 2 root root 20 Jan 13 17:01 test

-rw-r–r-- 1 root root 13427353 Jan 13 16:57 wordpress-5.3.2.zip

3、将远端文件传送到本地

#scp remote_host_ip:/dir/filename local_dir

[[email protected] opt]# rm -rf wordpress-5.3.2.zip*

[[email protected] opt]# ll

total 4

-rw-r–r--. 1 root root 453 Jan 11 06:15 download.log

drwxr-xr-x. 2 root root 20 Jan 13 01:00 test

[[email protected] opt]# scp 192.168.0.125:/opt/wordpress-5.3.2.zip ./

[email protected]’s password:

wordpress-5.3.2.zip 100% 13MB 56.5MB/s 00:00

[[email protected] opt]# ll

total 13120

-rw-r–r--. 1 root root 453 Jan 11 06:15 download.log

drwxr-xr-x. 2 root root 20 Jan 13 01:00 test

-rw-r–r--. 1 root root 13427353 Jan 13 01:03 wordpress-5.3.2.zip

4、将远端文件夹传送到本地

#scp -r remote_host_ip:/dir/dir local_dir/

[[email protected] opt]# scp -r 192.168.0.125:/opt/test2 ./

[email protected]’s password:

hi.txt 100% 0 0.0KB/s 00:00

[[email protected] opt]# ll

total 13120

-rw-r–r--. 1 root root 453 Jan 11 06:15 download.log

drwxr-xr-x. 2 root root 20 Jan 13 01:00 test

drwxr-xr-x. 2 root root 20 Jan 13 01:05 test2

-rw-r–r--. 1 root root 13427353 Jan 13 01:03 wordpress-5.3.2.zip

5、使用通配符批量传送

[[email protected] opt]# ll

total 13120

-rw-r–r--. 1 root root 453 Jan 11 06:15 download.log

drwxr-xr-x. 2 root root 20 Jan 13 01:00 test

drwxr-xr-x. 2 root root 20 Jan 13 01:05 test2

-rw-r–r--. 1 root root 0 Jan 13 01:07 test3

-rw-r–r--. 1 root root 13427353 Jan 13 01:03 wordpress-5.3.2.zip

[[email protected] opt]# scp -r test* 192.168.0.125:/tmp/

[email protected]’s password:

hi.txt 100% 0 0.0KB/s 00:00

hi.txt 100% 0 0.0KB/s 00:00

test3

6、使用-l参数限制传送速率

#scp -l 速率 其中速率单位为Kbit/s,800Kbit/s=100KB/s

[[email protected] opt]# scp -l 800 wordpress-5.3.2.zip 192.168.0.125:/tmp/

[email protected]’s password:

wordpress-5.3.2.zip 100% 13MB 99.9KB/s 02:11

7、使用-P端口指定远端服务器ssh端口为自定义端口

#scp -P 60022 指定远端服务器scp服务端口

Linux命令之scp命令一、命令简介二、使用示例三、参数解释

8、指定scp使用的用户

#scp file [email protected]_host_ip:/dir 默认与本地登录用户一致

[[email protected] opt]# scp wordpress-5.3.2.zip [email protected]:/home/wuhs/

[email protected]’s password:

wordpress-5.3.2.zip

9、保留原文档的时间等属性传送

#scp -p 默认安装新创建文件时间,使用-p参数保留原文档时间和权限

Linux命令之scp命令一、命令简介二、使用示例三、参数解释
Linux命令之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为单位。

-o ssh_option: 如果习惯于使用ssh_config(5)中的参数传递方式,

-P port:注意是大写的P, port是指定数据传输用到的端口号

-S program: 指定加密传输时所使用的程序。此程序必须能够理解ssh(1)的选项。

继续阅读