everyday:linux scp
[TOC]
功能說明
- scp 是
secure cope
的縮寫,用于遠(yuǎn)程拷貝文件棕所,與之類似的有cp
瀑踢,不過 cp 只是在本機(jī)進(jìn)行拷貝,不能跨服務(wù)器哄陶,最重要的是scp
的傳輸是加密的帆阳,這個(gè)可能會(huì)稍微影響下速度。 - 當(dāng)服務(wù)器硬盤變?yōu)橹蛔x(read only system)時(shí),用scp可以把文件移出來蜒谤。
- scp 還非常不占資源山宾,不會(huì)提高多少系統(tǒng)負(fù)荷,在這一點(diǎn)上鳍徽,
rsync
就遠(yuǎn)遠(yuǎn)不及资锰。相對(duì)而言,rsync 比 scp 會(huì)快一點(diǎn)阶祭,但當(dāng)小文件眾多的情況下绷杜,rsync 會(huì)導(dǎo)致硬盤I/O非常高,而 scp 基本上不影響系統(tǒng)正常使用濒募。
命令詳解
-
命令格式
scp [參數(shù)][原路徑][目標(biāo)路徑] -
命令功能
scp 是基于ssh登錄
鞭盟,進(jìn)行安全的遠(yuǎn)程文件拷貝的命令」逄辏可以在linux服務(wù)器之間復(fù)制文件和目錄齿诉。
-
命令參數(shù)
-1 強(qiáng)制scp命令使用協(xié)議ssh1
-2 強(qiáng)制scp命令使用協(xié)議ssh2
-4 強(qiáng)制scp命令只使用IPv4尋址
-6 強(qiáng)制scp命令只使用IPv6尋址
-B 使用批處理模式(傳輸過程中不詢問傳輸口令或短語)
-C 允許壓縮。(將-C標(biāo)志傳遞給ssh晌姚,從而打開壓縮功能)
-p 保留原文件的修改時(shí)間粤剧,訪問時(shí)間和訪問權(quán)限
-q 不顯示傳輸進(jìn)度條
-r 遞歸復(fù)制整個(gè)目錄
-v 詳細(xì)方式顯示輸出。scp和ssh(1)會(huì)顯示出整個(gè)過程的調(diào)試信息舀凛。這些信息用于調(diào)試連接俊扳,驗(yàn)證和配置問題。
-c cipher 以cipher將數(shù)據(jù)傳輸進(jìn)行加密猛遍,這個(gè)選項(xiàng)將直接傳遞給ssh馋记。
-F ssh_config 指定一個(gè)替代的ssh配置文件,此參數(shù)直接傳遞給ssh懊烤。
-i identity_file 從指定文件中讀取傳輸時(shí)使用的密鑰文件梯醒,此參數(shù)直接傳遞給ssh。
-l limit 限定用戶所能使用的帶寬腌紧,以Kbit/s為單位茸习。
-o ssh_option 如果習(xí)慣于使用ssh_config(5)中的參數(shù)傳遞方式
-P port 注意是大寫的P, port是指定數(shù)據(jù)傳輸用到的端口號(hào)
-S program 指定加密傳輸時(shí)所使用的程序。此程序必須能夠理解ssh(1)的選項(xiàng)壁肋。
-
舉例
-
從本地服務(wù)器復(fù)制到遠(yuǎn)程服務(wù)器
- eg1 復(fù)制文件
-
1. 指定用戶名号胚,命令執(zhí)行后需要輸入用戶密碼
+ 指定遠(yuǎn)程目錄,文件名字不變
scp local.file remote.username@remote.ip:remote.folder
+ 指定文件名
scp local.file remote.username@remote.ip:remote.file
2. 沒有指定用戶名浸遗,命令執(zhí)行后需要輸入用戶名和密碼
+ 指定遠(yuǎn)程目錄猫胁,文件名字不變
scp local.file remote.ip:remote.folder
+ 指定文件名
scp local.file remote.ip:remote.file
+ eg2 復(fù)制目錄
1. 指定用戶名,命令執(zhí)行后需要輸入用戶密碼
scp -r local.folder remote.username@remote.ip:remote.folder
2. 沒有指定用戶名跛锌,命令執(zhí)行后需要輸入用戶名和密碼
scp -r local.folder remote.ip:remote.folder
2. ###### **從遠(yuǎn)程服務(wù)器復(fù)制到本地服務(wù)器**
+ eg1 從遠(yuǎn)程復(fù)制文件到本地目錄
1. 指定用戶名弃秆,命令執(zhí)行后需要輸入用戶密碼
scp remote.username@remote.ip:remote.folder/remote.file local.folder
or
cd local.folder
scp remote.username@remote.ip:remote.folder/remote.file .
2. 沒有指定用戶名,命令執(zhí)行后需要輸入用戶名和密碼
scp remote.ip:remote.folder/remote.file local.folder
+ eg2 從遠(yuǎn)程復(fù)制目錄到本地目錄
1. 指定用戶名,命令執(zhí)行后需要輸入用戶密碼
scp -r remote.username@remote.ip:remote.folder local.folder
2. 沒有指定用戶名菠赚,命令執(zhí)行后需要輸入用戶名和密碼
scp -r remote.ip:remote.folder local.folder
+ eg3 上傳本地文件到遠(yuǎn)程指定目錄
scp local.folder/local.file remote.username@remote.ip:remote.folder
+ eg4 上傳本地目錄到遠(yuǎn)程指定目錄
scp -r local.folder remote.username@remote.ip:remote.folder