十个 SCP 传输命令例子

Linux系统管理员应该很熟悉CLI环境,因为通常在Linux服务器中是不安装GUI的。SSH可能是Linux系统管理员通过远程方式安全管理服务器的最流行协议。在SSH命令中内置了一种叫SCP的命令,用来在服务器之间安全传输文件。

以下命令可以解读为:用“username account”“拷贝 source file name”到“destination host”上的“destination folder”里。

SCP命令的基本语法

  1. scp source_file_name username@destination_host:destination_folder

SCP命令有很多可以使用的参数,这里指的是每次都会用到的参数。

用-v参数来提供SCP进程的详细信息

不带参数的基本SCP命令会在后台拷贝文件,除非操作完成或者有错误出现,否则用户在界面上是看不到任何提示信息的。你可以用“-v”参数来在屏幕上打印出调试信息,这能帮助你调试连接、认证和配置的一些问题。

  1. pungki@mint ~/Documents $ scp -v Label.pdf mrarianto@202.x.x.x:.

部分输出

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

从源文件获取修改时间、访问时间和模式

“-p”参数会帮到把预计的时间和连接速度会显示在屏幕上。

  1. pungki@mint ~/Documents $ scp -p Label.pdf mrarianto@202.x.x.x:.

部分输出

  1. mrarianto@202.x.x.x's password:
  2. Label.pdf 100% 3672KB 126.6KB/s 00:29

用-C参数来让文件传输更快

有一个参数能让传输文件更快,就是“-C”参数,它的作用是不停压缩所传输的文件。它特别之处在于压缩是在网络传输中进行,当文件传到目标服务器时,它会变回压缩之前的原始大小。

来看看这些命令,我们使用一个93 Mb的单一文件来做例子。

  1. pungki@mint ~/Documents $ scp -pv messages.log mrarianto@202.x.x.x:.

部分输出

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

不用“-C”参数来拷贝文件,结果用了1661.3秒,你可以比较下用了“-C”参数之后的结果。

  1. pungki@mint ~/Documents $ scp -Cpv messages.log mrarianto@202.x.x.x:.

部分输出

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

看到了吧,压缩了文件之后,传输过程在162.5秒内就完成了,速度是不用“-C”参数的10倍。如果你要通过网络拷贝很多份文件,那么“-C”参数能帮你节省掉很多时间。

有一点我们需要注意,这个压缩的方法不是适用于所有文件。当源文件已经被压缩过了,那就没办法再压缩很多了。诸如那些像.zip,.rar,pictures和.iso的文件,用“-C”参数就没什么意义。

选择其它加密算法来加密文件

SCP默认是用“AES-128”加密算法来加密传输的。如果你想要改用其它加密算法来加密传输,你可以用“-c”参数。我们来瞧瞧。

  1. pungki@mint ~/Documents $ scp -c 3des Label.pdf mrarianto@202.x.x.x:.
  2. mrarianto@202.x.x.x's password:
  3. Label.pdf 100% 3672KB 282.5KB/s 00:13

上述命令是告诉SCP用3des algorithm来加密文件。要注意这个参数是“-c”(小写)而不是“-C“(大写)。

限制带宽使用

还有一个很有用的参数是“-l”参数,它能限制使用带宽。如果你为了拷贝很多文件而去执行了一份自动化脚本又不希望带宽被SCP进程耗尽,那这个参数会非常管用。

  1. pungki@mint ~/Documents $ scp -l 400 Label.pdf mrarianto@202.x.x.x:.
  2. mrarianto@202.x.x.x's password:
  3. Label.pdf 100% 3672KB 50.3KB/s 01:13

在“-l”参数后面的这个400值意思是我们给SCP进程限制了带宽为50 KB/秒。有一点要记住,带宽是以千比特/秒 (kbps)表示的,而8 比特等于1 字节。

因为SCP是用千字节/秒 (KB/s)计算的,所以如果你想要限制SCP的最大带宽只有50 KB/s,你就需要设置成50 x 8 = 400。

指定端口

