Skip to content

SSH 文件传输

SCP、SFTP 和 rsync over SSH 是 SSH 生态中三种互补的文件传输工具,覆盖从简单拷贝到增量同步的全场景需求。

速查卡片

项目内容
SCP 拷贝文件scp file.txt user@host:/path/
SCP 下载文件scp user@host:/path/file.txt ./
SCP 递归拷贝scp -r dir/ user@host:/path/
SCP 指定端口scp -P 2222 file.txt user@host:/path/
SCP 限速scp -l 8192 file.bin user@host:/path/
SCP 保留属性scp -p file.txt user@host:/path/
SFTP 连接sftp user@host
SFTP 批量脚本sftp -b batch.txt user@host
rsync 增量同步rsync -avz -e ssh dir/ user@host:/path/
rsync 限速rsync --bwlimit=10000 -avz -e ssh dir/ user@host:/path/

SCP —— 安全文件拷贝

示例一:基本拷贝操作

bash
# 本地 → 远程
scp file.txt user@host:/home/user/

# 远程 → 本地
scp user@host:/home/user/file.txt ./

# 远程 → 远程(通过本地中转)
scp user@host1:/path/file.txt user@host2:/path/

# 远程 → 远程(直连模式,需要源主机能免密认证目标主机)
scp -R user@host1:/path/file.txt user@host2:/path/

# 远程 → 本地,指定文件名
scp user@host:/var/log/nginx/access.log ./access_$(date +%Y%m%d).log

示例二:目录与属性

bash
# 递归拷贝目录
scp -r /local/project/ user@host:/opt/

# 保留修改时间、访问时间和权限
scp -p file.txt user@host:/path/

# 递归 + 保留属性
scp -rp /local/project/ user@host:/opt/

示例三:连接与控制

bash
# 指定端口(注意是大写 -P)
scp -P 2222 file.txt user@host:/path/

# 指定密钥
scp -i ~/.ssh/id_ed25519 file.txt user@host:/path/

# 限速(Kbit/s)
scp -l 8192 large_file.bin user@host:/path/

# 压缩传输
scp -C large_file.bin user@host:/path/

# 安静模式(不显示进度条)
scp -q file.txt user@host:/path/

# 通过跳板机拷贝
scp -J jump@gateway file.txt user@target:/path/

# 使用传统 SCP 协议(非 SFTP,兼容老服务器)
scp -O file.txt user@old-host:/path/

示例四:通配符与多文件

bash
# 拷贝多个文件
scp file1.txt file2.txt user@host:/path/

# 使用通配符
scp "*.log" user@host:/var/log/

# 从多个远程主机拷贝(需要逐个指定)
scp user@host1:/path/file1.txt user@host2:/path/file2.txt ./

命令行选项参考

选项说明
-P port远程端口(大写 P,小写 -p 用于保留属性)
-p保留修改时间、访问时间和权限
-r递归拷贝目录
-C启用压缩
-l limit限速(Kbit/s)
-i key指定私钥文件
-F config指定 SSH 配置文件
-J host通过跳板机连接
-o option传递 SSH 配置选项
-q安静模式(不显示进度)
-v详细日志
-3远程→远程通过本地中转(默认)
-R远程→远程直连模式
-O使用传统 SCP 协议(兼容老服务器)
-T禁用严格文件名检查

注意:OpenSSH 9.0 起 scp 默认使用 SFTP 协议进行传输,更安全可靠。老服务器如不支持 SFTP,用 -O 回退到传统 SCP 协议。

SFTP —— 交互式文件管理

示例一:交互式会话

bash
# 连接
sftp user@host

# 指定端口
sftp -P 2222 user@host

# 连接并直接进入指定目录
sftp user@host:/var/www/html

进入后常见交互命令:

text
# 导航
pwd                  # 显示远程当前目录
lpwd                 # 显示本地当前目录
ls                   # 列出远程目录
lls                  # 列出本地目录
cd /path             # 切换远程目录
lcd /path            # 切换本地目录

# 传输
get file.txt         # 下载文件
get -r dir/          # 递归下载目录
put file.txt         # 上传文件
put -r dir/          # 递归上传目录

# 断点续传
reget file.txt       # 恢复下载(等同于 get -a)
reput file.txt       # 恢复上传(等同于 put -a)

# 远程文件管理
mkdir dir            # 创建远程目录
lmkdir dir           # 创建本地目录
rm file.txt          # 删除远程文件
rmdir dir            # 删除远程目录
rename old new       # 重命名
chmod 644 file.txt   # 修改权限
ln -s target link    # 创建符号链接

# 退出
exit / bye / quit

示例二:批量脚本

bash
# 创建命令文件 batch.txt
cat > batch.txt << 'EOF'
cd /var/log/nginx
lcd ./logs
get access.log
get error.log
rm access.log.1
exit
EOF

# 执行批处理
sftp -b batch.txt user@host

# 从标准输入读取(密码需要提前配置免密)
echo "get file.txt" | sftp user@host

示例三:单次命令

bash
# 下载文件(一行命令)
sftp user@host:/path/to/file.txt ./local_file.txt

# 上传文件
echo "put local.txt /tmp/remote.txt" | sftp -b - user@host

命令行选项参考

选项说明
-P port远程端口
-i key指定私钥文件
-b batchfile批处理模式
-B size缓冲区大小(默认 32768)
-r递归拷贝目录(用于 get/put 时)
-p保留文件权限和访问时间
-l limit限速(Kbit/s)
-C启用压缩
-q安静模式
-v详细日志
-J host通过跳板机连接
-R num并发请求数(默认 64)

rsync over SSH —— 增量同步

rsync 不是 OpenSSH 的组成部分,但通过 -e ssh 参数可以借用 SSH 的安全通道。相比 scp,它支持增量传输(只传差异部分),适合大文件同步和定期备份。

示例一:基本同步

bash
# 本地 → 远程(增量同步)
rsync -avz -e ssh /local/dir/ user@host:/remote/dir/

# 远程 → 本地
rsync -avz -e ssh user@host:/remote/dir/ /local/dir/

# 注意斜线差别
rsync -avz -e ssh dir/ user@host:/path/    # 同步 dir 的**内容**
rsync -avz -e ssh dir  user@host:/path/    # 同步 dir 目录本身

示例二:常用参数组合

bash
# 标准全量同步(归档模式+压缩+显示进度+排除文件)
rsync -avzP \
  --exclude='node_modules' \
  --exclude='.git' \
  -e ssh ./project/ user@host:/opt/project/

# 镜像同步(删除目标多出的文件)
rsync -avz --delete -e ssh ./project/ user@host:/opt/project/

# 限速(KB/s)
rsync -avz --bwlimit=10000 -e ssh ./dir/ user@host:/path/

常用 rsync 参数

参数说明
-a归档模式(保留权限、时间、符号链接等)
-v详细输出
-z传输时压缩
-P显示进度 + 断点续传
--delete删除目标端多余文件(镜像同步)
--exclude=排除指定文件/目录
--bwlimit=N限速(KB/s)
-n / --dry-run模拟运行(查看会传哪些文件)
-e ssh使用 SSH 传输

工具对比

特性scpsftprsync over SSH
使用方式命令行交互式/批处理命令行
增量传输
断点续传✅(reget/reput)
目录同步基础(-r)递归(-r)完整(-a --delete)
进度条✅(progress)✅(-P)
排除文件✅(--exclude)
限速
适用场景简单拷贝交互式管理定期同步/备份

注意事项

  • scp 的端口选项是大写 -P,与其他 SSH 命令的小写 -p 不同(-p 用于保留文件属性)
  • OpenSSH 9.0+ 的 scp 默认使用 SFTP 协议,老服务器用 -O 回退
  • rsync 对文件名的末尾斜线敏感:带 / 同步目录内容,不带 / 同步目录本身
  • 大文件传输建议用 rsync 而非 scp(出错可续传、节省带宽)

来源

来源链接
OpenSSH scp(1) 手册https://man.openbsd.org/scp
OpenSSH sftp(1) 手册https://man.openbsd.org/sftp