歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 十個 SCP 傳輸命令例子

十個 SCP 傳輸命令例子

日期:2017/2/28 14:24:46   编辑:Linux教程

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 [email protected]:.

部分輸出

  1. Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -t .
  2. OpenSSH_6.0p1Debian-3,OpenSSL1.0.1c10May2012
  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. [email protected]'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 [email protected]:.

部分輸出

  1. [email protected]'s password:
  2. Label.pdf 100% 3672KB 126.6KB/s 00:29

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

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

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

  1. pungki@mint ~/Documents $ scp -pv messages.log [email protected]:.

部分輸出

  1. Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t .
  2. OpenSSH_6.0p1Debian-3,OpenSSL1.0.1c10May2012
  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:Tryingprivate key:/home/pungki/.ssh/id_rsa
  11. debug1:Next authentication method: password
  12. [email protected]'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 [email protected]:.

部分輸出

  1. Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t .
  2. OpenSSH_6.0p1Debian-3,OpenSSL1.0.1c10May2012
  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:Tryingprivate key:/home/pungki/.ssh/id_rsa
  13. debug1:Next authentication method: password
  14. [email protected]'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.rarpictures.iso的文件,用“-C”參數就沒什麼意義。

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

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

  1. pungki@mint ~/Documents $ scp -c 3desLabel.pdf [email protected]:.
  2. [email protected]'s password:
  3. Label.pdf 100% 3672KB 282.5KB/s 00:13

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

限制帶寬使用

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

  1. pungki@mint ~/Documents $ scp -l 400Label.pdf [email protected]:.
  2. [email protected]'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 2249Label.pdf [email protected]:.
  2. [email protected]'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 [email protected]:.
  2. [email protected]'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 [email protected]:.
  2. [email protected]'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.68080%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. [email protected]:.
  3. [email protected]'s password:
  4. Label.pdf 100% 3672KB 282.5KB/s 00:13

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

當你處於公司網絡時,你可以用“-F”參數,當你處於公共網絡時,你可以忽略掉“-F”參數。

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

Linux之cp/scp命令+scp命令詳解 http://www.linuxidc.com/Linux/2014-09/107127.htm

CentOS 不能使用scp命令 解決方法 http://www.linuxidc.com/Linux/2014-09/106569.htm

兩台Linux主機之間scp復制文件 http://www.linuxidc.com/Linux/2014-04/99979.htm

壓搾scp傳輸速度 http://www.linuxidc.com/Linux/2014-01/95498.htm

加速scp傳輸速度 http://www.linuxidc.com/Linux/2014-01/95497.htm

shell實現scp批量下發文件 http://www.linuxidc.com/Linux/2013-11/92783.htm

Copyright © Linux教程網 All Rights Reserved