通常SCP是把22作为默认端口。但是为了安全起见SSH 监听端口改成其它端口。比如说,我们想用2249端口,这种情况下就要指定端口。命令如下所示。

  1. pungki@mint ~/Documents $ scp -P 2249 Label.pdf mrarianto@202.x.x.x:.
  2. mrarianto@202.x.x.x's password:
  3. Label.pdf 100% 3672KB 262.3KB/s 00:14

确认一下写的是大写字母“P”而不是“p“,因为“p”已经被用来保留源文件的修改时间和模式(LCTT 译注:和 ssh 命令不同了)。

递归拷贝文件和文件夹

有时我们需要拷贝文件夹及其内部的所有文件/子文件夹,我们如果能用一条命令解决问题那就更好了。SCP用“-r”参数就能做到。

  1. pungki@mint ~/Documents $ scp -r documents mrarianto@202.x.x.x:.
  2. mrarianto@202.x.x.x's password:
  3. Label.pdf 100% 3672KB 282.5KB/s 00:13
  4. scp.txt 100% 10KB 9.8KB/s 00:00

拷贝完成后,你会在目标服务器中找到一个名为“documents”的文件夹,其中就是所拷贝的所有文件。“documents”是系统自动创建的文件夹。

禁用进度条和警告/诊断信息

如果你不想从SCP中看到进度条和警告/诊断信息,你可以用“-q”参数来静默它们,举例如下。

  1. pungki@mint ~/Documents $ scp -q Label.pdf mrarianto@202.x.x.x:.
  2. mrarianto@202.x.x.x's password:
  3. pungki@mint ~/Documents $

正如你所看到的,在你输入密码之后,没有任何关于SCP进度的消息反馈。进度完成后,你也看不到任何提示。

用SCP通过代理来拷贝文件

代理服务器经常用于办公环境,SCP自然是没有经过代理方面的配置的。当你的环境正在使用代理,那么你就必须要“告诉”SCP与代理关联起来。

场景如下:代理的地址是10.0.96.6,端口是8080。该代理还实现了用户认证功能。首先,你需要创建一个“~/.ssh/config”文件,其次把以下命令输入进该文件。

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

接着你需要创建一个同样包括以下命令的“~/.ssh/proxyauth”文件。

  1. myusername:mypassword

然后你就可以像往常一样使用SCP了。

请注意corkscrew可能还没有安装在你的系统中。在我的Linux Mint中,我需要首先先用标准Linux Mint安装程序来安装它。

  1. $ apt-get install corkscrew

对于其它的一些基于yum安装的系统,用户能用以下的命令来安装corkscrew。

  1. # yum install corkscrew

还有一点就是因为“~/.ssh/proxyauth”文件中以明文的格式包含了你的“用户名”和“密码”,所以请确保该文件只能你来查看。

选择不同的ssh_config文件

对于经常在公司网络和公共网络之间切换的移动用户来说,一直改变SCP的设置显然是很痛苦的。如果我们能放一个保存不同配置的ssh_config文件来匹配我们的需求那就很好了。

以下是一个简单的场景

代理是被用来在公司网络但不是公共网络并且你会定期切换网络时候使用的。

  1. pungki@mint ~/Documents $ scp -F /home/pungki/proxy_ssh_config Label.pdf
  2. mrarianto@202.x.x.x:.
  3. mrarianto@202.x.x.x's password:
  4. Label.pdf 100% 3672KB 282.5KB/s 00:13

默认情况下每个用户会把“ssh_config”文件放在“~/.ssh/config“路径下。用兼容的代理创建一个特定的“ssh_config”文件,能让你切换网络时更加方便容易。

当你处于公司网络时,你可以用“-F”参数,当你处于公共网络时,你可以忽略掉“-F”参数。

以上就是关于SCP的全部内容了,你可以查看SCP的man页面来获取更多内容,请随意留下您的评论及建议。

原文发布时间:2014-12-25

本文来自云栖合作伙伴“linux中国”

