How to mount OneDrive on GNU Linux with rclone.

How to mount OneDrive on GNU Linux with rclone

Miguel Menéndez

OneDrive, Microsoft’s storage service, unlike other “cloud” storage options like Mega or Internxt, does not have a native desktop client for GNU Linux.

Remember that in any case, you can always use a web browser to interact with your content on OneDrive from GNU Linux.

If you’re not too afraid of the terminal emulator, then I’m going to use the rclone program to mount an MS OneDrive drive to a GNU Linux directory and use it transparently.

rclone

Rclone is a program that will allow you to mount and synchronize a local GNU Linux directory with numerous remote storage services (Google Drive, Dropbox, Nextcloud, Amazon Web Services S3 and 40 other services).

With this utility, you can perform remote backups, recover files from “the cloud”, replicate data, migrate data between services, or even use multiple remote storage services as a local drive.

Remember to check the limitations of OneDrive in terms of traffic (requests/time, upload and download speed…) and maximum file size (250GB at the time of writing this) because it may not suit your needs and you may have to resort to other services such as Azure or AWS S3.

How to mount a Microsoft OneDrive drive on GNU Linux with rclone

Using rclone is not as complicated as it seems. It does require patience and a certain ease with the terminal emulator.

1. Install rclone

On Debian and derivatives:

$ sudo apt install rclone

On Arch and derivatives:

$ sudo pacman -S rclone

2. Add the remote service

$ rclone config

If this is your first time running rclone, you must add a new remote service. Type n to add a new remote service:

$ rclone config
NOTICE: Config file "/home/miguel/.config/rclone/rclone.conf" not found - using defaults
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
name>

Now enter the name of the new service. You can choose any name that is descriptive of the service. In this example, let’s use “OneDrive”:

$ rclone config
NOTICE: Config file "/home/miguel/.config/rclone/rclone.conf" not found - using defaults
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
name> OneDrive

After giving the new service a name, you will see a numbered list with all services supported by rclone (Google Drive, Box…):

[...]
21 / Microsoft OneDrive
   \ "onedrive"
22 / OpenDrive
   \ "opendrive"
[...]
Sotorage>

You must enter the number of the service you want to use. In this example, Microsoft OneDrive is numbered 21:

[...]
21 / Microsoft OneDrive
   \ "onedrive"
22 / OpenDrive
   \ "opendrive"
[...]
Sotorage> 21

Leave client_id and client_secret blank. Reject the advanced configuration and tell it that you do want to use autoconfiguration:

Microsoft App Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id>
Microsoft App Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret>
Edit advanced config? (y/n)
y) Yes
n) No
y/n> n
Remote config
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine
y) Yes
n) No
y/n>

Chen you press Y and Enter, your web browser will open for you to sign in with your Microsoft account, asking if you want to grant access, read and write permissions to rclone , tell him yes:

Microsoft Login.

Now you must select the type of account. In this example, it’s a personal or business OneDrive account:

Choose a number from below, or type in an existing value
 1 / OneDrive Personal or Business
   \ "onedrive"
 2 / Root Sharepoint site
   \ "sharepoint"
 3 / Type in driveID
   \ "driveid"
 4 / Type in SiteID
   \ "siteid"
 5 / Search a Sharepoint site
   \ "search"
Your choice> 1
Found 1 drives, please select the one you want to use:
0:  (personal) id=a98x4l1p985q0083
Choose drive to use:>

If you have multiple OneDrive drives assigned to your Microsoft account, they will be numbered. In this example, there is only one personal unit, 0:

Found 1 drives, please select the one you want to use:
0:  (personal) id=a98x4l1p985q0083
Choose drive to use:> 0
Found drive 'root' of type 'personal', URL: https://onedrive.live.com/?cid=a98x4l1p985q0083
Is that okay?
y) Yes
n) No
y/n> y

To finalize this rclone configuration, you must save it:

[OneDrive]
token = {"access_token":":DLKjf39ruKDJL29uldskdjf10ewlfkJLKEJLKSHDE[...]"}
drive_id = a98x4l1p985q0083
drive_type = personal
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y

And exit with q from the rclone setup wizard:

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> q

$

3. Mount OneDrive to a local directory

Create a folder in your home directory:

$ mkdir ~/OneDrive

And mount the OneDrive drive to that directory:

$ rclone --vfs-cache-mode writes mount "OneDrive": ~/OneDrive

In the line above, “OneDrive” is the name we gave the remote service in step 2.

If you open a file explorer, you’ll see a new drive named OneDrive appear (in /home/miguel/OneDrive/) with the contents of your OneDrive “cloud” storage space.

If you end the above command (with Ctrl + C) or close the terminal window the drive will be unmounted.

4. Mount OneDrive on boot with Systemd

Create a new systemd service:

$ sudo nano /etc/systemd/system/rclonemount.service

With the following content:

[Unit]
Description=rclonemount
AssertPathIsDirectory=/mnt/onedrive
After=network-online.target

[Service]
Type=simple
ExecStart=/usr/bin/rclone mount --config=/home/miguel/.config/rclone/rclone.conf --vfs-cache-mode writes OneDrive: /home/miguel/OneDrive
ExecStop=/bin/fusermount -u /home/miguel/OneDrive
Restart=always
RestartSec=10

[Install]
WantedBy=default.target

If you have permission problems, you can add --uid and --gid to the start command:

ExecStart=/usr/bin/rclone mount --config=/home/miguel/.config/rclone/rclone.conf --vfs-cache-mode writes OneDrive: /home/miguel/OneDrive --uid=miguel --gid=miguel

Start the new service with systemctl start rclonemount. If there are no errors and you want it to mount on every system boot: systemctl enable rclonemount.

Sources: Microsoft OneDrive (rclone.org) y rclone mount options .

Comments

Found a bug? Do you think something could be improved? Feel free to let me know and I will be happy to take a look.