强曰为道
与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

Rclone 数据迁移完全指南 / 第 8 章 - 服务模式

第 8 章 - 服务模式


8.1 服务模式概述

Rclone 的 serve 命令可以将本地或远程存储以各种协议对外提供服务,实现:

  • 通过 HTTP 浏览和下载文件
  • 通过 WebDAV 让应用程序(如 Office)直接编辑云端文件
  • 通过 FTP 管理文件
  • 通过 S3 协议提供兼容 S3 的对象存储服务

支持的服务协议

协议命令说明典型场景
HTTPserve httpHTTP 文件浏览/下载临时文件共享
WebDAVserve webdavWebDAV 读写服务Office 在线编辑
FTPserve ftpFTP 文件传输FTP 客户端访问
SFTPserve sftpSFTP 安全文件传输安全文件访问
S3serve s3兼容 S3 的对象存储自建 S3 服务
Resticserve resticRestic 备份服务端Restic 备份

8.2 serve http — HTTP 文件服务

基本用法

# 共享本地目录
rclone serve http /data/public/ --addr :8080

# 共享远程存储
rclone serve http s3:my-bucket/ --addr :8080

# 后台运行
rclone serve http /data/ --addr :8080 --daemon

浏览器访问 http://localhost:8080 即可浏览和下载文件。

添加认证

# 用户名密码认证
rclone serve http /data/ --addr :8080 \
  --user admin \
  --pass mypassword

# 使用 htpasswd 文件(多用户)
rclone serve http /data/ --addr :8080 \
  --htpasswd /etc/rclone.htpasswd

# 生成 htpasswd 文件
htpasswd -bc /etc/rclone.htpasswd admin mypassword

高级配置

# TLS/HTTPS
rclone serve http /data/ --addr :443 \
  --cert /etc/ssl/cert.pem \
  --key /etc/ssl/key.pem

# 自定义模板
rclone serve http /data/ --addr :8080 \
  --template /path/to/template.html

# 限制访问
rclone serve http /data/ --addr :8080 \
  --user readonly \
  --pass secret \
  --read-only

临时文件共享

#!/bin/bash
# share.sh - 快速共享文件
PORT=${1:-8080}
DIR=${2:-.}

echo "Sharing $DIR on port $PORT..."
echo "Press Ctrl+C to stop"

rclone serve http "$DIR" \
  --addr ":$PORT" \
  --user share \
  --pass "$(openssl rand -base64 12)" \
  --read-only

8.3 serve webdav — WebDAV 服务

WebDAV(Web Distributed Authoring and Versioning)允许应用程序直接读写远程文件。

基本用法

# 本地目录
rclone serve webdav /data/documents/ --addr :8080

# 远程存储
rclone serve webdav gdrive:Documents/ --addr :8080 \
  --user admin --pass mypassword

Nextcloud 风格 WebDAV

# 模拟 Nextcloud 的 WebDAV 接口
rclone serve webdav s3:my-files/ --addr :8080 \
  --user admin --pass mypassword \
  --realm "My Cloud Storage"

Windows 挂载 WebDAV

# Windows 中映射网络驱动器
# 1. 打开"此电脑" → "映射网络驱动器"
# 2. 输入: http://server-ip:8080/
# 3. 输入用户名密码

# 或使用命令行
net use Z: http://server-ip:8080/ /user:admin mypassword

macOS 挂载 WebDAV

# 在 Finder 中连接
# 菜单 → 前往 → 连接服务器 → http://server-ip:8080/

# 或命令行
mount_webdav -s -v webdav http://server-ip:8080/ /Volumes/webdav

LibreOffice / OnlyOffice 集成

# 启动 WebDAV 服务后,LibreOffice 可以直接打开云端文件
# 文件 → 打开 → 输入 URL: http://server:8080/document.docx

8.4 serve ftp — FTP 服务

基本用法

# FTP 服务
rclone serve ftp /data/ --addr :2121 \
  --user ftpuser --pass ftppass

# 远程存储
rclone serve ftp s3:my-bucket/ --addr :2121 \
  --user ftpuser --pass ftppass

TLS 加密 FTP

rclone serve ftp /data/ --addr :2121 \
  --user ftpuser --pass ftppass \
  --tls-cert /etc/ssl/cert.pem \
  --tls-key /etc/ssl/key.pem

FTP 客户端连接

# 使用 lftp
lftp ftp://ftpuser:ftppass@localhost:2121

# 使用 curl
curl ftp://ftpuser:ftppass@localhost:2121/

