Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Linux 系統間檔案傳輸:scp 指令跨主機複製實戰

編輯:Linux 系統常見問題解答

Linux 伺服器之間傳檔,最標準、安全的工具是 scp (Secure Copy),底層走 SSH 通道,加密傳輸且保留權限/時間戳記。

基本語法

scp [選項] 來源 目的地
# 遠端路徑格式:user@host:/path

常用場景範例

場景指令說明
遠端 → 本地(單一檔案)scp root@192.168.1.10:/www/test.zip /www/下載遠端檔案至本地目錄
遠端 → 本地(整個目錄)scp -r root@192.168.1.10:/www/data /www/-r 遞迴複製目錄
本地 → 遠端scp /local/file.zip root@192.168.1.10:/www/上傳本地檔案至遠端
遠端 ↔ 遠端(經本地中轉)scp -3 user@host1:/file user@host2:/dir-3 強制經本地轉發(預設直連)
指定 SSH 埠號scp -P 2222 file user@host:/dir大寫 -P(小寫 -p 為保留屬性)
保留原檔屬性scp -p file user@host:/dir保留修改時間、存取時間、權限
限制頻寬scp -l 8192 file user@host:/dir限制 8 Mbps(單位 Kbit/s),避免佔滿頻寬
壓縮傳輸scp -C file user@host:/dir啟用 SSH 壓縮,適合文字/程式碼檔案

進階技巧

  • 免密碼登入:預先部署 SSH 金鑰 (ssh-copy-id),腳本自動化無需輸入密碼
  • 搭配 SSH 多路複用:ControlMaster auto 大幅加速批次 scp
  • 大量小檔案:tar czf - /src | ssh user@host "tar xzf - -C /dst" 管線傳輸比逐個 scp 快數倍
  • rsync 替代:rsync -avz -e ssh /src/ user@host:/dst/ 支援斷點續傳、增量同步,生產環境首選

權限問題排查

  • 遠端用戶需有目標目錄寫入權限
  • SELinux/AppArmor 可能阻擋,檢查 audit.log
  • 根目錄複製需 root 權限,建議改用專用備份帳號 + sudo 限定命令

scp 簡單直觀,適合臨時、少量檔案跨主機傳輸;大規模、常態化同步請改用 rsync 或分散式檔案系統。

Copyright © Windows教程網 All Rights Reserved