每天一個 Linux 命令:scp 命令
scp
是 secure copy
的簡寫,用於在 Linux 下進行遠程拷貝文件的命令,和它類似的命令有 cp
,不過 cp
只是在本機進行拷貝不能跨服務器,而且 scp
傳輸是加密的。可能會稍微影響一下速度。
當你服務器硬盤變爲只讀 read only system 時,用 scp
可以幫你把文件移出來。另外,scp
還非常不佔資源,不會提高多少系統負荷,在這一點上,rsync
就遠遠不及它了。雖然 rsync
比 scp
會快一點,但當小文件衆多的情況下,rsync
會導致硬盤 I/O 非常高,而 scp
基本不影響系統正常使用。
1.命令格式:
scp [參數] [原路徑] [目標路徑]
2.命令功能:
scp 是 secure copy 的縮寫,scp 是 linux 系統下基於 ssh 登陸進行安全的遠程文件拷貝命令。linux 的 scp 命令可以在 linux 服務器之間複製文件和目錄。
3.命令參數:
-1 強制 scp 命令使用協議 ssh1
-2 強制 scp 命令使用協議 ssh2
-4 強制 scp 命令只使用 IPv4 尋址
-6 強制 scp 命令只使用 IPv6 尋址
-B 使用批處理模式(傳輸過程中不詢問傳輸口令或短語)
-C 允許壓縮。(將-C 標誌傳遞給 ssh,從而打開壓縮功能)
-p 保留原文件的修改時間,訪問時間和訪問權限。
-q 不顯示傳輸進度條。
-r 遞歸複製整個目錄。
-v 詳細方式顯示輸出。scp 和 ssh(1) 會顯示出整個過程的調試信息。這些信息用於調試連接,驗證和配置問題。
-c cipher 以 cipher 將數據傳輸進行加密,這個選項將直接傳遞給 ssh。
-F ssh_config 指定一個替代的 ssh 配置文件,此參數直接傳遞給 ssh。
-i identity_file 從指定文件中讀取傳輸時使用的密鑰文件,此參數直接傳遞給 ssh。
-l limit 限定用戶所能使用的帶寬,以 Kbit/s 爲單位。
-o ssh_option 如果習慣於使用 ssh_config(5) 中的參數傳遞方式,
-P port 注意是大寫的 P, port 是指定數據傳輸用到的端口號
-S program 指定加密傳輸時所使用的程序。此程序必須能夠理解 ssh(1) 的選項。
4.使用實例:
scp 命令的實際應用概述:
從本地服務器複製到遠程服務器:
(1) 複製文件:
命令格式:
scp local_file remote_username@remote_ip:remote_folder
或者
scp local_file remote_username@remote_ip:remote_file
或者
scp local_file remote_ip:remote_folder
或者
scp local_file remote_ip:remote_file
第 1,2 個指定了用戶名,命令執行後需要輸入用戶密碼,第 1 個僅指定了遠程的目錄,文件名字不變,第 2 個指定了文件名
第 3,4 個沒有指定用戶名,命令執行後需要輸入用戶名和密碼,第 3 個僅指定了遠程的目錄,文件名字不變,第 4 個指定了文件名
(2) 複製目錄:
命令格式:
scp -r local_folder remote_username@remote_ip:remote_folder
或者
scp -r local_folder remote_ip:remote_folder
第 1 個指定了用戶名,命令執行後需要輸入用戶密碼;
第 2 個沒有指定用戶名,命令執行後需要輸入用戶名和密碼;
從遠程服務器複製到本地服務器:
從遠程複製到本地的 scp 命令與上面的命令雷同,只要將從本地複製到遠程的命令後面 2 個參數互換順序就行了。
實例 1:從遠處複製文件到本地目錄
命令:
scp root@192.168.120.204:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/
輸出:
[root@localhost ~]# cd /opt/soft/
[root@localhost soft]# 11
總計 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 root@192.168.120.204:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/
root@192.168.120.204's password:
nginx-0.5.38.tar.gz 100% 479KB 478.7KB/s 00:00
[root@localhost soft]# 11
總計 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 機器上的 / opt/soft / 的目錄中下載 nginx-0.5.38.tar.gz 文件到本地 / opt/soft / 目錄中
實例 2:從遠處複製到本地
命令:
scp -r root@192.168.120.204:/opt/soft/mongodb /opt/soft/
輸出:
[root@localhost soft]# 11
總計 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 root@192.168.120.204:/opt/soft/mongodb /opt/soft/
root@192.168.120.204's 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]# 11
總計 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 機器上的 / opt/soft / 中下載 mongodb 目錄到本地的 / opt/soft / 目錄來。
實例 3:上傳本地文件到遠程機器指定目錄
命令:
scp /opt/soft/nginx-0.5.38.tar.gz root@192.168.120.204:/opt/soft/scptest
輸出:
上傳前目標機器的目標目錄:
[root@localhost soft]# cd scptest/
[root@localhost scptest]# 11
總計 0
[root@localhost scptest]# 11
本地機器上傳:
[root@localhost soft]# scp /opt/soft/nginx-0.5.38.tar.gz root@192.168.120.204:/opt/soft/scptest
root@192.168.120.204's password:
nginx-0.5.38.tar.gz 100% 479KB 478.7KB/s 00:00
[root@localhost soft]#
上傳後目標機器的目標目錄:
[root@localhost scptest]# 11
總計 484
-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz
[root@localhost scptest]#
說明:
複製本地 opt/soft / 目錄下的文件 nginx-0.5.38.tar.gz 到遠程機器 192.168.120.204 的 opt/soft/scptest 目錄
實例 4:上傳本地目錄到遠程機器指定目錄
命令:
scp -r /opt/soft/mongodb root@192.168.120.204:/opt/soft/scptest
輸出:
上傳前目標機器的目標目錄:
[root@localhost ~]# cd /opt/soft/scptest/
[root@localhost scptest]# 11
總計 484
-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz
[root@localhost scptest]#
本地機器上傳:
[root@localhost ~]# scp -r /opt/soft/mongodb root@192.168.120.204:/opt/soft/scptest
root@192.168.120.204's 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 ~]#
上傳後目標機器的目標目錄:
[root@localhost scptest]# 11
總計 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]#
說明:
上傳本地目錄 /opt/soft/mongodb 到遠程機器 192.168.120.204 上 / opt/soft/scptest 的目錄中去
轉自:www.cnblogs.com/peida/archive/2013/03/15/2960802.html
****推薦關注「算法愛好者」,修煉編程內功
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/KhqLEvHJz8RBIz7rMHk-lw