# 使用 FileZilla
# 主机: ftp://localhost
# 端口: 2121
# 用户名: ftpuser
# 密码: ftppass

8.5 serve sftp — SFTP 服务

基本用法

# SFTP 服务
rclone serve sftp /data/ --addr :2222 \
  --user sftpuser --key /etc/rclone_sftp_key

# 生成 SSH 密钥
ssh-keygen -t ed25519 -f /etc/rclone_sftp_key -N ""

多用户配置

# 使用 authorized_keys 文件
rclone serve sftp /data/ --addr :2222 \
  --user sftpuser \
  --authorized-keys /home/sftpuser/.ssh/authorized_keys

连接测试

# 使用 ssh
sftp -P 2222 sftpuser@localhost

# 使用 scp
scp -P 2222 sftpuser@localhost:/file.txt ./

8.6 serve s3 — 自建 S3 服务

将任意 Rclone 支持的存储以 S3 兼容协议对外提供服务。

基本用法

# 将本地目录以 S3 协议提供服务
rclone serve s3 /data/s3-store/ --addr :8333

# 将 Google Drive 以 S3 协议提供服务
rclone serve s3 gdrive: --addr :8333

配合 MinIO 客户端

# 配置 MinIO 客户端
mc alias set myserver http://localhost:8333 accessKey secretKey

# 列出 Bucket
mc ls myserver/

# 上传文件
mc cp file.txt myserver/mybucket/

# 下载文件
mc cp myserver/mybucket/file.txt ./

配合 Rclone 客户端

# 客户端配置
[local-s3]
type = s3
provider = Other
endpoint = http://localhost:8333
access_key_id = rclone
secret_access_key = rclone

# 使用
rclone ls local-s3:

8.7 serve restic — Restic 备份服务

Restic 是一个高效的加密备份工具,Rclone 可以作为其服务端。

配置

# 启动 Restic 服务
rclone serve restic s3:restic-repo/ --addr :8181

# 使用 Restic 客户端
export RESTIC_REPOSITORY=rest:http://localhost:8181/
export RESTIC_PASSWORD=your-password

restic init
restic backup /data/
restic snapshots

8.8 安全配置

TLS/HTTPS

# 生成自签名证书(测试用)
openssl req -x509 -newkey rsa:4096 -nodes \
  -keyout key.pem -out cert.pem -days 365 \
  -subj "/CN=localhost"

# 使用 TLS 启动服务
rclone serve http /data/ --addr :443 \
  --cert cert.pem --key key.pem

访问控制

# 限制只读访问
rclone serve webdav /data/ --addr :8080 --read-only

# 使用基本认证
rclone serve http /data/ --addr :8080 \
  --user admin --pass $(rclone obscure mypassword)

# 绑定特定地址(只允许本地访问)
rclone serve http /data/ --addr 127.0.0.1:8080

使用反向代理

# nginx 反向代理配置
server {
    listen 443 ssl;
    server_name files.example.com;

    ssl_certificate /etc/ssl/cert.pem;
    ssl_certificate_key /etc/ssl/key.pem;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        client_max_body_size 10G;
    }
}

8.9 服务管理

使用 systemd 管理服务

sudo tee /etc/systemd/system/rclone-webdav.service << 'EOF'
[Unit]
Description=Rclone WebDAV Service
After=network-online.target

[Service]
Type=simple
ExecStart=/usr/bin/rclone serve webdav /data/documents/ \
  --addr 127.0.0.1:8080 \
  --user admin \
  --pass-secret RCLONE_WEBDAV_PASS \
  --log-level INFO
Restart=on-failure
RestartSec=5
Environment=RCLONE_WEBDAV_PASS=your-secure-password

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable rclone-webdav
sudo systemctl start rclone-webdav

注意事项

⚠️ 生产环境:不建议将 serve 直接暴露到公网。建议使用 nginx 反向代理 + TLS + 认证。

⚠️ 性能serve 命令适合中小规模使用。大规模并发访问请使用专用服务(如 MinIO、Nginx)。

⚠️ FTP 安全:FTP 协议不加密,生产环境请使用 SFTP 或 WebDAV over HTTPS。

💡 临时分享serve http 非常适合临时分享文件,几分钟内即可启动。

💡 跨平台兼容:WebDAV 被大多数操作系统原生支持,是最通用的共享协议。


扩展阅读


上一章← 第 7 章 - 挂载远程存储 下一章第 9 章 - 加密存储 →