scp(secure copy)是專門用於主機之間進行遠端檔案拷貝的指令。
scp [參數] [來源檔案] [目標檔案]
scp 在兩台主機之間複製檔案,底層以 ssh(1) 作為傳輸通道,因此沿用同樣的認證機制與加密安全性。它也允許在兩個遠端伺服器之間直接複製檔案。
1)-r:遞迴複製整個目錄。
2)-p:保留檔案的最後修改時間、最後存取時間與權限模式。
1)把遠端檔案複製到本機
root@Unbuntu:/home/sunjimeng/桌面# rcp sunmeng@CentOS:/home/sunmeng/rcptext ./ root@Unbuntu:/home/sunjimeng/桌面# ll 總用量 8 drwxr-xr-x 2 sunjimeng sunjimeng 4096 6月 30 08:56 ./ drwxr-xr-x 20 sunjimeng sunjimeng 4096 6月 29 08:27 ../ -rw-r--r-- 1 root root 0 6月 30 08:56 rcptext
2)複製整個目錄需加上 -r
root@Unbuntu:/home/sunjimeng/桌面# rcp sunmeng@CentOS:/home/sunmeng/Documents ./ rcp: /home/sunmeng/Documents: not a plain file root@Unbuntu:/home/sunjimeng/桌面# rcp -r sunmeng@CentOS:/home/sunmeng/Documents ./ root@Unbuntu:/home/sunjimeng/桌面# ll 總用量 12 drwxr-xr-x 3 sunjimeng sunjimeng 4096 6月 30 08:59 ./ drwxr-xr-x 20 sunjimeng sunjimeng 4096 6月 29 08:27 ../ drwxr-xr-x 2 root root 4096 6月 30 08:59 Documents/ -rw-r--r-- 1 root root 0 6月 30 08:56 rcptext
3)把本機檔案推送到遠端
CentOS: [root@localhost Documents]# ll 總用量 8 -rw-r--r--. 1 root root 38 6月 27 07:19 text1.txt -rw-r--r--. 1 root root 46 6月 27 07:19 text2.txt Unbuntu: root@Unbuntu:/home/sunjimeng/桌面# touch Unbuntu.txt root@Unbuntu:/home/sunjimeng/桌面# rcp ./Unbuntu.txt sunmeng@CentOS:/home/sunmeng/Documents/ CentOS: [root@localhost Documents]# ll 總用量 8 -rw-r--r--. 1 root root 38 6月 27 07:19 text1.txt -rw-r--r--. 1 root root 46 6月 27 07:19 text2.txt -rw-r--r-- 1 sunmeng sunmeng 0 6月 29 18:05 Unbuntu.txt
4)複製時保留原檔案屬性
CentOS: [root@localhost Documents]# rcp text1.txt root@Unbuntu:/home/sunjimeng/桌面 [root@localhost Documents]# rcp -p text2.txt root@Unbuntu:/home/sunjimeng/桌面 Unbuntu: root@Unbuntu:/home/sunjimeng/桌面# ll 總用量 16 drwxr-xr-x 2 sunjimeng sunjimeng 4096 6月 30 09:20 ./ drwxr-xr-x 20 sunjimeng sunjimeng 4096 6月 30 09:18 ../ -rw-r--r-- 1 root root 38 6月 30 09:19 text1.txt -rw-r--r-- 1 root root 46 6月 27 22:19 text2.txt
可以清楚看到,加上 -p 之後 text2.txt 的時間戳維持原樣,而未加參數的 text1.txt 則被改成當下時間。
rcp 透過 rsh 通道在遠端複製檔案或目錄;scp 則走 ssh 通道。rcp 屬不安全的傳輸方式,scp 相對安全,代價是執行時可能需要使用者輸入密碼與口令。
Linux 下常見的跨主機資料傳輸指令與工具還有:rcp、scp、rsync、ftp、sftp、lftp、wget、curl(參考:https://blog.csdn.net/emili/article/details/6858818)。