使用 rclone 挂载 Cloudflare R2 到本地
☁️ 使用 rclone 挂载 Cloudflare R2 到本地
Cloudflare R2 是一款兼容 S3 协议的对象存储服务,价格低廉且无需出网流量费用。
通过 rclone,我们可以将 R2 直接挂载成本地磁盘目录,方便在本地以文件系统方式管理。
🧱 一、安装 rclone 与 fuse
Arch Linux
sudo pacman -S rclone fuse3
Ubuntu / Debian
sudo apt install rclone fuse3
⚙️ 二、配置 Cloudflare R2 远程存储
执行命令:
rclone config
依次按提示输入:
n) New remote
name> r2
Storage> s3
provider> 6 # Cloudflare
env_auth> false
access_key_id> <你的Access Key ID>
secret_access_key> <你的Secret Access Key>
region> auto
endpoint> https://<你的账户ID>.r2.cloudflarestorage.com
Edit advanced config? n
完成后保存。可使用以下命令测试:
rclone ls r2:img
如果列出文件即配置成功。
💡 提示:
Cloudflare R2 的 Access Key ID 与 Secret Access Key 可在 Cloudflare 控制台 → R2 → API Tokens 中生成。
🗂️ 三、测试挂载命令
rclone mount r2:img /home/ding/mnt/Cloudflare-R2 \
--vfs-cache-mode writes \
--allow-other \
--dir-cache-time 72h \
--poll-interval 5m \
--umask 002
🚫 报错示例:
fusermount3: option allow_other only allowed if 'user_allow_other' is set in /etc/fuse.conf
Fatal error: failed to mount FUSE fs: fusermount: exit status 1
⚠️ 四、问题原因与解决方案
--allow-other 允许其他用户访问挂载点,但 FUSE 默认禁用此功能。
需要手动启用:
编辑 /etc/fuse.conf
sudo nano /etc/fuse.conf
取消注释或添加以下行:
user_allow_other
保存退出。
重新加载 fuse 模块(可选)
sudo modprobe fuse
🧩 五、重新挂载测试
rclone mount r2:img /home/ding/mnt/Cloudflare-R2 \
--vfs-cache-mode writes \
--allow-other \
--dir-cache-time 72h \
--poll-interval 5m \
--umask 002
✅ 成功后验证:
df -h | grep rclone
输出示例:
r2:img on /home/ding/mnt/Cloudflare-R2 type fuse.rclone (rw,nosuid,nodev,relatime,user_id=1000,group_id=1000,allow_other)
🔄 六、配置 systemd 自动挂载
创建服务文件:
sudo nano /etc/systemd/system/rclone-r2.service
写入以下内容:
[Unit]
Description=Mount Cloudflare R2 via rclone
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=ding
Group=ding
ExecStart=/usr/bin/rclone mount r2:img /home/ding/mnt/Cloudflare-R2 \
--vfs-cache-mode writes \
--allow-other \
--dir-cache-time 72h \
--poll-interval 5m \
--umask 002
ExecStop=/bin/fusermount3 -u /home/ding/mnt/Cloudflare-R2
Restart=on-failure
[Install]
WantedBy=default.target
启用与启动:
sudo systemctl daemon-reload
sudo systemctl enable rclone-r2
sudo systemctl start rclone-r2
查看状态:
systemctl status rclone-r2
🧾 七、验证挂载
mount | grep rclone
ls /home/ding/mnt/Cloudflare-R2
如果能看到 R2 文件即说明挂载成功。
📘 八、总结
| 项目 | 说明 |
|---|---|
| 问题 | allow_other only allowed if 'user_allow_other' is set in /etc/fuse.conf |
| 原因 | FUSE 默认禁用 allow_other |
| 解决 | 编辑 /etc/fuse.conf 添加 user_allow_other |
| 推荐 | 使用 systemd 开机自动挂载 |
| 适用系统 | Arch / Debian / Ubuntu / Fedora 等 |
✅ 最终效果:
-
Cloudflare R2 已成功以本地目录挂载;
-
可通过文件管理器或命令行操作;
-
支持开机自动挂载;
-
结合
PicGo、Typora、Obsidian等可实现自动图床上传与本地访问同步。
💡 附加建议
若仅需要只读访问,可添加
--read-only参数;建议将
rclone.conf备份到 dotfiles 或私有仓库,以便重装系统后快速恢复。可结合 Nginx 或 caddy 提供 Web 访问层,实现私有图床浏览。