良许Linux教程网 干货合集 在 Linux 中传输文件文件夹的 10个 scp 命令

在 Linux 中传输文件文件夹的 10个 scp 命令

scp 命令的基本用法

下面的命令的意思是 把 source_file_name复制到destination_hostusername account下的destination_folder里。

> scp source_file_name username@destination_host:destination_folder

复制

有很多参数可以在scp命令中使用。以下是一些在日常操作中可能会用到的参数。

使用-v参数显示 scp 过程的详细信息

默认的scp命令不带参数,会在后台默默地复制文件。用户只有在过程结束或者出现错误时才能看到结果。

你可以使用 -v 参数来把调试信息输出到屏幕上。这样可以帮助你排查连接、认证和配置方面的问题。

rumenz@local $ scp -v Label.pdf rumenz@192.168.1.110:.
输出

复制文件时 scp 显示进度

Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -t .
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: Host '202.x.x.x' is known and matches the RSA host key.
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Next authentication method: password
rumenz@202.x.x.x's password:
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
Sending file modes: C0770 3760348 Label.pdf
Sink: C0770 3760348 Label.pdf
Label.pdf 100% 3672KB 136.0KB/s 00:27
Transferred: sent 3766304, received 3000 bytes, in 65.2 seconds
Bytes per second: sent 57766.4, received 46.0
debug1: Exit status 0

提供原始文件的修改时间、访问时间和模式

-p 参数将帮助你解决这个问题。估计时间和连接速度将出现在屏幕上。

rumenz@local $ scp -p Label.pdf rumenz@192.168.1.110:.
输出

scp 估计复制大文件所需的时间

> rumenz@202.x.x.x's password:
Label.pdf 100% 3672KB 126.6KB/s 00:29

使用 -C 参数加快文件传输速度

可以加快文件传输速度的参数之一是 -C 范围。这 -C 参数将随时随地压缩你的文件,独特的是压缩只发生在网络中。当文件到达目标服务器时,它将恢复到压缩前的原始大小。

> rumenz@local $ scp -pv messages.log rumenz@192.168.1.110:.
输出

scp 在不压缩的情况下传输文件

Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t .
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: identity file /home/pungki/.ssh/id_rsa type -1
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Trying private key: /home/pungki/.ssh/id_rsa
debug1: Next authentication method: password
rumenz@202.x.x.x's password:
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
debug1: Sending command: scp -v -p -t .
File mtime 1323853868 atime 1380425711
Sending file timestamps: T1323853868 0 1380425711 0
messages.log 100% 93MB 58.6KB/s 27:05
Transferred: sent 97614832, received 25976 bytes, in 1661.3 seconds
Bytes per second: sent 58758.4, received 15.6
debug1: Exit status 0

-C 允许压缩

rumenz@local $ scp -Cpv messages.log rumenz@192.168.1.110:.
输出

scp 使用压缩更快地传输文件

Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t .
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: identity file /home/pungki/.ssh/id_rsa type -1
debug1: Host '202.x.x.x' is known and matches the RSA host key.
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Next authentication method: publickey
debug1: Trying private key: /home/pungki/.ssh/id_rsa
debug1: Next authentication method: password
rumenz@202.x.x.x's password:
debug1: Enabling compression at level 6.
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
debug1: channel 0: new [client-session]
debug1: Sending command: scp -v -p -t .
File mtime 1323853868 atime 1380428748
Sending file timestamps: T1323853868 0 1380428748 0
Sink: T1323853868 0 1380428748 0
Sending file modes: C0600 97517300 messages.log
messages.log 100% 93MB 602.7KB/s 02:38
Transferred: sent 8905840, received 15768 bytes, in 162.5 seconds
Bytes per second: sent 54813.9, received 97.0
debug1: Exit status 0
debug1: compress outgoing: raw data 97571111, compressed 8806191, factor 0.09
debug1: compress incoming: raw data 7885, compressed 3821, factor 0.48

如果你要通过网络复制大量文件,-C 参数将帮助你减少所需的总时间。

