Linux中國

十個 SCP 傳輸命令例子

以下命令可以解讀為:用「username account」「拷貝 source file name」到「destination host」上的「destination folder」里。

SCP命令的基本語法

scp source_file_name username@destination_host:destination_folder

SCP命令有很多可以使用的參數,這裡指的是每次都會用到的參數。

用-v參數來提供SCP進程的詳細信息

不帶參數的基本SCP命令會在後台拷貝文件,除非操作完成或者有錯誤出現,否則用戶在界面上是看不到任何提示信息的。你可以用「-v」參數來在屏幕上列印出調試信息,這能幫助你調試連接、認證和配置的一些問題。

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

部分輸出

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
mrarianto@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」參數會幫到把預計的時間和連接速度會顯示在屏幕上。

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

部分輸出

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

用-C參數來讓文件傳輸更快

有一個參數能讓傳輸文件更快,就是「-C」參數,它的作用是不停壓縮所傳輸的文件。它特別之處在於壓縮是在網路傳輸中進行,當文件傳到目標伺服器時,它會變回壓縮之前的原始大小。

來看看這些命令,我們使用一個93 Mb的單一文件來做例子。

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

部分輸出

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
mrarianto@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」參數來拷貝文件,結果用了1661.3秒,你可以比較下用了「-C」參數之後的結果。

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

部分輸出

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
mrarianto@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

看到了吧,壓縮了文件之後,傳輸過程在162.5秒內就完成了,速度是不用「-C」參數的10倍。如果你要通過網路拷貝很多份文件,那麼「-C」參數能幫你節省掉很多時間。

有一點我們需要注意,這個壓縮的方法不是適用於所有文件。當源文件已經被壓縮過了,那就沒辦法再壓縮很多了。諸如那些像.zip.rarpictures.iso的文件,用「-C」參數就沒什麼意義。

選擇其它加密演算法來加密文件

SCP默認是用「AES-128」加密演算法來加密傳輸的。如果你想要改用其它加密演算法來加密傳輸,你可以用「-c」參數。我們來瞧瞧。

pungki@mint ~/Documents $ scp -c 3des Label.pdf mrarianto@202.x.x.x:.

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

上述命令是告訴SCP3des algorithm來加密文件。要注意這個參數是「-c」(小寫)而不是「-C「(大寫)。

限制帶寬使用

還有一個很有用的參數是「-l」參數,它能限制使用帶寬。如果你為了拷貝很多文件而去執行了一份自動化腳本又不希望帶寬被SCP進程耗盡,那這個參數會非常管用。

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

mrarianto@202.x.x.x's password:
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埠,這種情況下就要指定埠。命令如下所示。

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

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

確認一下寫的是大寫字母「P」而不是「p「,因為「p」已經被用來保留源文件的修改時間和模式(LCTT 譯註:和 ssh 命令不同了)。

遞歸拷貝文件和文件夾

有時我們需要拷貝文件夾及其內部的所有文件/子文件夾,我們如果能用一條命令解決問題那就更好了。SCP用「-r」參數就能做到。

pungki@mint ~/Documents $ scp -r documents mrarianto@202.x.x.x:.

mrarianto@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」是系統自動創建的文件夾。

禁用進度條和警告/診斷信息

如果你不想從SCP中看到進度條和警告/診斷信息,你可以用「-q」參數來靜默它們,舉例如下。

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

mrarianto@202.x.x.x's password:
pungki@mint ~/Documents $

正如你所看到的,在你輸入密碼之後,沒有任何關於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

然後你就可以像往常一樣使用SCP了。

請注意corkscrew可能還沒有安裝在你的系統中。在我的Linux Mint中,我需要首先先用標準Linux Mint安裝程序來安裝它。

$ apt-get install corkscrew

對於其它的一些基於yum安裝的系統,用戶能用以下的命令來安裝corkscrew。

# yum install corkscrew

還有一點就是因為「~/.ssh/proxyauth」文件中以明文的格式包含了你的「用戶名」和「密碼」,所以請確保該文件只能你來查看。

選擇不同的ssh_config文件

對於經常在公司網路和公共網路之間切換的移動用戶來說,一直改變SCP的設置顯然是很痛苦的。如果我們能放一個保存不同配置的ssh_config文件來匹配我們的需求那就很好了。

以下是一個簡單的場景

代理是被用來在公司網路但不是公共網路並且你會定期切換網路時候使用的。

pungki@mint ~/Documents $ scp -F /home/pungki/proxy_ssh_config Label.pdf

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

默認情況下每個用戶會把「ssh_config」文件放在「~/.ssh/config「路徑下。用兼容的代理創建一個特定的「ssh_config」文件,能讓你切換網路時更加方便容易。

當你處於公司網路時,你可以用「-F」參數,當你處於公共網路時,你可以忽略掉「-F」參數。

以上就是關於SCP的全部內容了,你可以查看SCPman頁面來獲取更多內容,請隨意留下您的評論及建議。

via: http://www.tecmint.com/scp-commands-examples/

作者:Pungki Arianto 譯者:ZTinoZ 校對:wxy

本文由 LCTT 原創翻譯,Linux中國 榮譽推出


本文轉載來自 Linux 中國: https://github.com/Linux-CN/archive

對這篇文章感覺如何?

太棒了
0
不錯
0
愛死了
0
不太好
0
感覺很糟
0
雨落清風。心向陽

    You may also like

    Leave a reply

    您的電子郵箱地址不會被公開。 必填項已用 * 標註

    此站點使用Akismet來減少垃圾評論。了解我們如何處理您的評論數據

    More in:Linux中國