2019-04-21 11:50:48 +08:00

47 lines
3.8 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1 id="scp">scp</h1>
<p>加密的方式在本地主机和远程主机之间复制文件</p>
<h2 id="补充说明">补充说明</h2>
<p><strong>scp命令</strong> 用于在Linux下进行远程拷贝文件的命令和它类似的命令有cp不过cp只是在本机进行拷贝不能跨服务器而且scp传输是加密的。可能会稍微影响一下速度。当你服务器硬盘变为只读read only system时用scp可以帮你把文件移出来。另外scp还非常不占资源不会提高多少系统负荷在这一点上rsync就远远不及它了。虽然 rsync比scp会快一点但当小文件众多的情况下rsync会导致硬盘I/O非常高而scp基本不影响系统正常使用。</p>
<h3 id="语法">语法</h3>
<pre><code class="language-bash">scp(选项)(参数)</code></pre>
<h3 id="选项">选项</h3>
<pre><code class="language-bash">-1使用ssh协议版本1
-2使用ssh协议版本2
-4使用ipv4
-6使用ipv6
-B以批处理模式运行
-C使用压缩
-F指定ssh配置文件
-iidentity_file 从指定文件中读取传输时使用的密钥文件例如亚马逊云pem此参数直接传递给ssh
-l指定宽带限制
-o指定使用的ssh选项
-P指定远程主机的端口号
-p保留文件的最后修改时间最后访问时间和权限模式
-q不显示复制进度
-r以递归方式复制。</code></pre>
<h3 id="参数">参数</h3>
<ul>
<li>源文件:指定要复制的源文件。</li>
<li>目标文件:目标文件。格式为<code>user@hostfilename</code>(文件名为目标文件的名称)。</li>
</ul>
<h3 id="实例">实例</h3>
<p>从远程复制到本地的scp命令与上面的命令雷同只要将从本地复制到远程的命令后面2个参数互换顺序就行了。</p>
<p><strong>从远处复制文件到本地目录</strong></p>
<pre><code class="language-bash">scp root@10.10.10.10:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/</code></pre>
<p>从10.10.10.10机器上的<code>/opt/soft/</code>的目录中下载nginx-0.5.38.tar.gz 文件到本地<code>/opt/soft/</code>目录中。</p>
<p><strong>从亚马逊云复制OpenVPN到本地目录</strong></p>
<pre><code class="language-bash">scp -i amazon.pem ubuntu@10.10.10.10:/usr/local/openvpn_as/etc/exe/openvpn-connect-2.1.3.110.dmg openvpn-connect-2.1.3.110.dmg</code></pre>
<p>从10.10.10.10机器上下载openvpn安装文件到本地当前目录来。</p>
<p><strong>从远处复制到本地</strong></p>
<pre><code class="language-bash">scp -r root@10.10.10.10:/opt/soft/mongodb /opt/soft/</code></pre>
<p>从10.10.10.10机器上的<code>/opt/soft/</code>中下载mongodb目录到本地的<code>/opt/soft/</code>目录来。</p>
<p><strong>上传本地文件到远程机器指定目录</strong></p>
<div class="sourceCode" id="cb6"><pre><code class="language-bash"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="fu">scp</span> /opt/soft/nginx-0.5.38.tar.gz root@10.10.10.10:/opt/soft/scptest</a>
<a class="sourceLine" id="cb6-2" data-line-number="2"><span class="co"># 指定端口 2222</span></a>
<a class="sourceLine" id="cb6-3" data-line-number="3"><span class="fu">scp</span> -rp -P 2222 /opt/soft/nginx-0.5.38.tar.gz root@10.10.10.10:/opt/soft/scptest</a></code></pre></div>
<p>复制本地<code>/opt/soft/</code>目录下的文件nginx-0.5.38.tar.gz到远程机器10.10.10.10的<code>opt/soft/scptest</code>目录。</p>
<p><strong>上传本地目录到远程机器指定目录</strong></p>
<pre><code class="language-bash">scp -r /opt/soft/mongodb root@10.10.10.10:/opt/soft/scptest</code></pre>
<p>上传本地目录<code>/opt/soft/mongodb</code>到远程机器10.10.10.10上<code>/opt/soft/scptest</code>的目录中去。</p>
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->