标签 OneDrive 下的文章

本教程适用于将 Microsoft 365 E5 开发者订阅(MSDN)的 5TB OneDrive 空间挂载为 Linux 本地磁盘。当然也适用其他版本的空间挂载,但是需要自己根据实际情况做出修改。

请输入图片描述

一、 基础准备在开始之前,请确保你拥有 MSDN 管理员账号,并在 Linux 服务器上安装 fuse3(挂载必需依赖)。

# 1. 安装 Rclone
curl https://rclone.org/install.sh | sudo bash
# 2. 安装 FUSE 依赖
# Ubuntu / Debian:
apt update && apt install -y fuse3
# CentOS / AlmaLinux:
yum install -y fuse3
# 3. 修正 FUSE 权限配置(允许非 root 用户访问)
sed -i 's/#user_allow_other/user_allow_other/g' /etc/fuse.conf

二、 Rclone 配置步骤

执行 rclone config
按照以下逻辑操作:n) New remote: 输入名称 onedrive
Storage Type: 输入 42 (Microsoft OneDrive)
client_id/client_secret: 直接回车跳过
region: 输入 1 (global)
Edit advanced config: 输入 n
Use auto config: 输入 n (因为是远程服务器)获取
Token:在你的本地电脑

  • (Windows/Mac)下载 Rclone。
  • 在本地终端运行:rclone authorize "onedrive"
  • 浏览器弹出授权,登录 MSDN 账号并确认。
  • 将本地终端生成的 {"access_token":...} 整个 JSON 字符串复制。回到服务器粘贴。

config_type: 输入 1 (OneDrive Personal or Business)
Drive Selection: 搜索到 5TB 盘后,输入序号 0。
Confirm: 输入 y 确认,最后输入 q 退出。

三、 手动挂载测试创建挂载点并尝试手动挂载,确认没有报错:

# 创建挂载点
mkdir -p /mnt/onedrive

# 手动挂载命令(测试用)
rclone mount onedrive:/ /mnt/onedrive \
  --vfs-cache-mode writes \
  --vfs-cache-max-size 50G \
  --allow-other \
  --header "Referer:https://onedrive.live.com/" \
  --vfs-read-chunk-size 16M \
  --vfs-read-chunk-size-limit 1G \
  --buffer-size 64M \
  --daemon

使用 df -h 查看是否出现了 5TB 的磁盘。

四、 配置开机自动挂载 (Systemd)为了保证服务器重启后自动挂载,建议配置系统服务。

1. 创建服务文件

nano /etc/systemd/system/rclone.service

2. 粘贴以下内容(请确保 /usr/bin/rclone 路径正确,可用 which rclone 查看)

[Unit]
Description=Rclone OneDrive Mount
After=network-online.target

[Service]
Type=simple
# 核心挂载命令
ExecStart=/usr/bin/rclone mount onedrive:/ /mnt/onedrive \
    --vfs-cache-mode writes \
    --vfs-cache-max-size 50G \
    --vfs-cache-max-age 24h \
    --allow-other \
    --header "Referer:https://onedrive.live.com/" \
    --vfs-read-chunk-size 16M \
    --vfs-read-chunk-size-limit 1G \
    --buffer-size 64M \
    --dir-cache-time 5m \
    --low-level-retries 10
# 停止时卸载
ExecStop=/bin/fusermount3 -qzu /mnt/onedrive
Restart=on-failure
RestartSec=10
User=root

[Install]
WantedBy=multi-user.target

3. 启用服务

//先卸载之前手动挂载的路径
fusermount3 -qzu /mnt/onedrive
//启用并启动服务
systemctl daemon-reload
systemctl enable rclone
systemctl start rclone
//检查状态
systemctl status rclone

五、 常用维护指令汇总

目的                        命令

验证 5TB 容量 rclone about onedrive:
查看实时挂载日志 journalctl -u rclone -f
重启挂载服务 systemctl restart rclone
强制卸载挂载点 fusermount3 -qzu /mnt/onedrive
清空 OneDrive 回收站 建议去网页端操作,可释放 Trashed 占用的配额

在本文中,我将分享如何在 Debian Linux 服务器上通过命令行方式配置 OneDrive 同步工具,实现:

  • 同步 OneDrive 上某个文件夹
  • 自动下载(不上传)
  • 实时监控同步
  • 后台运行
  • 随开机自动启动

适用于无桌面的服务器环境,例如通过 SSH 登录的 VPS。


✅ 安装 OneDrive 同步客户端(abraunegg 版)

首先安装依赖并从源码编译:

sudo apt update
sudo apt install -y ldc pkg-config git curl build-essential libcurl4-openssl-dev libsqlite3-dev libdbus-1-dev

