溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在linux中使用scp命令

發(fā)布時(shí)間:2021-03-15 16:46:26 來源:億速云 閱讀:148 作者:Leah 欄目:系統(tǒng)運(yùn)維

今天就跟大家聊聊有關(guān)怎么在linux中使用scp命令,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

1.命令格式:

scp [參數(shù)] [原路徑] [目標(biāo)路徑]

2.命令功能:
scp是 secure copy的縮寫, scp是linux系統(tǒng)下基于ssh登陸進(jìn)行安全的遠(yuǎn)程文件拷貝命令。linux的scp命令可以在linux服務(wù)器之間復(fù)制文件和目錄。

3.命令參數(shù):
-1  強(qiáng)制scp命令使用協(xié)議ssh2 
-2  強(qiáng)制scp命令使用協(xié)議ssh3 
-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)。

4.使用實(shí)例:
scp命令的實(shí)際應(yīng)用概述: 
從本地服務(wù)器復(fù)制到遠(yuǎn)程服務(wù)器:

(1) 復(fù)制文件: 
命令格式: 

代碼如下:


scp local_file <a href="mailto:remote_username@remote_ip:remote_folder">remote_username@remote_ip:remote_folder</a>  
或者  
scp local_file <a href="mailto:remote_username@remote_ip:remote_file">remote_username@remote_ip:remote_file</a>  
或者  
scp local_file remote_ip:remote_folder  
或者  
scp local_file remote_ip:remote_file  


第1,2個(gè)指定了用戶名,命令執(zhí)行后需要輸入用戶密碼,第1個(gè)僅指定了遠(yuǎn)程的目錄,文件名字不變,第2個(gè)指定了文件名 
第3,4個(gè)沒有指定用戶名,命令執(zhí)行后需要輸入用戶名和密碼,第3個(gè)僅指定了遠(yuǎn)程的目錄,文件名字不變,第4個(gè)指定了文件名  

(2) 復(fù)制目錄: 
命令格式: 
scp -r local_folder remote_username@remote_ip:remote_folder 
或者 
scp -r local_folder remote_ip:remote_folder 
第1個(gè)指定了用戶名,命令執(zhí)行后需要輸入用戶密碼; 
第2個(gè)沒有指定用戶名,命令執(zhí)行后需要輸入用戶名和密碼;
 
從遠(yuǎn)程服務(wù)器復(fù)制到本地服務(wù)器:
從遠(yuǎn)程復(fù)制到本地的scp命令與上面的命令雷同,只要將從本地復(fù)制到遠(yuǎn)程的命令后面2個(gè)參數(shù)互換順序就行了。
實(shí)例1:從遠(yuǎn)處復(fù)制文件到本地目錄
命令:scp root@192.168.120.204:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/
輸出:

代碼如下:


[root@localhost ~]# cd /opt/soft/
[root@localhost soft]# ll
總計(jì) 80072
drwxr-xr-x 12 root root     4096 09-21 18:40 fms3.5
drwxr-xr-x  3 root root     4096 09-21 17:58 fms4.5
drwxr-xr-x 10 root root     4096 10-30 17:15 jdk1.6.0_16
drwxr-xr-x 10 root root     4096 09-17 19:27 jdk1.6.0_16.bak
-rwxr-xr-x  1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin
drwxrwxrwx  2 root root     4096 09-21 01:16 mysql
drwxr-xr-x  3 root root     4096 09-21 18:40 setup_file
drwxr-xr-x  9 root root     4096 09-17 19:23 tomcat6.0.32
drwxr-xr-x  9 root root     4096 2012-08-14 tomcat_7.0
[root@localhost soft]# scp <a href="mailto:root@192.168.120.204:/opt/soft/nginx-0.5.38.tar.gz">root@192.168.120.204:/opt/soft/nginx-0.5.38.tar.gz</a> /opt/soft/
<a href="mailto:root@192.168.120.204's">root@192.168.120.204's</a> password:
nginx-0.5.38.tar.gz                                                                               100%  479KB 478.7KB/s   00:00    
[root@localhost soft]# ll
總計(jì) 80556
drwxr-xr-x 12 root root     4096 09-21 18:40 fms3.5
drwxr-xr-x  3 root root     4096 09-21 17:58 fms4.5
drwxr-xr-x 10 root root     4096 10-30 17:15 jdk1.6.0_16
drwxr-xr-x 10 root root     4096 09-17 19:27 jdk1.6.0_16.bak
-rwxr-xr-x  1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin
drwxrwxrwx  2 root root     4096 09-21 01:16 mysql
-rw-r--r--  1 root root   490220 03-15 09:11 nginx-0.5.38.tar.gz
drwxr-xr-x  3 root root     4096 09-21 18:40 setup_file
drwxr-xr-x  9 root root     4096 09-17 19:23 tomcat6.0.32
drwxr-xr-x  9 root root     4096 2012-08-14 tomcat_7.0
[root@localhost soft]#


