Skip to content

Instantly share code, notes, and snippets.

@kabili207
Last active June 6, 2024 08:32
Show Gist options
  • Save kabili207/2cd2d637e5c7617411a666d8d7e97101 to your computer and use it in GitHub Desktop.
Save kabili207/2cd2d637e5c7617411a666d8d7e97101 to your computer and use it in GitHub Desktop.
Rclone systemd user service

rclone systemd service

Preparation

This service will use the same remote name you specified when using rclone config create. If you haven't done that yet, do so now.

Next, create the mountpoint for your remote. The service uses the location ~/mnt/<remote> by default.

mkdir ~/mnt/dropbox

The --allow-other option is required in order to work in many desktop environments. This flag must be enabled by adding user_allow_other to /etc/fuse.conf. If you aren't using a desktop environment, such as on a server, this option can be omitted.

Adding the service

Save the rclone@.service file in ~/.config/systemd/user/ Make sure you include the @. This is required to work.

As your normal user, run:

systemctl --user daemon-reload

Using the service

You can now start/enable each remote by using rclone@<remote>

systemctl --user enable --now rclone@dropbox
# User service for Rclone mounting
#
# Place in ~/.config/systemd/user/
# File must include the '@' (ex rclone@.service)
# As your normal user, run
# systemctl --user daemon-reload
# You can now start/enable each remote by using rclone@<remote>
# systemctl --user enable --now rclone@dropbox
[Unit]
Description=rclone: Remote FUSE filesystem for cloud storage config %i
Documentation=man:rclone(1)
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
ExecStartPre=-/usr/bin/mkdir -p %h/mnt/%i
ExecStart= \
/usr/bin/rclone mount \
--config=%h/.config/rclone/rclone.conf \
--vfs-cache-mode writes \
--vfs-cache-max-size 100M \
--log-level INFO \
--log-file /tmp/rclone-%i.log \
--umask 022 \
--allow-other \
%i: %h/mnt/%i
ExecStop=/bin/fusermount -u %h/mnt/%i
[Install]
WantedBy=default.target
@lilws
Copy link

lilws commented Feb 27, 2022

Hi, I came from googling around and still can't find out how to run multiple rclone shell script in bash as systemd. I can run the service when calling systemd. But it won't run at boot.

@stuie1952
Copy link

You need to enable the service for it to start at boot.

Just replace start with enable in systemctl

@xqft
Copy link

xqft commented Mar 1, 2022

I get mount helper error: fusermount: mount failed: Operation not permitted :/

@stuie1952
Copy link

Did you 'modprobe fuse '

@stuie1952
Copy link

The --allow-other option is required in order to work in many desktop environments. This flag must be enabled by adding user_allow_other to /etc/fuse.conf.

@xqft
Copy link

xqft commented Mar 2, 2022

Yep I did both

@xqft
Copy link

xqft commented Mar 7, 2022

Removing MemoryDenyWriteExecute=true solved my problem.

@kabili207
Copy link
Author

Removing MemoryDenyWriteExecute=true solved my problem.

I've removed this since it seems many people are having this issue

@stuie1952
Copy link

stuie1952 commented Mar 7, 2022

here is my script. it works for me.
cheers

[Unit]
Description=rclone: Remote FUSE filesystem for cloud storage config %i
Documentation=man:rclone(1)
After=network-online.target
Wants=network-online.target
AssertPathIsDirectory=%h/cloud/

[Service]
Type=notify
ExecStartPre=/bin/mkdir -p %h/cloud/%i
ExecStart= \
  /usr/bin/rclone mount \
    --config=%h/.config/rclone/rclone.conf \
    --allow-non-empty \
    --vfs-cache-mode writes \
    --vfs-cache-max-size 100M \
    --dir-cache-time 72h \
    --vfs-read-chunk-size-limit 128M \
    --vfs-read-chunk-size-limit off \
    --log-level DEBUG \
    --log-file /tmp/rclone-%i.log \
    --umask 022 \
    --allow-other \
    %i: %h/cloud/%i
ExecStop=/bin/fusermount -uz %h/cloud/%i
ExecStop=/bin/rmdir %h/cloud/%i

[Install]
WantedBy=default.target

@lilws
Copy link

lilws commented Mar 7, 2022

Thanks, since last time too busy at work so I can't return here. On that date, I moved the service to different install target and it works. I do this on xpenology DSM 7.0.1. And the install location is multi-user.target, I've just moved it into syno-high-priority... and it works. But then I must figured how to run this service after synology mounted all HDDs, if not, rclone will complain it can't find the location to mount.

@ioogithub
Copy link

next step is to get the fuse filesystem to work with dolphin. i get the feeling this is not going to be simple ...

Have you found anything at all that can help with this? Dolphin treats any rclone mounts as a local filesystem which makes dolphin unusable with dolphin. I have been searching for any solution for a few years. If you search there are other users who have opened bug reports and feature requests but nothing positive have come of it. Apparently the issue is with the KIO subsystem which dolphin uses so there isn't really anything the use can do to improve the situation.

@shyamjos
Copy link

shyamjos commented Nov 24, 2022

Worked like a charm , Awesome work

@5l2
Copy link

5l2 commented Feb 21, 2023

You may need the following command:

loginctl enable-linger $USER

If you encounter the service ceasing when you exit the last session of ssh or not starting during boot.

@mezhgano
Copy link

mezhgano commented Mar 8, 2023

Thanks for such a handy stuff
Instead of typing 2 separate commands to run service, you can just:
systemctl --user enable --now rclone@dropbox

Also, if you setting up this on VPS or any kind of server without desktop environment, you probably want to omit option --allow-other

@rodhfr
Copy link

rodhfr commented Apr 4, 2023

This is perfect! even me a complete unversed user could make this run properly without much hassle. 😁
Before this I've spend hours trying to run everything as root with systemd when simply could run everything as normal user. Guess I need to work some knowledge on those Linux permissions, users and hierarchy. Thank you!

@crowjdh
Copy link

crowjdh commented Apr 23, 2023

Also a great learning material, thanks.

@MatteoR23
Copy link

Instead of AssertPathIsDirectory=%h/mnt/%i I would add ExecStartPre=-/usr/bin/mkdir -p %h/mnt/%i
You will avoid to manually create folders ;-)
This is my file version:

# User service for Rclone mounting
#
# Place in ~/.config/systemd/user/
# File must include the '@' (ex rclone@.service)
# As your normal user, run 
#   systemctl --user daemon-reload
# You can now start/enable each remote by using rclone@<remote>
#   systemctl --user enable --now rclone@dropbox

[Unit]
Description=rclone: Remote FUSE filesystem for cloud storage config %i
Documentation=man:rclone(1)
After=network-online.target
Wants=network-online.target 

[Service]
Type=notify
ExecStartPre=-/usr/bin/mkdir -p %h/mnt/%i
ExecStart= \
  /usr/bin/rclone mount \
    --config=%h/.config/rclone/rclone.conf \
    --vfs-cache-mode writes \
    --vfs-cache-max-size 100M \
    --log-level INFO \
    --log-file /tmp/rclone-%i.log \
    --umask 022 \
    --allow-other \
    %i: %h/mnt/%i
ExecStop=/bin/fusermount -u %h/mnt/%i

[Install]
WantedBy=default.target

@samirfor
Copy link

Here I had to give a few more commands to get it to run smoothly.

mkdir -p ~/.config/systemd/user/

vim ~/.config/systemd/user/rclone@.service
# PASTE SERVICE FILE AND SAVE

loginctl enable-linger $USER
export XDG_RUNTIME_DIR=/run/user/$(id -u)
systemctl --user daemon-reload
# rclone's remote called gd in ~/.config/rclone/rclone.conf
systemctl --user enable --now rclone@gd.service

# show the files
ls -lah ~/mnt/gd

@rileyrg
Copy link

rileyrg commented Nov 4, 2023

Very late but may I ask why you're even mounting and why use a systemd config to do it? Wouldn't a simple script run at login achieved the same thing? You're not running things off the mount are you? I ask because I'm setting up rclone now to do bisync and I have no use for the mount so I'm wondering what use case created this utility?

@kifujin
Copy link

kifujin commented Nov 4, 2023 via email

@kabili207
Copy link
Author

There are a few reasons I went with a mount using a systems service:

  1. I access several cloud and network storages very frequently
  2. Mounting them allows native integration with any program I use
  3. One of these services has half a terabyte of storage. I have absolutely ZERO desire to synchronize that to my computer.
    a. This is cloud storage anyway. The entire point, in my opinion, is to keep the data in question OFF my machine in the first place.
  4. There are half a dozen different ways this could have been scripted. Going with a systemd service made the most sense at the time (and still does for my purposes)

