My blog

Mounting OneDrive automatically on Linux

I recently moved to a Linux desktop and wanted to have some OneDrive storage automatically mounted - I thought it worth documenting my approach.

Note: I would actually recommend basically any other cloud service over a Microsoft product, but it does the job, and I get it for free from my employer.

I accomplished this with:

rclone remote setup

rclone has a interactive setup tool that you run from the command line, which can be started with the command:

 rclone config

This will guide you through linking rclone to your cloud storage account (referred to as "remotes"). All default settings are fine for most purposes.

Mounting an rclone remote

The format for rclone's mount command is:

# All text in capitals is a placeholder.

rclone mount REMOTENAME:/FOLDER/FOLDER_TO_MOUNT /MOUNT_DESTINATION --OPTION1 --OPTION2 --OPTIO...

My command looked like this, as I just wanted to mount the entire onedrive to a folder in my home directory:

sudo rclone mount onedrive:/ /home/USERNAME/onedrive --vfs-cache-mode Full --vfs-refresh

Those two trailing options are flags I've seen recommended for use with OneDrive - feel free to look a bit more deeply into this, but these settings are working well for me.

You can use this command to test everything is working correctly. Now, we want to ensure this is run automatically instead of doing it manually every boot.

Mounting OneDrive automatically on boot

We must create a systemd service file that will run our command. Using a text editor of your choice (I am fond of Micro, but any will do) create the following file:

/etc/systemd/system/rclone-onedrive.service

The contents of which should look like:

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

[Service]
Type=notify
ExecStart=/usr/bin/rclone mount onedrive:/ /home/USERNAME/onedrive \
  --vfs-cache-mode full \
  --vfs-refresh 
ExecStop=/bin/fusermount -u /home/USERNAME/onedrive
Restart=on-failure
User=USERNAME
Group=USERNAME

[Install]
WantedBy=default.target

Then we need to enable the service:

sudo systemctl enable rclone-onedrive.service

You should now to be able to reboot, and hopefully find your cloud drive mounted!

If it's not there, it's possible an error has occurred. You can try the command:

systemctl status reclone-onedrive.service

And see if the service is active, and if there's any errors reported.

Good luck!