未分类 · 2025年6月13日 0

Linux安装Rclone并开机自动挂载

安装Rclone

发行版使用自家的包管理命令一般都可以方便的安装。

apt install rclone fuse

fuse是挂载时需要。

之后使用rclone config添加网盘之类的。按提示一步步来问题不大,实在不会就百度一下吧。

挂载到目录,命令为 rclone mount “网盘名称:路径” “本地路径”

rclone mount OD:/ /opt/movie --allow-other --attr-timeout 5m --vfs-cache-mode full --vfs-cache-max-age 3h --vfs-cache-max-size 25G --vfs-read-chunk-size-limit 100M --buffer-size 256M --daemon

当然除了基础的挂载。还有一堆乱七八糟的参数可以加。具体可以参考官方文档,按需添加。

取消挂载

# 强制取消挂载
/usr/bin/fusermount -u /opt/movie
# 安全取消挂载 (会等待现有任务完成)
/usr/bin/fusermount -qzu /opt/movie

要日常使用,自动挂载自然少不了。

方法一:通过脚本自动打开screen,并执行挂载

我自己早期使用的方式,主要是当时用的那个LINUX不支持systemd。所以写个脚本插入到init.rc中执行。当时用的还是screen方式。相当于开个窗口在前台运行。

方法二:配置systemd文件

适合单一的挂载命令。

· 创建rclone-mount.service文件。

nano /etc/systemd/system/rclone-mount.service

· 然后输入以下内容(根据个人情况调整)。

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

[Service]
Type=forking
ExecStart=/usr/bin/rclone mount OD:/ /opt/movie --allow-other --attr-timeout 5m --vfs-cache-mode full --vfs-cache-max-age 3h --vfs-cache-max-size 25G --vfs-read-chunk-size-limit 100M --buffer-size 256M --daemon
ExecStop=/bin/fusermount -u /opt/movie
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target

· 保存后,重新加载一下systemd。然后设置开机自启动。

systemctl daemon-reload
systemctl enable rclone-mount.service

不想那么快重启,也可以先手动启动。

systemctl start rclone-mount.service

这个方式每次修改完rclone-mount.service都要重载一下。就是systemctl daemon-reload。

方式三:将挂载命令写入脚本,再配置systemd

如果有好几个路径要挂载,那就只好写到脚本里再挂载了。

· 创建脚本文件,位置和名字随意

nano /root/rclone_mount.sh

· 在脚本中写入你的挂载命令:

#!/bin/sh
rclone mount OD:/ /opt/movie --allow-other --attr-timeout 5m --vfs-cache-mode full --vfs-cache-max-age 3h --vfs-cache-max-size 25G --vfs-read-chunk-size-limit 100M --buffer-size 256M --daemon

· 给脚本添加执行权限

chmod +x /root/rclone_mount.sh

· 编辑rclone-mount.service文件

nano /etc/systemd/system/rclone-mount.service

· 填入以下内容:

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

[Service]
Type=forking
ExecStart=/root/rclone_mount.sh
ExecStop=/bin/fusermount -u /opt/movie
Restart=on-failure

[Install]
WantedBy=multi-user.target

· 保存后,重新加载一下systemd。然后设置开机自启动。

systemctl daemon-reload
systemctl enable rclone-mount.service

不想那么快重启,可以先手动启动。

systemctl start rclone-mount.service

日后如果有变更只需要编辑脚本就可以了。不用修改服务。


systmd service文件常见的存放位置

  1. /etc/systemd/system/(优先级最高)
  2. /run/systemd/system/:runtime systemd unit
  3. /lib/systemd/system :安装应用自带的service存储在这里(优先级最低)