August 13, 2020

How to mount your GDrive on Ubuntu using rclone

How to mount your GDrive on Ubuntu using rclone

UPDATE: For anyone having trouble with mounting since recent updates to Ubuntu 20.04 you may need to add "--allow-non-empty \" to your service file.
I've updated the original service file below to reflect this.

Firstly, let's get rclone installed.

curl | sudo bash

Next we need to create a place to mount our gdrive and set our permissions.
We're giving ourselves full control and allowing others to read and execute.

sudo mkdir /media/gdrive

sudo chmod 755 /media/gdrive

Next we need to create 0Auth2 client id credentials for rclone. So go to this link below and create a new project on the Google Cloud Platform if you don't already have one.

Google Cloud Platform
Google Cloud Platform lets you build, deploy and scale applications, websites and services on the same infrastructure as Google.

Now we can create some credentials.

So hit 'Create Credentials'
Choose '0AuthClientID'
Application type is 'other'.

Copy your client id to a text editor
Copy your client secret to your text editor as well.
I found this was needed to strip random spaces from the id and secret that prevented them from working, so don't skip this bit.

Armed with this we can configure our mount in rclone now

rclone config
Choose 'n' for new remote
Name is anything you like - I suggest 'gdrive' here
Type: drive or choose number corresponding to google drive
Scope: 1 (full access)
Accept defaults by pressing enter on 'root folder id' and 'service account file'
Say no to auto-config as this won't work on a remote/headless machine.
Copy the link given and log in, then allow permissions for rclone.
Auth with the code provided
Is it a Team Drive - say no if unsure
Is the remote ok? Say Yes.

Now lets check our mount works:

rclone mount gdrive: /media/gdrive --allow-other --vfs-cache-mode writes

If this is working correctly you will not get any errors, and you won't get a prompt back until you exit it with Ctrl+C.

Now we need to automate the mounting on boot. So we'll create a systemd service based on the official wiki guide.

sudo nano /etc/systemd/system/rclone.service

Then paste in the contents below: (remember to place 'adam' with your user)

# /etc/systemd/system/rclone.service
Description=Google Drive (rclone)

ExecStart=/usr/bin/rclone mount \
        --config=/home/adam/.config/rclone/rclone.conf \
        --allow-other \
        --fast-access \
        --cache-tmp-upload-path=/tmp/rclone/upload \
        --cache-chunk-path=/tmp/rclone/chunks \
        --cache-workers=4 \
        --cache-writes \
        --cache-dir=/tmp/rclone/vfs \
        --cache-db-path=/tmp/rclone/db \
        --no-modtime \
        --drive-use-trash \
        --stats=0 \
        --checkers=8 \
        --dir-cache-time=60m \
        --allow-non-empty \
        --cache-info-age=60m gdrive:/ /media/gdrive
ExecStop=/bin/fusermount -u /media/gdrive


Run the entries below to reload the daemon, enable and start the service.

systemctl daemon-reload
sudo systemctl enable rclone.service
sudo systemctl start rclone.service

If you want to tweak the settings for your particular case, or you are having problems getting the mount to work, read up on the official docs as things will likely change from the time of writing.

Now that you have got your mount working and running on startup you can utilise your cloud storage as if it were native to your server. You could mount it in your Nextcloud, point your media server to it and stream your files, or use it as a backup destination to keep things safe! Enjoy!