十个 SCP 传输命令例子相关推荐

  1. Linux SSH远程文件/文件夹传输命令scp

    相信各位VPSer在使用VPS时会常常在不同VPS间互相备份数据或者转移数据,大部分情况下VPS上都已经安装了Nginx或者类似的web server,直接将要传输的文件放到web server的文件 ...

  2. Linux SSH远程文件/目录传输命令scp

    一.scp是什么? scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响 ...

  3. Linux下几种文件传输命令 sz rz sftp scp

    vbs自动启动程序 64位系统编译的程序,在32位系统中运行会有问题 Linux下几种文件传输命令 sz rz sftp scp 2011-10-28 16:08:47|  分类: 默认分类 |  标 ...

  4. 在linux下一般用scp这个命令来通过ssh传输文件

    在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件 scp username@servername:/path/filename /var/www/local_dir(本 ...

  5. Linux下利用ssh远程文件传输 传输命令 scp

    在linux下一般用scp这个命令来通过ssh传输文件. 一.scp是什么? scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进 ...

  6. linux传输文件命令sftp,Linux下几种文件传输命令 sz rz sftp scp

    Secure Ftp 是一个基于SSH安全协议的文件传输管理工具.因为它是基于SSH的,会在传输过程当中对用户的密码.数据等敏感信息进行加密,所以能够有效的防止用户信息在传输的过程当中被窃取,比FTP ...

  7. 使用ssh的工具scp传输文件

    零.scp说明 SCP(Secure Copy)是一个在各个主机之间进行复制或者文件传输的一个命令行工具. 它使用一种同ssh一样的安全机制.事实上,它在后台使用ssh连接来进行文件的传输. scp既 ...

  8. scp 传输文件到另一台服务器

    文章目录 一.scp 命令简介 二.简单测试 scp 命令 1.启动 ssh 服务 3.scp 命令格式 2.运行 scp 命令 三.scp 免密码传输文件 四.docker 备份 MySQL 到另一 ...

  9. 使用scp传输文件给linux服务器,出现Permission denied(publickey) 的解决办法

    原因:scp传输文件基于ssh拷贝服务,但是ssh在没有密钥登录的情况下,禁用了密码登录,所以需要我们修改ssh的配置文件 解决方法: 1.通过软件(putty.xshell等)或者浏览器控制台远程连 ...

最新文章

  1. OpenCL™(开放计算语言)概述
  2. 人生苦短,我用python+vscode
  3. caffe---ubuntu1604下anaconda2.5的尝试----失败,建议使用系统的python系统,避免各种各样的陷阱...
  4. 微型计算机主机的主要组成部件是,微型计算机主机的主要组成部分有
  5. 【月报】Java知音的二月汇总
  6. span 超出部分换行
  7. Linux文章大合集
  8. 五种酷炫代码雨的源代码
  9. 校验电话号码 手机号码正则表达式
  10. w10如何共享计算机硬盘,w10共享盘怎么设置_win10如何共享硬盘
  11. 备战数学建模23-数据包络分析
  12. Ubuntu18.04 配置nvidia 460驱动、cuda10.1和cudnn7.6.5
  13. js 匹配正确手机号
  14. 51JOB:根据HR处理简历的一般流程,简历投递后会有如下几种状态出现
  15. 数字式高精度可调电流源电路设计
  16. Superpro5000的使用经验小结
  17. c语言 signal作用,C语言中的signal函数
  18. with open (files_name) as f
  19. 【PyTorch基础教程2】自动求导机制(学不会来打我啊)
  20. 计算机网络课设路由器,计算机网络课程设计---小型校园网络设计.doc

热门文章

  1. 首款移动社交购物平台“商宝”的产品简介、特点、交易服务以及入驻方式
  2. excel去掉超链接
  3. zz SOA推荐书籍列表
  4. Java Socket实战之一:单线程通信
  5. 关于javascript的介绍
  6. POJ2513Colored Sticks(欧拉路加字典树)
  7. Cisco 2950 忘记密码如何重设
  8. 【the service mysql57 failed the most】
  9. Linux CPU信息
  10. Peter R. Rizun:“比特币现金社区的目标是全球80亿人使用BCH”