压缩方法不适用于所有文件。当源文件已经被压缩时,就没有什么效果了。文件如.zip,.rar,pictures, 和.iso

更改 scp 密码以加密文件

默认情况下scp使用 AES-128 来加密文件。如果你想更改为其他密码对其进行加密,你可以使用 -c 范围。看看这个命令。

rumenz@local $ scp -c 3des Label.pdf rumenz@192.168.1.110:.

rumenz@202.x.x.x's password:
Label.pdf 100% 3672KB 282.5KB/s 00:13

上面的命令告诉scp使用3des algorithm来加密文件。请注意此参数使用 -c不是-C

使用 scp 命令限制带宽使用

另一个可能有用的参数是 -l 范围。这 -l 参数将限制使用的带宽制 。如果你执行自动化脚本来复制大量文件,这将很有用

rumenz@local $ scp -l 400 Label.pdf rumenz@192.168.1.110:.

rumenz@202.x.x.x's password:
Label.pdf 100% 3672KB 50.3KB/s 01:13

scp 默认带宽的单位是Kilobyte/sec(KB/s)。所以如果你想限制你的带宽scp最多只有50 KB/s,你需要将其设置为50 x 8=400.

指定要与 scp 一起使用的特定端口

scp正在使用端口22作为默认端口。但出于安全原因,你可以将端口更改为另一个端口. 例如,我们使用端口2249.

rumenz@local $ scp -P 2249 Label.pdf rumenz@192.168.1.110:.

rumenz@202.x.x.x's password:
Label.pdf 100% 3672KB 262.3KB/s 00:14

递归复制目录中的文件

有时我们需要复制目录和其中的所有文件,目录

rumenz@local $ scp -r documents rumenz@192.168.1.110:.

rumenz@202.x.x.x's password:
Label.pdf 100% 3672KB 282.5KB/s 00:13
scp.txt 100% 10KB 9.8KB/s 00:00

复> 制过程完成后,你将在目标服务器上找到一个名为documents及其所有文件。文件夹documents是自动创建的。

禁用进度表和警告 / 诊断消息

rumenz@local $ scp -q Label.pdf rumenz@192.168.1.110:.

rumenz@202.x.x.x's password:
rumenz@local $

可以看到,输入密码后,没有关于 scp 进程的信息。该过程完成后,你将再次看到提示。

通过代理使用 scp 复制文件

代理服务器通常用于办公环境。scp 本身没有配置代理。当你的环境使用代理时,你必须告诉scp 与代理进行通信。

例如代理地址是10.0.96.6代理端口是8080.代理还实现了用户身份验证。首先,你需要创建 ~/.ssh/config文件

ProxyCommand /usr/bin/corkscrew 10.0.96.6 8080 %h %p ~/.ssh/proxyauth

然后你需要创建文件 ~/.ssh/proxyauth 里面输入。

myusername:mypassword

前提是需要安装corkscrew

$ apt-get install corkscrew

Centos系统可以用yum安装corkscrew

# yum install corkscrew

由于 ~/.ssh/proxyauth 文件包含你的 usernamepassword 以明文格式,请确保该文件只能自己访问。

选择不同的ssh_config 文件

对于经常在公司网络和公共网络之间切换的移动用户来说,总是在scp中更改设置会很痛苦。

场景示例

代理在公司网络中使用,但不在公共网络中使用,并且你定期切换网络。

rumenz@local $ scp -F /home/pungki/proxy_ssh_config Label.pdf

rumenz@192.168.1.110:.
rumenz@202.x.x.x's password:
Label.pdf 100% 3672KB 282.5KB/s 00:13

默认情况下 ssh_config 每个用户的文件将被放置在 ~/.ssh/config。创建一个特定的 ssh_config 具有代理兼容性的文件将更容易在网络之间切换。

当你在公司网络,你可以使用 -F 范围。当你在公共网络上时,你可以不用-F参数。

以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

137e00002230ad9f26e78-265x300
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部