scp是secure copy的簡寫,用於在Linux下進行遠端拷貝檔案的命令,和它類似的命令有cp,不過cp只是在本機進行拷貝不能跨伺服器,而且scp傳輸是加密的。可能會稍微影響一下速度。當你伺服器硬碟變為只讀 read only system時,用scp可以幫你把檔案移出來。另外,scp還非常不佔資源,不會提高多少系統負荷,在這一點上,rsync就遠遠不及它了。雖然 rsync比scp會快一點,但當小檔案眾多的情況下,rsync會導致硬碟I/O非常高,而scp基本不影響系統正常使用。
1.命令格式:
1 |
scp [引數] [原路徑] [目標路徑] |
2.命令功能:
scp是 secure copy的縮寫, scp是linux系統下基於ssh登陸進行安全的遠端檔案拷貝命令。linux的scp命令可以在linux伺服器之間複製檔案和目錄。
3.命令引數:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
-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) 複製檔案:
命令格式:
1 |
scp local_file remote_username@remote_ip:remote_folder |
或者
1 |
scp local_file remote_username@remote_ip:remote_file |
或者
1 |
scp local_file remote_ip:remote_folder |
或者
1 |
scp local_file remote_ip:remote_file |
第1,2個指定了使用者名稱,命令執行後需要輸入使用者密碼,第1個僅指定了遠端的目錄,檔名字不變,第2個指定了檔名
第3,4個沒有指定使用者名稱,命令執行後需要輸入使用者名稱和密碼,第3個僅指定了遠端的目錄,檔名字不變,第4個指定了檔名
(2) 複製目錄:
命令格式:
1 |
scp -r local_folder remote_username@remote_ip:remote_folder |
或者
1 |
scp -r local_folder remote_ip:remote_folder |
第1個指定了使用者名稱,命令執行後需要輸入使用者密碼;
第2個沒有指定使用者名稱,命令執行後需要輸入使用者名稱和密碼;
從遠端伺服器複製到本地伺服器:
從遠端複製到本地的scp命令與上面的命令雷同,只要將從本地複製到遠端的命令後面2個引數互換順序就行了。
例項1:從遠處複製檔案到本地目錄
命令:
1 |
scp root@192.168.120.204:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/ |
輸出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
[root@localhost ~]# cd /opt/soft/ [root@localhost soft]# ll 總計 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]# ll 總計 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:從遠處複製到本地
命令:
1 |
scp -r root@192.168.120.204:/opt/soft/mongodb /opt/soft/ |
輸出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
[root@localhost soft]# ll 總計 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]# ll 總計 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:上傳本地檔案到遠端機器指定目錄
命令:
1 |
scp /opt/soft/nginx-0.5.38.tar.gz root@192.168.120.204:/opt/soft/scptest |
輸出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
上傳前目標機器的目標目錄: [root@localhost soft]# cd scptest/ [root@localhost scptest]# ll 總計 0 [root@localhost scptest]# ll 本地機器上傳: [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]# ll 總計 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:上傳本地目錄到遠端機器指定目錄
命令:
1 |
scp -r /opt/soft/mongodb root@192.168.120.204:/opt/soft/scptest |
輸出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
上傳前目標機器的目標目錄: [root@localhost ~]# cd /opt/soft/scptest/ [root@localhost scptest]# ll 總計 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]# ll 總計 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的目錄中去
本系列文章:
每天一個 Linux 命令(1):ls命令
每天一個 Linux 命令(2):cd命令
每天一個 Linux 命令(3):pwd命令
每天一個 Linux 命令(4):mkdir命令
每天一個 Linux 命令(5):rm 命令
每天一個 Linux 命令(6):rmdir 命令
每天一個 Linux 命令(7):mv命令
每天一個 Linux 命令(8):cp 命令
每天一個 Linux 命令(9):touch 命令
每天一個 Linux 命令(10):cat 命令
每天一個 Linux 命令(11):nl 命令
每天一個 Linux 命令(12):more 命令
每天一個 Linux 命令(13):less 命令
每天一個 Linux 命令(14):head 命令
每天一個 Linux 命令(15):tail 命令
每天一個 Linux 命令(16):which命令
每天一個 Linux 命令(17):whereis 命令
每天一個 Linux 命令(18):locate 命令
每天一個 Linux 命令(19):find 命令概覽
每天一個 Linux 命令(20):find命令之exec
每天一個 Linux 命令(21):find命令之xargs
每天一個 Linux 命令(22):find 命令的引數詳解
每天一個 Linux 命令(23):Linux 目錄結構
每天一個 Linux 命令(24):Linux 檔案型別與副檔名
每天一個 Linux 命令(25):Linux 檔案屬性詳解
每天一個 Linux 命令(26):用 SecureCRT 來上傳和下載檔案
每天一個 Linux 命令(27):linux chmod 命令
每天一個 Linux 命令(28):tar 命令
每天一個 Linux 命令(29): chgrp 命令
每天一個 Linux 命令(30): chown 命令
每天一個 Linux 命令(31): /etc/group 檔案詳解
每天一個 Linux 命令(32):gzip 命令
每天一個 Linux 命令(33):df 命令
每天一個 Linux 命令(34): du 命令
每天一個 Linux 命令(35): ln 命令
每天一個 Linux 命令(36): diff 命令
每天一個 Linux 命令(37): date 命令
每天一個 Linux 命令(38): cal 命令
每天一個 Linux 命令(39): grep 命令
每天一個 Linux 命令(40): wc 命令
每天一個 Linux 命令(41): ps 命令
每天一個 Linux 命令(44): top 命令
每天一個 Linux 命令(45): free 命令
每天一個 Linux 命令(46): vmstat 命令
每天一個 Linux 命令(47): iostat 命令
每天一個 Linux 命令(48): watch 命令
每天一個 Linux 命令(49): at 命令
每天一個 Linux 命令(50): crontab 命令
每天一個 Linux 命令(51): lsof 命令
每天一個 Linux 命令(52): ifconfig 命令
每天一個 Linux 命令(53): route 命令
每天一個 Linux 命令(54): ping 命令
每天一個 Linux 命令(55): traceroute 命令
每天一個 Linux 命令(56): netstat 命令
每天一個 Linux 命令(57): ss 命令
每天一個 Linux 命令(58): telnet 命令
每天一個 Linux 命令(59): rcp 命令