scp

透過 SSH 連線複製檔案。

執行前須知

-P(大寫)指定連接埠;小寫 -p 保留檔案時間及權限。OpenSSH 9.0 及之後的 scp 預設使用 SFTP,伺服器須支援相應子系統。傳輸可能覆寫目標檔案,請審慎選擇檔名。範例假設 report.txt 已存在;Windows 的路徑與引號規則不同。

使用範例

將本機檔案上傳至遠端帳號的家目錄。

scp -P 2222 ./report.txt alice@server.example.com:~/

下載遠端檔案,並使用新的本機檔名。

scp -P 2222 alice@server.example.com:~/report.txt ./report-downloaded.txt

參數與範例

-P port常用

使用指定的 SSH 連接埠。大寫 P 指定連接埠。

範例
scp -P 2222 ./report.txt alice@server.example.com:~/
-i identity_file常用

使用指定的私鑰進行身份驗證。

範例
scp -i ~/.ssh/id_ed25519 ./report.txt alice@server.example.com:~/
-r常用

遞歸複製目錄。在覆蓋檔案之前檢查來源和目標。

範例
scp -r ./reports alice@server.example.com:~/
-p常用

盡可能保留修改和存取時間以及檔案模式。小寫 p 不設定連接埠。

範例
scp -p ./report.txt alice@server.example.com:~/
-J destination

透過指定的跳轉主機進行連線。

範例
scp -J alice@bastion.example.com ./report.txt alice@internal.example.com:~/
-o option

傳遞 SSH 客戶端配置選項。

範例
scp -o ConnectTimeout=10 ./report.txt alice@server.example.com:~/
-C

請求 SSH 壓縮。

範例
scp -C ./report.txt alice@server.example.com:~/
-l limit

限制頻寬(以千位元每秒為單位)。

範例
scp -l 1024 ./report.txt alice@server.example.com:~/
-v

顯示連接診斷;輸出可能包含敏感細節。

範例
scp -v ./report.txt alice@server.example.com:~/
-O

必要時使用舊版 SCP 協定以實現相容性。現代 OpenSSH 預設使用 SFTP。

範例
scp -O ./report.txt alice@server.example.com:~/

權威參考

核對日期:2026 年 10 月 27 日