git clone https://github.com/abraunegg/onedrive.git
cd onedrive
./configure
make
sudo make install

✅ 授权 OneDrive 登录

首次运行会提示你复制一个授权链接:

onedrive

复制链接到本地浏览器访问,登录并授权后,将跳转到一个以 https://login.microsoftonline.com/common/oauth2/nativeclient?... 开头的地址。把这个完整链接复制回 SSH 终端,回车完成授权


✅ 设置同步目录配置(可选)

你可以指定本地同步目录和忽略某些临时文件:

编辑配置文件:

mkdir -p ~/.config/onedrive
nano ~/.config/onedrive/config

内容示例:

# 设置本地同步目录(可选)
sync_dir = "/root/ca"
# 忽略临时文件(可选)
skip_file = "~*|.~*|*.tmp|*.swp|*.partial"
# 这里根据你的cpu线程自行修改
threads = "1" 

❗️注意:不能在 config 文件中使用 single_directory 参数,它只能通过命令行传入。


✅ 只同步 OneDrive 某个文件夹

使用 --single-directory 参数:

onedrive --monitor --single-directory "qbit" --download-only
  • 只会同步 OneDrive 上的 qbit(是你同步的) 文件夹
  • 不会上传本地文件改动(只下载)

✅ 后台运行 OneDrive 实时同步

使用 nohup 方式运行:

nohup onedrive --monitor --single-directory "qbit" --download-only > /var/log/onedrive.log 2>&1 &

✅ 设置 systemd 用户服务实现开机自启

步骤 1:创建 systemd 服务文件(有两个自行选择,推荐使用第一个,第二个是经常强制关闭的时候使用会主要目的是为了重建数据库)

mkdir -p ~/.config/systemd/user
nano ~/.config/systemd/user/onedrive.service

内容如下(常用):

[Unit]
Description=OneDrive Sync Service (Single Directory)
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/onedrive --monitor --single-directory "qbit" 
Restart=always
RestartSec=10
Environment=XDG_CONFIG_HOME=/root/.config
StandardOutput=append:/root/onedrive.log
StandardError=append:/root/onedrive.log

[Install]
WantedBy=default.target

内容如下(如果经常强制终止的建议用这个):

[Unit]
Description=OneDrive Sync Service (Single Directory)
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/bin/bash -c '/usr/local/bin/onedrive --single-directory "qbit" --resync --sync || true; exec /usr/local/bin/onedrive --monitor --single-directory "New"'
Restart=always
RestartSec=10
Environment=XDG_CONFIG_HOME=/root/.config
StandardOutput=append:/root/onedrive.log
StandardError=append:/root/onedrive.log

[Install]
WantedBy=default.target
注意:请根据 which onedrive 调整 ExecStart 路径
尝试执行一次带 --resync --sync 的同步。如果成功或失败 (|| true),继续。
如果只需要下载,那就在/usr/local/bin/onedrive --monitor --single-directory "qbit"后面加上 --download-only上传同理

题外话

如果你觉得日志可能会越写越多,可以设置一个定时,让他每隔多久运行一次清理日志。
创建脚本
nano /root/clear_onedrive_log.sh

#!/bin/bash
# 清空 OneDrive 日志
truncate -s 0 /root/onedrive.log

给脚本加执行权限

chmod +x /root/clear_onedrive_log.sh

设置定时任务(每 10 天执行一次)
编辑 crontab:

crontab -e

添加一行(每 10 天的 0 点执行一次):

0 0 */10 * * /root/clear_onedrive_log.sh

解释:
0 0 /10 * → 每个月的第 1、11、21…天 0:00 执行
脚本执行后会清空 /root/onedrive.log

步骤 2:启用并启动服务

loginctl enable-linger $(whoami)
systemctl --user daemon-reexec
systemctl --user daemon-reload
systemctl --user enable onedrive.service
systemctl --user start onedrive.service

检查运行状态:

systemctl --user status onedrive.service

方便网页查看同步进度,运行一下这个命令

bash -c "$(curl -fsSL https://sh.digac.icu/shell/onedrivelog.sh)"

✅ 升级 cURL(可选)

如果出现如下警告:

WARNING: Your cURL/libcurl version (7.88.1) has known HTTP/2 bugs ...
echo "deb http://deb.debian.org/debian bookworm-backports main" \
  | sudo tee /etc/apt/sources.list.d/backports.list
apt-get update
apt-get -t bookworm-backports install curl

说明你的系统使用了存在兼容性问题的旧版 curl,但客户端已自动回退使用 HTTP/1.1。为最佳体验,你可以手动升级 curl 到 7.92+,或者忽略该提示继续使用。