概述

本文介绍如何在Linux服务器上使用rclone挂载Google Drive,实现云端存储的本地访问。适用于Debian 13/12系统,涵盖手动挂载和systemd自动挂载两种方案。

环境要求

  • 操作系统: Debian 13 / Debian 12 / Ubuntu 20.04+
  • 网络: 可访问Google服务(需代理或直连)
  • 软件依赖: rclone, fuse
  • 用户权限: 普通用户(systemd配置需要sudo)

一、软件安装

1.1 安装rclone和fuse

sudo apt update
sudo apt install -y rclone fuse

注意事项:

  • fuse包是挂载功能的必要条件,仅安装rclone无法实现mount功能
  • 如系统未安装fuse,rclone mount命令将报错

二、rclone配置

2.1 启动配置向导

rclone config

2.2 配置参数说明

步骤选项说明
1n新建remote
2名称建议gdriveclawdrive,后续挂载时使用
3Storage选择18 (Google Drive)
4client_id留空(使用rclone默认应用)
5client_secret留空
6scope选择1 (Full access)
7root_folder_id留空(访问整个Drive)
8service_account_file留空
9Edit advanced confign
10Use auto config选择y

2.3 远程服务器授权流程(关键步骤)

对于无图形界面的服务器,需使用SSH端口转发完成授权:

步骤1: 在本地电脑建立SSH隧道

ssh -L localhost:53682:localhost:53682 username@remote_server

参数说明:

  • -L localhost:53682:localhost:53682:将本地53682端口转发到服务器的53682端口
  • username:服务器用户名
  • remote_server:服务器IP或域名

步骤2: 在SSH会话中运行rclone配置

rclone config
# 按上述参数配置,在"Use auto config?"步骤选择 y

终端将显示:

Waiting for code...
Go to this URL in your browser: http://localhost:53682/auth?state=xxxxx

步骤3: 在本地浏览器打开链接

直接在本地浏览器访问:

http://localhost:53682/auth?state=xxxxx

注意:由于SSH端口转发,localhost指向的是远程服务器,但可以在本地浏览器打开。

步骤4: 登录Google账号并授权

浏览器会提示登录Google账号,授权rclone访问Google Drive。

步骤5: 授权自动完成

授权成功后,浏览器会显示成功信息,同时服务器上的rclone会自动接收到token并继续配置。

参考文档: rclone官方远程设置指南

常见问题: 如未获取到有效的refresh_token,后续使用时会报错"empty token found"。必须完成上述完整授权流程。

三、验证配置

3.1 测试连接

rclone lsd gdrive:

预期输出:列出Google Drive根目录下的文件夹

3.2 查看配置详情

cat ~/.config/rclone/rclone.conf

完整的配置应包含:

[gdrive]
type = drive
scope = drive
token = {"access_token":"xxx","token_type":"Bearer","refresh_token":"xxx","expiry":"xxx"}

四、手动挂载

4.1 创建挂载点

mkdir -p ~/GoogleDrive

4.2 执行挂载

rclone mount gdrive: ~/GoogleDrive --vfs-cache-mode writes

参数说明:

  • --vfs-cache-mode writes: 启用写入缓存,提升文件操作稳定性
  • 可选--allow-other: 允许其他用户访问挂载点(需配置/etc/fuse.conf)

4.3 常见错误处理

错误1: fusermount: option allow_other only allowed if 'user_allow_other' is set

解决方案:

# 编辑fuse配置
sudo nano /etc/fuse.conf
# 取消注释: user_allow_other

或在挂载命令中移除--allow-other参数。

4.4 验证挂载

# 查看挂载状态
mount | grep rclone

# 查看文件
ls ~/GoogleDrive

# 测试读写
echo "test" > ~/GoogleDrive/test.txt
cat ~/GoogleDrive/test.txt

4.5 卸载

fusermount -u ~/GoogleDrive

五、systemd自动挂载

5.1 创建服务文件

创建 /etc/systemd/system/rclone-mount.service

[Unit]
Description=rclone mount Google Drive
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=username
Group=groupname
ExecStart=/usr/bin/rclone mount gdrive: /home/username/GoogleDrive --vfs-cache-mode writes
ExecStop=/bin/fusermount -u /home/username/GoogleDrive
Restart=on-failure
RestartSec=10

[Install]
WantedBy=default.target

重要提示: ExecStart中不应使用--daemon参数。

5.2 参数冲突说明

在systemd服务中使用--daemon参数会导致:

  • rclone fork到后台运行
  • systemd认为主进程已退出
  • 服务状态显示failed或不断重启

正确做法: 使用Type=simple,去掉--daemon

5.3 启用服务

sudo systemctl daemon-reload
sudo systemctl enable rclone-mount.service
sudo systemctl start rclone-mount.service

5.4 服务管理

# 查看状态
sudo systemctl status rclone-mount.service

# 查看日志
sudo journalctl -u rclone-mount.service -f

# 重启服务
sudo systemctl restart rclone-mount.service

# 停止服务
sudo systemctl stop rclone-mount.service

5.5 启动失败排查

场景: 服务启动报错,但手动挂载正常

可能原因:

  1. 手动挂载未卸载,导致冲突
  2. –daemon参数与systemd冲突
  3. 网络未就绪(应使用network-online.target)

排查步骤:

# 1. 检查是否已有挂载
mount | grep GoogleDrive

# 2. 如有,先卸载
fusermount -u ~/GoogleDrive

# 3. 重新启动服务
sudo systemctl restart rclone-mount.service

六、常见问题速查

错误信息原因解决方案
403 ForbiddenToken过期或API限制重新运行rclone config reconnect
empty token found未完成OAuth授权重新配置rclone,完成授权流程
transport endpoint not connected挂载断开重新执行mount命令
fusermount: entry not found目录未挂载或已卸载检查mount状态
daemon exited with status 1–daemon与systemd冲突移除–daemon参数
couldn't find section in configrclone配置名称错误检查rclone listremotes

七、性能优化

7.1 缓存模式选择

模式适用场景
off不缓存,直接读写(网络要求高)
minimal最小缓存,顺序读写
writes写入缓存,适合文档编辑(推荐)
full完整缓存,适合大文件

7.2 常用优化参数

rclone mount gdrive: ~/GoogleDrive \
  --vfs-cache-mode writes \
  --vfs-cache-max-size 1G \
  --dir-cache-time 5m \
  --bwlimit 10M

参数说明:

  • --vfs-cache-max-size: 本地缓存上限
  • --dir-cache-time: 目录缓存时间
  • --bwlimit: 带宽限制

八、总结

8.1 关键步骤回顾

  1. 安装rclone和fuse
  2. 配置rclone(使用SSH端口转发完成无浏览器授权)
  3. 验证配置(确保token有效)
  4. 手动挂载测试
  5. 配置systemd自动挂载

8.2 核心注意事项

  • 系统环境:Debian 13/12
  • Google Drive storage编号为18
  • 无浏览器时需使用ssh -L端口转发授权
  • 必须获取有效的refresh_token
  • systemd服务不应使用–daemon参数
  • –allow-other需要fuse系统配置支持

8.3 适用场景

  • 服务器数据备份到云端
  • 多台服务器共享云端文件
  • 大容量存储扩展(云端空间+本地缓存)
  • 自动化脚本访问云端数据

参考文档: