Remotely accessing a OneDrive account on Linux via rclone.

Contents

Introduction

This is a guide showing how to access a Microsoft OneDrive storage system on a Linux computer. We will use the software rclone. You must install rclone now to follow this guide. Additionally, we will create persistent access to the account. Don’t do this on a public, unsecured network.

Configuration

Remote Setup

Open a terminal. Run

      remote config
    

A text-based interactive setup process will commence. Below is the output from the terminal:

      No remotes found, make a new one?
n) New remote
s) Set configuration password
q) Quit config
n/s/q>

We want to make a new remote connection (known simply as a ‘remote’), so type n. Rclone will now ask for you to name your remote:

      name> Your_Remote_Name
    

Replace Your_Remote_Name with a memorable name. It is best if this name doesn’t include special characters (e.g. $, &) nor be a common name (e.g. desktop, documents). I chose OneDrive, and where you see OneDrive in the rest of the prompts/code in this guide, it will be different if you choose a different name.\nNow we must select our type of storage.

      Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
1 / 1Fichier
\ (fichier)

56 / seafile
\ (seafile)
Storage>

Various storage options will be given. Type in the number associated with OneDrive. (For me it was 34, but this may well have changed since). Now the following will show:

      Option client_id.
OAuth Client Id.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_id>

I just pressed enter, as it is not relevant to me and likely not for you either.

      Microsoft App Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret>

Again, I just pressed enter, as it is not relevant to me and likely not for you either. A prompt to enter your region will show:

      Option region.
Choose national cloud region for OneDrive.
Choose a number from below, or type in your own string value.
Press Enter for the default (global).
1 / Microsoft Cloud Global
\ (global)

4 / Azure and Office 365 operated by Vnet Group in China
\ (cn)
region>

Enter the number associated with the region you or your employer are/is in.

      Edit advanced config? (y/n)
y) Yes
n) No
y/n> n

You will now see the above. Unless you know what you’re doing, just type n. rclone will now ask you to authenticate with Microsoft. It will use your default browser. It is best your browser can handle JavaScript and Cookies (e.g. Firefox, Chrome). If you do not have a default browser, set one now. Type y.

      Remote config
Use web browser to automatically authenticate rclone with remote?
* Say Y if the machine running rclone has a web browser you can use
* Say N if running rclone on a (remote) machine without web browser access
If not sure try Y. If Y failed, try N.
y) Yes
n) No
y/n>

The terminal will show some information similar to this:

      2024/05/12 12:29:03 NOTICE: If your browser doesn't open automatically go to ...
2024/05/12 12:29:03 NOTICE: Log in and authorize rclone for access
2024/05/12 12:29:03 NOTICE: Waiting for code...
2024/05/12 12:29:05 NOTICE: Got code

Your default browser will open/launch a new tab and authenticate with the Microsoft or related corporate account you are signed into on that browser. It should be very quick. Once it is done, the following will show in your browser. You can close the tab now.

Confirmation of authentication in the default browser.

Back to the terminal now, and now a list of options for the kind of OneDrive you use will be shown. Choose the one most relevant to you.

      Option config_type.
Type of connection
Choose a number from below, or type in an existing string value.
Press Enter for the default (onedrive).
1 / OneDrive Personal or Business
\ (onedrive)
2 / Root Sharepoint site
\ (sharepoint)
/ Sharepoint site name or URL
3 | E.g. mysite or https://contoso.sharepoint.com/sites/mysite
\ (url)
4 / Search for a Sharepoint site
\ (search)
5 / Type in driveID (advanced)
\ (driveid)
6 / Type in SiteID (advanced)
\ (siteid)
/ Sharepoint server-relative path (advanced)
7 | E.g. /teams/hr
\ (path)
config_type>

Hint: It is likely to be option 1 for personal or small businesses, 2/4 for larger corporations, or 3 for self-contained websites. After a couple of seconds, this will appear:

      Option config_driveid.
Select drive you want to use
Choose a number from below, or type in your own string value.
Press Enter for the default ([REDACTED]).
1 / OneDrive (business)
\ ([REDACTED])
config_driveid>

If you have multiple accounts signed-in on your default browser, you may see multiple options. Choose the one with its associated OneDrive you want to have integrated access to locally (you can do the same for your other accounts by following the process again once finished). Type the number to choose. Now rclone will run a check with you if the drive looks recognisable, based on the URL of the OneDrive/SharePoint site.

      Drive OK?

Found drive "root" of type "business"
URL: https://[REDATCTED]-my.sharepoint.com/personal/[REDACTED]/Documents

y) Yes (default)
n) No
y/n>

The redacted parts of the URL will contain identifying information. Once said yes, you have set up your remote!

      Configuration complete.
Options:
- type: onedrive
- token: {"access_token":"[REDACTED]","expiry":"[REDACTED]"}
- drive_id: [REDACTED]
- drive_type: business
Keep this "OneDrive" remote?
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d>

To finish, just type y. Now this will show:

      Current remotes:

Name Type
==== ====
OneDrive onedrive

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q>

Unless you want to change some things (e.g. set up a new remote) it is now safe to quit (type q).

Remote Mount

So that on startup you have instant access to your remote files, create a new empty file somewhere safe (e.g. in your home directory). Copy the following shell script to your new file:

      if ! grep " /route_to_where_you_want_to_find_the_remote " /proc/mounts
then
rclone mount OneDrive: /route_to_where_you_want_to_find_the_remote --daemon
fi

Rename your file to .OneDrive_AutoMount.sh. Edit the text as required. In a terminal, write

      sudo nano /etc/crontab

Type in your root password. Edit the crontab to be like as shown below, with <your_user_name> replaced.

Editing crontab with GNU Nano.

Ctrl+O to exit and then now when you next restart your computer, the remote OneDrive will now show as a mounted local drive when next seen in the file manager.

Caveat

For now, there is no API provided by Microsoft for rclone to use for easily deleting files on this new remote. The behaviour I have seen when I have attempted to delete a file is a wastebin folder will be created in OneDrive which will hold the deleted file; it is not deleted at all, rather it's just moved to this newly created folder.

References and Further Reading

Legal

The prompts are from rclone. Cloud background is by webtreats under CC-BY-2.0. Original text, images and code snippet from the article Remotely accessing a OneDrive account on Linux via Rclone. by unsaturatedtransistor is licensed under CC BY-SA 4.0.

CC BY & SA.