@hertg
Copy link

hertg commented Nov 25, 2023

This is not necessarily an issue with this exact systemd unit, but I just want to leave this hint here for anybody that makes the same stupid mistake as me: If you use rclone and this systemd unit to mount an SMB network share and your share is unavailable (or misconfigured), this may cause a huge delay whenever you're opening your file manager (or when any software opens a "Save as" prompt).

Was using the following settings

[nas]
type = smb
host = 192.168.20.10
pass = <redacted>

And mounted it with this systemd unit.

systemctl --user enable --now rclone@nas.service

Then I forgot about it, turned my NAS off two months later, and was dumbfounded for a week why my file manager takes 30-60 seconds to open everytime. 🤦

@icservis
Copy link

Work nicely form me for first run, but after restart, I get error nonempty directory:
024/01/15 21:35:36 mount helper error: fusermount: mountpoint is not empty
2024/01/15 21:35:36 mount helper error: fusermount: if you are sure this is safe, use the 'nonempty' mount option
2024/01/15 21:35:36 Fatal error: failed to mount FUSE fs: fusermount: exit status 1

Trying to use option nonempty, but get:
2024/01/15 21:36:50 ERROR : -o/--option not supported with this FUSE backend

Running Proxmox 8. Help appreciated. Thanks a lot

@yanisallouch
Copy link

yanisallouch commented Jan 22, 2024

Sure! Here is a better version of the text:

Hello, I'm having some trouble with the drive mounting script. When I mount the folders, they appear empty in Nautilus. I'm using two Google Drive accounts, A and B, each with two namespaces: personal and shared. The file structure is as follows:

~/drive/{account}/{namespace}

This works just fine using Rclone Browser. However, when I use the script provided above, I can mount the folders to /mnt/drive/%i, but they are empty.

@kabili207
Copy link
Author

@yanisallouch Did you make the change to /etc/fuse.conf mentioned in the preparation section? It is required for many desktop environments and will result in the "empty folder" issue you're seeing if you skip it.

@yanisallouch
Copy link

@kabili207 Yes :

# The file /etc/fuse.conf allows for the following parameters:
#
# user_allow_other - Using the allow_other mount option works fine as root, in
# order to have it work as user you need user_allow_other in /etc/fuse.conf as
# well. (This option allows users to use the allow_other option.) You need
# allow_other if you want users other than the owner to access a mounted fuse.
# This option must appear on a line by itself. There is no value, just the
# presence of the option.

user_allow_other


# mount_max = n - this option sets the maximum number of mounts.
# Currently (2014) it must be typed exactly as shown
# (with a single space before and after the equals sign).

#mount_max = 1000

@nilreml
Copy link

nilreml commented Mar 15, 2024

Thanks @kabili207 and everyone else who contributed here! Very useful indeed.

I created a small repo out of all this - if you have any objections, please let me know and I'll take it down or modify it accordingly.

@jibarozzo
Copy link

Hi, my service stopped working all of a sudden. It would fail to start after reboot and reloading daemon. I reinstalled fuse3 and Plasma 6 , and removed the --allow_users flag. After this the service started fine. I realized that something might have broken with updating this week(Arch on LTS kernel). When I checked the /etc/fuse.conf file user_allow_other was commented. This was un-commented before. I also tried it with the --allow_users flag again and user_allow_other un-commented and it worked as before. Just an FYI.

@GavinKCLS
Copy link

Just wanted to say thanks for this - works great for me

@nilreml
Copy link

nilreml commented May 5, 2024

Hi, my service stopped working all of a sudden. It would fail to start after reboot and reloading daemon. I reinstalled fuse3 and Plasma 6 , and removed the --allow_users flag. After this the service started fine. I realized that something might have broken with updating this week(Arch on LTS kernel). When I checked the /etc/fuse.conf file user_allow_other was commented. This was un-commented before. I also tried it with the --allow_users flag again and user_allow_other un-commented and it worked as before. Just an FYI.

Good point, thanks. /etc/fuse.conf gets overwritten on upgrading whenever it's content in the package changes, and multiple config file loading like /etc/apt/sources.list.d/* isn't yet supported AFAIK.

If you don't need to access the mount with users other than your own, it's actually safer and easier to just remove --allow-other from rclone mount options, which doesn't require setting user_allow_other.

Tested on Debian 12.5, rclone 1.66

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment