佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > Linux中SSH的SCP傳輸命令運(yùn)用詳解

Linux中SSH的SCP傳輸命令運(yùn)用詳解

熱門(mén)標(biāo)簽:硅谷的囚徒呼叫中心 解決方案 電話(huà)機(jī)器人搭建 服務(wù)器配置 家政服務(wù)網(wǎng)絡(luò) 外呼系統(tǒng) 美團(tuán) 百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式

Linux系統(tǒng)管理員應(yīng)該很熟悉CLI環(huán)境,因?yàn)橥ǔT贚inux服務(wù)器中是不安裝GUI的。SSH可能是Linux系統(tǒng)管理員通過(guò)遠(yuǎn)程方式安全管理服務(wù)器的最流行協(xié)議。在SSH命令中內(nèi)置了一種叫SCP的命令,用來(lái)在服務(wù)器之間安全傳輸文件。
以下命令可以解讀為:用“username account”“拷貝 source file name”到“destination host”上的“destination folder”里。
SCP命令的基本語(yǔ)法

   

復(fù)制代碼
代碼如下:
scp source_file_name username@destination_host:destination_folder

SCP命令有很多可以使用的參數(shù),這里指的是每次都會(huì)用到的參數(shù)。
用-v參數(shù)來(lái)提供SCP進(jìn)程的詳細(xì)信息

不帶參數(shù)的基本SCP命令會(huì)在后臺(tái)拷貝文件,除非操作完成或者有錯(cuò)誤出現(xiàn),否則用戶(hù)在界面上是看不到任何提示信息的。你可以用“-v”參數(shù)來(lái)在屏幕上打印出調(diào)試信息,這能幫助你調(diào)試連接、認(rèn)證和配置的一些問(wèn)題。

   

復(fù)制代碼
代碼如下:
pungki@mint ~/Documents $ scp -v Label.pdf mrarianto@202.x.x.x:.

部分輸出

   

復(fù)制代碼
代碼如下:
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

從源文件獲取修改時(shí)間、訪(fǎng)問(wèn)時(shí)間和模式

“-p”參數(shù)會(huì)幫到把預(yù)計(jì)的時(shí)間和連接速度會(huì)顯示在屏幕上。

   

復(fù)制代碼
代碼如下:
pungki@mint ~/Documents $ scp -p Label.pdf mrarianto@202.x.x.x:.

部分輸出

   

復(fù)制代碼
代碼如下:
mrarianto@202.x.x.x's password:
Label.pdf 100% 3672KB 126.6KB/s 00:29

用-C參數(shù)來(lái)讓文件傳輸更快

有一個(gè)參數(shù)能讓傳輸文件更快,就是“-C”參數(shù),它的作用是不停壓縮所傳輸?shù)奈募?。它特別之處在于壓縮是在網(wǎng)絡(luò)傳輸中進(jìn)行,當(dāng)文件傳到目標(biāo)服務(wù)器時(shí),它會(huì)變回壓縮之前的原始大小。

來(lái)看看這些命令,我們使用一個(gè)93 Mb的單一文件來(lái)做例子。

   

復(fù)制代碼
代碼如下:
pungki@mint ~/Documents $ scp -pv messages.log mrarianto@202.x.x.x:.

部分輸出

   

復(fù)制代碼
代碼如下:
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”參數(shù)來(lái)拷貝文件,結(jié)果用了1661.3秒,你可以比較下用了“-C”參數(shù)之后的結(jié)果。

   

復(fù)制代碼
代碼如下:
pungki@mint ~/Documents $ scp -Cpv messages.log mrarianto@202.x.x.x:.

部分輸出

   

復(fù)制代碼
代碼如下:
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

看到了吧,壓縮了文件之后,傳輸過(guò)程在162.5秒內(nèi)就完成了,速度是不用“-C”參數(shù)的10倍。如果你要通過(guò)網(wǎng)絡(luò)拷貝很多份文件,那么“-C”參數(shù)能幫你節(jié)省掉很多時(shí)間。

有一點(diǎn)我們需要注意,這個(gè)壓縮的方法不是適用于所有文件。當(dāng)源文件已經(jīng)被壓縮過(guò)了,那就沒(méi)辦法再壓縮很多了。諸如那些像.zip,.rar,pictures和.iso的文件,用“-C”參數(shù)就沒(méi)什么意義。
選擇其它加密算法來(lái)加密文件

SCP默認(rèn)是用“AES-128”加密算法來(lái)加密傳輸?shù)?。如果你想要改用其它加密算法?lái)加密傳輸,你可以用“-c”參數(shù)。我們來(lái)瞧瞧。

   

復(fù)制代碼
代碼如下:
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

上述命令是告訴SCP用3des algorithm來(lái)加密文件。要注意這個(gè)參數(shù)是“-c”(小寫(xiě))而不是“-C“(大寫(xiě))。
限制帶寬使用

還有一個(gè)很有用的參數(shù)是“-l”參數(shù),它能限制使用帶寬。如果你為了拷貝很多文件而去執(zhí)行了一份自動(dòng)化腳本又不希望帶寬被SCP進(jìn)程耗盡,那這個(gè)參數(shù)會(huì)非常管用。

   

復(fù)制代碼
代碼如下:
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”參數(shù)后面的這個(gè)400值意思是我們給SCP進(jìn)程限制了帶寬為50 KB/秒。有一點(diǎn)要記住,帶寬是以千比特/秒 (kbps)表示的,而8 比特等于1 字節(jié)。

因?yàn)镾CP是用千字節(jié)/秒 (KB/s)計(jì)算的,所以如果你想要限制SCP的最大帶寬只有50 KB/s,你就需要設(shè)置成50 x 8 = 400。
指定端口

通常SCP是把22作為默認(rèn)端口。但是為了安全起見(jiàn)SSH 監(jiān)聽(tīng)端口改成其它端口。比如說(shuō),我們想用2249端口,這種情況下就要指定端口。命令如下所示。

   

復(fù)制代碼
代碼如下:
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

確認(rèn)一下寫(xiě)的是大寫(xiě)字母“P”而不是“p“,因?yàn)?ldquo;p”已經(jīng)被用來(lái)保留源文件的修改時(shí)間和模式(LCTT 譯注:和 ssh 命令不同了)。
遞歸拷貝文件和文件夾

有時(shí)我們需要拷貝文件夾及其內(nèi)部的所有文件/子文件夾,我們?nèi)绻苡靡粭l命令解決問(wèn)題那就更好了。SCP用“-r”參數(shù)就能做到。

   

復(fù)制代碼
代碼如下:
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

拷貝完成后,你會(huì)在目標(biāo)服務(wù)器中找到一個(gè)名為“documents”的文件夾,其中就是所拷貝的所有文件。“documents”是系統(tǒng)自動(dòng)創(chuàng)建的文件夾。
禁用進(jìn)度條和警告/診斷信息

如果你不想從SCP中看到進(jìn)度條和警告/診斷信息,你可以用“-q”參數(shù)來(lái)靜默它們,舉例如下。

   

復(fù)制代碼
代碼如下:
pungki@mint ~/Documents $ scp -q Label.pdf mrarianto@202.x.x.x:.

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

正如你所看到的,在你輸入密碼之后,沒(méi)有任何關(guān)于SCP進(jìn)度的消息反饋。進(jìn)度完成后,你也看不到任何提示。
用SCP通過(guò)代理來(lái)拷貝文件

代理服務(wù)器經(jīng)常用于辦公環(huán)境,SCP自然是沒(méi)有經(jīng)過(guò)代理方面的配置的。當(dāng)你的環(huán)境正在使用代理,那么你就必須要“告訴”SCP與代理關(guān)聯(lián)起來(lái)。

場(chǎng)景如下:代理的地址是10.0.96.6,端口是8080。該代理還實(shí)現(xiàn)了用戶(hù)認(rèn)證功能。首先,你需要?jiǎng)?chuàng)建一個(gè)“~/.ssh/config”文件,其次把以下命令輸入進(jìn)該文件。

   

復(fù)制代碼
代碼如下:
ProxyCommand /usr/bin/corkscrew 10.0.96.6 8080 %h %p ~/.ssh/proxyauth

接著你需要?jiǎng)?chuàng)建一個(gè)同樣包括以下命令的“~/.ssh/proxyauth”文件。

   

復(fù)制代碼
代碼如下:
myusername:mypassword

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

請(qǐng)注意corkscrew可能還沒(méi)有安裝在你的系統(tǒng)中。在我的Linux Mint中,我需要首先先用標(biāo)準(zhǔn)Linux Mint安裝程序來(lái)安裝它。

   

復(fù)制代碼
代碼如下:
$ apt-get install corkscrew

對(duì)于其它的一些基于yum安裝的系統(tǒng),用戶(hù)能用以下的命令來(lái)安裝corkscrew。

   

復(fù)制代碼
代碼如下:
# yum install corkscrew

還有一點(diǎn)就是因?yàn)?ldquo;~/.ssh/proxyauth”文件中以明文的格式包含了你的“用戶(hù)名”和“密碼”,所以請(qǐng)確保該文件只能你來(lái)查看。
選擇不同的ssh_config文件

對(duì)于經(jīng)常在公司網(wǎng)絡(luò)和公共網(wǎng)絡(luò)之間切換的移動(dòng)用戶(hù)來(lái)說(shuō),一直改變SCP的設(shè)置顯然是很痛苦的。如果我們能放一個(gè)保存不同配置的ssh_config文件來(lái)匹配我們的需求那就很好了。
以下是一個(gè)簡(jiǎn)單的場(chǎng)景

代理是被用來(lái)在公司網(wǎng)絡(luò)但不是公共網(wǎng)絡(luò)并且你會(huì)定期切換網(wǎng)絡(luò)時(shí)候使用的。

   

復(fù)制代碼
代碼如下:
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

默認(rèn)情況下每個(gè)用戶(hù)會(huì)把“ssh_config”文件放在“~/.ssh/config“路徑下。用兼容的代理創(chuàng)建一個(gè)特定的“ssh_config”文件,能讓你切換網(wǎng)絡(luò)時(shí)更加方便容易。

當(dāng)你處于公司網(wǎng)絡(luò)時(shí),你可以用“-F”參數(shù),當(dāng)你處于公共網(wǎng)絡(luò)時(shí),你可以忽略掉“-F”參數(shù)。

以上就是關(guān)于SCP的全部?jī)?nèi)容了,你可以查看SCP的man頁(yè)面來(lái)獲取更多內(nèi)容,請(qǐng)隨意留下您的評(píng)論及建議。

標(biāo)簽:撫州 邢臺(tái) 韶關(guān) 南昌 烏蘭察布 北海 臨沂 防城港

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Linux中SSH的SCP傳輸命令運(yùn)用詳解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266
    海门市| 两当县| 开远市| 都匀市| 锦屏县| 九寨沟县| 铜川市| 湛江市| 喀喇沁旗| 成武县| 布尔津县| 雷山县| 松原市| 民丰县| 漯河市| 丹寨县| 正宁县| 常山县| 綦江县| 乌什县| 隆德县| 尼木县| 赫章县| 修文县| 崇左市| 武山县| 扎赉特旗| 嵊泗县| 庆安县| 克山县| 乐亭县| 桃源县| 芮城县| 屏南县| 阿荣旗| 镇安县| 封丘县| 白玉县| 康马县| 同江市| 开封县|