說明:
從192.168.120.204機(jī)器上的/opt/soft/的目錄中下載nginx-0.5.38.tar.gz 文件到本地/opt/soft/目錄中

實(shí)例2:從遠(yuǎn)處復(fù)制到本地
命令:scp -r root@192.168.120.204:/opt/soft/mongodb /opt/soft/
輸出:

代碼如下:


[root@localhost soft]# ll
總計(jì) 80556
drwxr-xr-x 12 root root     4096 09-21 18:40 fms3.5
drwxr-xr-x  3 root root     4096 09-21 17:58 fms4.5
drwxr-xr-x 10 root root     4096 10-30 17:15 jdk1.6.0_16
drwxr-xr-x 10 root root     4096 09-17 19:27 jdk1.6.0_16.bak
-rwxr-xr-x  1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin
drwxrwxrwx  2 root root     4096 09-21 01:16 mysql
-rw-r--r--  1 root root   490220 03-15 09:11 nginx-0.5.38.tar.gz
drwxr-xr-x  3 root root     4096 09-21 18:40 setup_file
drwxr-xr-x  9 root root     4096 09-17 19:23 tomcat6.0.32
drwxr-xr-x  9 root root     4096 2012-08-14 tomcat_7.0
[root@localhost soft]# scp -r <a href="mailto:root@192.168.120.204:/opt/soft/mongodb">root@192.168.120.204:/opt/soft/mongodb</a> /opt/soft/
<a href="mailto:root@192.168.120.204's">root@192.168.120.204's</a> password:
mongodb-linux-i686-static-1.8.5.tgz                                                               100%   28MB  28.3MB/s   00:01    
README                                                                                            100%  731     0.7KB/s   00:00    
THIRD-PARTY-NOTICES                                                                               100% 7866     7.7KB/s   00:00    
mongorestore                                                                                      100% 7753KB   7.6MB/s   00:00    
mongod                                                                                            100% 7760KB   7.6MB/s   00:01    
mongoexport                                                                                       100% 7744KB   7.6MB/s   00:00    
bsondump                                                                                          100% 7737KB   7.6MB/s   00:00    
mongofiles                                                                                        100% 7748KB   7.6MB/s   00:01    
mongostat                                                                                         100% 7808KB   7.6MB/s   00:00    
mongos                                                                                            100% 5262KB   5.1MB/s   00:01    
mongo                                                                                             100% 3707KB   3.6MB/s   00:00    
mongoimport                                                                                       100% 7754KB   7.6MB/s   00:00    
mongodump                                                                                         100% 7773KB   7.6MB/s   00:00    
GNU-AGPL-3.0                                                                                      100%   34KB  33.7KB/s   00:00    
[root@localhost soft]# ll
總計(jì) 80560
drwxr-xr-x 12 root root     4096 09-21 18:40 fms3.5
drwxr-xr-x  3 root root     4096 09-21 17:58 fms4.5
drwxr-xr-x 10 root root     4096 10-30 17:15 jdk1.6.0_16
drwxr-xr-x 10 root root     4096 09-17 19:27 jdk1.6.0_16.bak
-rwxr-xr-x  1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin
drwxr-xr-x  3 root root     4096 03-15 09:18 mongodb
drwxrwxrwx  2 root root     4096 09-21 01:16 mysql
-rw-r--r--  1 root root   490220 03-15 09:11 nginx-0.5.38.tar.gz
drwxr-xr-x  3 root root     4096 09-21 18:40 setup_file
drwxr-xr-x  9 root root     4096 09-17 19:23 tomcat6.0.32
drwxr-xr-x  9 root root     4096 2012-08-14 tomcat_7.0
[root@localhost soft]#


