一、指令簡介
scp是secure copy的簡寫,用于在Linux下進行遠端拷貝檔案的指令,和它類似的指令有cp,不過cp隻是在本機進行拷貝不能跨伺服器,而且scp傳輸是加密的。可能會稍微影響一下速度。當你伺服器硬碟變為隻讀 read only system時,用scp可以幫你把檔案移出來。另外,scp還非常不占資源,不會提高多少系統負荷,在這一點上,rsync就遠遠不及它了。雖然 rsync比scp會快一點,但當小檔案衆多的情況下,rsync會導緻硬碟I/O非常高,而scp基本不影響系統正常使用。
scp指令格式:scp [參數] [原路徑] [目标路徑]
二、使用示例
1、将本地檔案傳送到遠端伺服器
#scp file remote_host_ip:/dir
[[email protected] opt]# scp wordpress-5.3.2.zip 192.168.0.125:/opt/
[email protected]’s password:
wordpress-5.3.2.zip 100% 13MB 70.5MB/s 00:00
2、将本地檔案夾傳送到遠端伺服器
#scp -r dir remote_host_ip:/dir
[[email protected] opt]# scp -r test 192.168.0.125:/opt/
[email protected]’s password:
hi.txt 100% 0 0.0KB/s 00:00
[[email protected] ~]# ll /opt/test/
total 0
-rw-r–r-- 1 root root 0 Jan 13 17:01 hi.txt
[[email protected] ~]# ll /opt/
total 13116
drwxr-xr-x. 2 root root 6 Oct 31 2018 rh
drwxr-xr-x 2 root root 20 Jan 13 17:01 test
-rw-r–r-- 1 root root 13427353 Jan 13 16:57 wordpress-5.3.2.zip
3、将遠端檔案傳送到本地
#scp remote_host_ip:/dir/filename local_dir
[[email protected] opt]# rm -rf wordpress-5.3.2.zip*
[[email protected] opt]# ll
total 4
-rw-r–r--. 1 root root 453 Jan 11 06:15 download.log
drwxr-xr-x. 2 root root 20 Jan 13 01:00 test
[[email protected] opt]# scp 192.168.0.125:/opt/wordpress-5.3.2.zip ./
[email protected]’s password:
wordpress-5.3.2.zip 100% 13MB 56.5MB/s 00:00
[[email protected] opt]# ll
total 13120
-rw-r–r--. 1 root root 453 Jan 11 06:15 download.log
drwxr-xr-x. 2 root root 20 Jan 13 01:00 test
-rw-r–r--. 1 root root 13427353 Jan 13 01:03 wordpress-5.3.2.zip
4、将遠端檔案夾傳送到本地
#scp -r remote_host_ip:/dir/dir local_dir/
[[email protected] opt]# scp -r 192.168.0.125:/opt/test2 ./
[email protected]’s password:
hi.txt 100% 0 0.0KB/s 00:00
[[email protected] opt]# ll
total 13120
-rw-r–r--. 1 root root 453 Jan 11 06:15 download.log
drwxr-xr-x. 2 root root 20 Jan 13 01:00 test
drwxr-xr-x. 2 root root 20 Jan 13 01:05 test2
-rw-r–r--. 1 root root 13427353 Jan 13 01:03 wordpress-5.3.2.zip
5、使用通配符批量傳送
[[email protected] opt]# ll
total 13120
-rw-r–r--. 1 root root 453 Jan 11 06:15 download.log
drwxr-xr-x. 2 root root 20 Jan 13 01:00 test
drwxr-xr-x. 2 root root 20 Jan 13 01:05 test2
-rw-r–r--. 1 root root 0 Jan 13 01:07 test3
-rw-r–r--. 1 root root 13427353 Jan 13 01:03 wordpress-5.3.2.zip
[[email protected] opt]# scp -r test* 192.168.0.125:/tmp/
[email protected]’s password:
hi.txt 100% 0 0.0KB/s 00:00
hi.txt 100% 0 0.0KB/s 00:00
test3
6、使用-l參數限制傳送速率
#scp -l 速率 其中速率機關為Kbit/s,800Kbit/s=100KB/s
[[email protected] opt]# scp -l 800 wordpress-5.3.2.zip 192.168.0.125:/tmp/
[email protected]’s password:
wordpress-5.3.2.zip 100% 13MB 99.9KB/s 02:11
7、使用-P端口指定遠端伺服器ssh端口為自定義端口
#scp -P 60022 指定遠端伺服器scp服務端口
8、指定scp使用的使用者
#scp file [email protected]_host_ip:/dir 預設與本地登入使用者一緻
[[email protected] opt]# scp wordpress-5.3.2.zip [email protected]:/home/wuhs/
[email protected]’s password:
wordpress-5.3.2.zip
9、保留原文檔的時間等屬性傳送
#scp -p 預設安裝新建立檔案時間,使用-p參數保留原文檔時間和權限
三、參數解釋
參數說明:
-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)的選項。