scp 命令

scp是 SSH 提供的一個(gè)客戶端程序,用來在兩臺(tái)主機(jī)之間加密傳送文件(即復(fù)制文件)。

簡介

scp是 secure copy 的縮寫,相當(dāng)于cp命令 + SSH。它的底層是 SSH 協(xié)議,默認(rèn)端口是22,相當(dāng)于先使用ssh命令登錄遠(yuǎn)程主機(jī),然后再執(zhí)行拷貝操作。

scp主要用于以下三種復(fù)制操作。

  • 本地復(fù)制到遠(yuǎn)程。
  • 遠(yuǎn)程復(fù)制到本地。
  • 兩個(gè)遠(yuǎn)程系統(tǒng)之間的復(fù)制。

使用scp傳輸數(shù)據(jù)時(shí),文件和密碼都是加密的,不會(huì)泄漏敏感信息。

基本語法

scp的語法類似cp的語法。

$ scp source destination 

上面命令中,source是文件當(dāng)前的位置,destination是文件所要復(fù)制到的位置。它們都可以包含用戶名和主機(jī)名。

$ scp user@host:foo.txt bar.txt 

上面命令將遠(yuǎn)程主機(jī)(user@host)用戶主目錄下的foo.txt,復(fù)制為本機(jī)當(dāng)前目錄的bar.txt。可以看到,主機(jī)與文件之間要使用冒號(hào)(:)分隔。

scp會(huì)先用 SSH 登錄到遠(yuǎn)程主機(jī),然后在加密連接之中復(fù)制文件??蛻舳税l(fā)起連接后,會(huì)提示用戶輸入密碼,這部分是跟 SSH 的用法一致的。

用戶名和主機(jī)名都是可以省略的。用戶名的默認(rèn)值是本機(jī)的當(dāng)前用戶名,主機(jī)名默認(rèn)為當(dāng)前主機(jī)。注意,scp會(huì)使用 SSH 客戶端的配置文件.ssh/config,如果配置文件里面定義了主機(jī)的別名,這里也可以使用別名連接。

scp支持一次復(fù)制多個(gè)文件。

$ scp source1 source2 destination 

上面命令會(huì)將source1source2兩個(gè)文件,復(fù)制到destination。

注意,如果所要復(fù)制的文件,在目標(biāo)位置已經(jīng)存在同名文件,scp會(huì)在沒有警告的情況下覆蓋同名文件。

用法示例

(1)本地文件復(fù)制到遠(yuǎn)程

復(fù)制本機(jī)文件到遠(yuǎn)程系統(tǒng)的用法如下。

# 語法 

$ scp SourceFile user@host:directory/TargetFile 

# 示例 

$ scp file.txt remote_username@10.10.0.2:/remote/directory

下面是復(fù)制整個(gè)目錄的例子。

# 將本機(jī)的 documents 目錄拷貝到遠(yuǎn)程主機(jī), 

# 會(huì)在遠(yuǎn)程主機(jī)創(chuàng)建 documents 目錄 

$ scp -r documents username@server_ip:/path_to_remote_directory 

# 將本機(jī)整個(gè)目錄拷貝到遠(yuǎn)程目錄下 

$ scp -r localmachine/path_to_the_directory username@server_ip:/path_to_remote_directory/ 

# 將本機(jī)目錄下的所有內(nèi)容拷貝到遠(yuǎn)程目錄下 

$ scp -r localmachine/path_to_the_directory/* username@server_ip:/path_to_remote_directory/


(2)遠(yuǎn)程文件復(fù)制到本地

從遠(yuǎn)程主機(jī)復(fù)制文件到本地的用法如下。

# 語法 

$ scp user@host:directory/SourceFile TargetFile 

# 示例 

$ scp remote_username@10.10.0.2:/remote/file.txt /local/directory

下面是復(fù)制整個(gè)目錄的例子。


# 拷貝一個(gè)遠(yuǎn)程目錄到本機(jī)目錄下  

$ scp -r username@server_ip:/path_to_remote_directory local-machine/path_to_the_directory/  

# 拷貝遠(yuǎn)程目錄下的所有內(nèi)容,到本機(jī)目錄下  

$ scp -r username@server_ip:/path_to_remote_directory/* local-machine/path_to_the_directory/ 

$ scp -r user@host:directory/SourceFolder TargetFolder 


(3)兩個(gè)遠(yuǎn)程系統(tǒng)之間的復(fù)制

本機(jī)發(fā)出指令,從遠(yuǎn)程主機(jī) A 拷貝到遠(yuǎn)程主機(jī) B 的用法如下。

# 語法 

$ scp user@host1:directory/SourceFile user@host2:directory/SourceFile 

# 示例 

$ scp user1@host1.com:/files/file.txt user2@host2.com:/files

系統(tǒng)將提示你輸入兩個(gè)遠(yuǎn)程帳戶的密碼。數(shù)據(jù)將直接從一個(gè)遠(yuǎn)程主機(jī)傳輸?shù)搅硪粋€(gè)遠(yuǎn)程主機(jī)。

配置項(xiàng)

(1)-c

-c參數(shù)用來指定文件拷貝數(shù)據(jù)傳輸?shù)募用芩惴ā?

$ scp -c blowfish some_file your_username@remotehost.edu:~ 

上面代碼指定加密算法為blowfish

(2)-C

-C參數(shù)表示是否在傳輸時(shí)壓縮文件。

$ scp -c blowfish -C local_file your_username@remotehost.edu:~ 

(3)-F

-F參數(shù)用來指定 ssh_config 文件,供 ssh 使用。

$ scp -F /home/pungki/proxy_ssh_config Label.pdf root@172.20.10.8:/root 

(4)-i

-i參數(shù)用來指定密鑰。

$ scp -vCq -i private_key.pem ~/test.txt root@192.168.1.3:/some/path/test.txt 

(5)-l

-l參數(shù)用來限制傳輸數(shù)據(jù)的帶寬速率,單位是 Kbit/sec。對于多人分享的帶寬,這個(gè)參數(shù)可以留出一部分帶寬供其他人使用。

$ scp -l 80 yourusername@yourserver:/home/yourusername/* . 

上面代碼中,scp命令占用的帶寬限制為每秒 80K 比特位,即每秒 10K 字節(jié)。

(6)-p

-p參數(shù)用來保留修改時(shí)間(modification time)、訪問時(shí)間(access time)、文件狀態(tài)(mode)等原始文件的信息。

$ scp -p ~/test.txt root@192.168.1.3:/some/path/test.txt 

(7)-P

-P參數(shù)用來指定遠(yuǎn)程主機(jī)的 SSH 端口。如果遠(yuǎn)程主機(jī)使用默認(rèn)端口22,可以不用指定,否則需要用-P參數(shù)在命令中指定。

$ scp -P 2222 user@host:directory/SourceFile TargetFile 

(8)-q

-q參數(shù)用來關(guān)閉顯示拷貝的進(jìn)度條。

$ scp -q Label.pdf mrarianto@202.x.x.x:. 

(9)-r

-r參數(shù)表示是否以遞歸方式復(fù)制目錄。

(10)-v

-v參數(shù)用來顯示詳細(xì)的輸出。

$ scp -v ~/test.txt root@192.168.1.3:/root/help2356.txt


丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清