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 日