說明:從192.168.120.204機(jī)器上的/opt/soft/中下載mongodb 目錄到本地的/opt/soft/目錄來。

實(shí)例3:上傳本地文件到遠(yuǎn)程機(jī)器指定目錄
命令:scp /opt/soft/nginx-0.5.38.tar.gz root@192.168.120.204:/opt/soft/scptest
輸出:

代碼如下:


上傳前目標(biāo)機(jī)器的目標(biāo)目錄:
[root@localhost soft]# cd scptest/
[root@localhost scptest]# ll
總計(jì) 0
[root@localhost scptest]# ll</p> <p>本地機(jī)器上傳:
[root@localhost soft]# scp /opt/soft/nginx-0.5.38.tar.gz <a href="mailto:root@192.168.120.204:/opt/soft/scptest">root@192.168.120.204:/opt/soft/scptest</a>
<a href="mailto:root@192.168.120.204's">root@192.168.120.204's</a> password:
nginx-0.5.38.tar.gz           100%  479KB 478.7KB/s   00:00    
[root@localhost soft]# </p> <p>上傳后目標(biāo)機(jī)器的目標(biāo)目錄:
[root@localhost scptest]# ll
總計(jì) 484
-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz
[root@localhost scptest]#


說明:復(fù)制本地opt/soft/目錄下的文件nginx-0.5.38.tar.gz 到遠(yuǎn)程機(jī)器192.168.120.204的opt/soft/scptest目錄

實(shí)例4:上傳本地目錄到遠(yuǎn)程機(jī)器指定目錄
命令:scp -r /opt/soft/mongodb root@192.168.120.204:/opt/soft/scptest
輸出:

代碼如下:


上傳前目標(biāo)機(jī)器的目標(biāo)目錄:
[root@localhost ~]# cd /opt/soft/scptest/
[root@localhost scptest]# ll
總計(jì) 484
-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz
[root@localhost scptest]#
本地機(jī)器上傳:
[root@localhost ~]# scp -r /opt/soft/mongodb <a href="mailto:root@192.168.120.204:/opt/soft/scptest">root@192.168.120.204:/opt/soft/scptest</a>
<a href="mailto:root@192.168.120.204's">root@192.168.120.204's</a> password:
mongodb-linux-i686-static-1.8.5.tgz                                                               100%   28MB  28.3MB/s   00:01    
README                                                                                            100%  731     0.7KB/s   00:00    
THIRD-PARTY-NOTICES                                                                               100% 7866     7.7KB/s   00:00    
mongorestore                                                                                      100% 7753KB   7.6MB/s   00:00    
mongod                                                                                            100% 7760KB   7.6MB/s   00:01    
mongoexport                                                                                       100% 7744KB   7.6MB/s   00:00    
bsondump                                                                                          100% 7737KB   7.6MB/s   00:00    
mongofiles                                                                                        100% 7748KB   7.6MB/s   00:00    
mongostat                                                                                         100% 7808KB   7.6MB/s   00:01    
mongos                                                                                            100% 5262KB   5.1MB/s   00:00    
mongo                                                                                             100% 3707KB   3.6MB/s   00:00    
mongoimport                                                                                       100% 7754KB   7.6MB/s   00:01    
mongodump                                                                                         100% 7773KB   7.6MB/s   00:00    
GNU-AGPL-3.0                                                                                      100%   34KB  33.7KB/s   00:00    
[root@localhost ~]# </p> <p>上傳后目標(biāo)機(jī)器的目標(biāo)目錄:
[root@localhost scptest]# ll
總計(jì) 488
drwxr-xr-x 3 root root   4096 03-15 09:33 mongodb
-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz
[root@localhost scptest]#

看完上述內(nèi)容,你們對(duì)怎么在linux中使用scp命令有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI