2 min read

Mounting OneDrive on Linux with Rclone

Mounting OneDrive on Linux  with Rclone

Introduction

If you use OneDrive for cloud storage and want to integrate it seamlessly into your Linux Mint system, Rclone is the perfect tool. It allows you to mount OneDrive as a filesystem, enabling easy access to your files as if they were stored locally. This guide covers setting up Rclone, mounting OneDrive, and automating the process using a cron job.

Installing Rclone

First, ensure that Rclone is installed on your system. If you haven’t installed it yet, run the following command:

sudo apt update && sudo apt install rclone

Alternatively, you can install the latest version directly from Rclone’s official site:

curl https://rclone.org/install.sh | sudo bash

Configuring OneDrive

Once Rclone is installed, you need to configure your OneDrive account.

  1. Follow the interactive prompts:
    • Choose n for a new remote.
    • Enter a name for the remote (e.g., onedrive).
    • Select Microsoft OneDrive from the list.
    • Follow the authentication process to link your OneDrive account.
    • Accept the default settings for other prompts unless you have specific needs.
    • Once complete, type q to exit the configuration.

Run the following command:

rclone config

To test if the configuration was successful, list your OneDrive files:

rclone ls onedrive:

If you see your OneDrive files listed, your configuration is successful.

Mounting OneDrive Manually

Before mounting, make sure you have created the onedrive directory in your $HOME folder:

mkdir -p $HOME/onedrive

To manually mount your OneDrive folder to $HOME/onedrive, use the following command:

rclone mount onedrive: $HOME/onedrive --vfs-cache-mode writes &

The --vfs-cache-mode writes flag ensures better compatibility when writing files.

To unmount the drive, use:

fusermount -u $HOME/onedrive

or

umount $HOME/onedrive

Automating the Mount with Cron

To ensure that your OneDrive is mounted automatically on reboot, we will use a cron job.

Add the following line at the end:

@reboot sleep 20 && rclone mount onedrive: $HOME/onedrive --vfs-cache-mode writes

Open the crontab editor:

crontab -e

This ensures that the system waits for 20 seconds after reboot before mounting OneDrive, allowing all necessary services to start properly.

Appendix: Automation Script

To simplify the setup, you can use the following bash script. It installs Rclone, creates the necessary folder, and adds the cron job automatically.

Script: setup_onedrive.sh

#!/bin/bash

echo "Updating package list and installing rclone..."
sudo apt update && sudo apt install -y rclone

echo "Ensuring the onedrive directory exists..."
mkdir -p "$HOME/onedrive"

echo "Adding mount command to crontab..."
(crontab -l 2>/dev/null; echo "@reboot sleep 20 && rclone mount onedrive: $HOME/onedrive --vfs-cache-mode writes") | crontab -

echo "\n\033[1;32mSetup complete!\033[0m"
echo "\033[1;33mNext step:\033[0m Run \033[1;34mrclone config\033[0m to link your OneDrive account."
echo "After setup, use \033[1;34mrclone ls onedrive:\033[0m to verify the connection."

Running the Script

  1. Paste the script and save the file (CTRL+X, Y, Enter).

Run the script:

./setup_onedrive.sh

Make it executable:

chmod +x setup_onedrive.sh

Copy the script into a file:

nano setup_onedrive.sh

After running the script, follow the instructions it provides to complete the OneDrive setup.