Linux: Mount OneDrive Cloud Drive with Rclone

I believe I am not the only one. To save money, many people pick the basic server configuration when choosing a server, leaving only a pitiful few tens of GB of storage, or even just 10 GB. But sometimes you really do need a larger storage space. At such times, cloud drives that provide APIs — OneDrive, Google Drive and the like — can be mounted onto the server and used as local storage, which to some extent helps relieve our storage anxiety.


Steps

1. Download and Install

A Linux system is assumed in this article, but the operations are basically the same everywhere, and other systems can follow along too.

Run the following command to download and install Rclone:

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

2. Configure Rclone

  • After installation, run:

    1
    
    rclone config
    
  • The output is as follows:

    1
    2
    3
    4
    5
    
    No remotes found - make a new one
    n) New remote
    s) Set configuration password
    q) Quit config
    n/s/q>
    

    Type n to create a new config. The name is up to you; here I entered:

    1
    
    name> ODrive
    
  • Next it asks you to choose the type of cloud drive service to mount:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    
    Option Storage.
    Type of storage to configure.
    Enter a string value. Press Enter for the default ("").
    Choose a number from below, or type in your own value.
    1 / 1Fichier
      \ "fichier"
    2 / Alias for an existing remote
      \ "alias"
    3 / Amazon Drive
      \ "amazon cloud drive"
    4 / Amazon S3 Compliant Storage Providers including AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, SeaweedFS, and Tencent COS
      \ "s3"
    5 / Backblaze B2
      \ "b2"
    6 / Better checksums for other remotes
      \ "hasher"
    7 / Box
      \ "box"
    8 / Cache a remote
      \ "cache"
    9 / Citrix Sharefile
      \ "sharefile"
    10 / Compress a remote
      \ "compress"
    11 / Dropbox
      \ "dropbox"
    12 / Encrypt/Decrypt a remote
      \ "crypt"
    13 / Enterprise File Fabric
      \ "filefabric"
    14 / FTP Connection
      \ "ftp"
    15 / Google Cloud Storage (this is not Google Drive)
      \ "google cloud storage"
    16 / Google Drive
      \ "drive"
    17 / Google Photos
      \ "google photos"
    18 / Hadoop distributed file system
      \ "hdfs"
    19 / Hubic
      \ "hubic"
    20 / In memory object storage system.
      \ "memory"
    21 / Jottacloud
      \ "jottacloud"
    22 / Koofr
      \ "koofr"
    23 / Local Disk
      \ "local"
    24 / Mail.ru Cloud
      \ "mailru"
    25 / Mega
      \ "mega"
    26 / Microsoft Azure Blob Storage
      \ "azureblob"
    27 / Microsoft OneDrive
      \ "onedrive"
    28 / OpenDrive
      \ "opendrive"
    29 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
      \ "swift"
    30 / Pcloud
      \ "pcloud"
    31 / Put.io
      \ "putio"
    32 / QingCloud Object Storage
      \ "qingstor"
    33 / SSH/SFTP Connection
      \ "sftp"
    34 / Sia Decentralized Cloud
      \ "sia"
    35 / Sugarsync
      \ "sugarsync"
    36 / Tardigrade Decentralized Cloud Storage
      \ "tardigrade"
    37 / Transparently chunk/split large files
      \ "chunker"
    38 / Union merges the contents of several upstream fs
      \ "union"
    39 / Uptobox
      \ "uptobox"
    40 / Webdav
      \ "webdav"
    41 / Yandex Disk
      \ "yandex"
    42 / Zoho
      \ "zoho"
    43 / http Connection
      \ "http"
    44 / premiumize.me
      \ "premiumizeme"
    45 / seafile
      \ "seafile"
    Storage> 27
    

    Here OneDrive is number 27, so type 27.

  • Next it will ask you to enter the OAuth client ID, which consists of client_id and client_secret. You can refer to my other article Creating a Microsoft OneDrive API to learn how to obtain them:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    Option client_id.
    OAuth Client Id.
    Leave blank normally.
    Enter a string value. Press Enter for the default ("").
    client_id> 填入ID
    Option client_secret.
    OAuth Client Secret.
    Leave blank normally.
    Enter a string value. Press Enter for the default ("").
    client_secret> 填入密钥
    
  • Next, choose the region for OneDrive. Here I chose Global, i.e. 1.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    
    Option region.
    Choose national cloud region for OneDrive.
    Enter a string value. Press Enter for the default ("global").
    Choose a number from below, or type in your own value.
    1 / Microsoft Cloud Global
      \ "global"
    2 / Microsoft Cloud for US Government
      \ "us"
    3 / Microsoft Cloud Germany
      \ "de"
    4 / Azure and Office 365 operated by 21Vianet in China
      \ "cn"
    region> 1
    
  • This step can just keep the default:

    1
    2
    3
    4
    
    Edit advanced config?
    y) Yes
    n) No (default)
    y/n> 
    
  • Note that this step requires logging in, so choose n:

    1
    2
    3
    4
    5
    6
    7
    
    Use auto config?
    * Say Y if not sure
    * Say N if you are working on a remote or headless machine
    
    y) Yes (default)
    n) No
    y/n> n
    

    The output echoes as follows:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    Option config_token.
    For this to work, you will need rclone available on a machine that has
    a web browser available.
    For more help and alternate methods see: https://rclone.org/remote_setup/
    Execute the following on the machine with the web browser (same rclone
    version recommended):
            rclone authorize "onedrive" "API参数" # 此处为之后需要执行的指令
    Then paste the result.
    Enter a string value. Press Enter for the default ("").
    config_token> 
    

    The authorization code needs to be obtained on a local computer. Download the Rclone build for your system version from Rclone downloads. After downloading, extract it and enter that directory, hold Shift and right-click the mouse, then select Open PowerShell window here. In the pop-up window, paste the command given by the program above; its format is roughly:

    1
    
    .\rclone.exe authorize "onedrive" "API参数"
    

    The browser will then open automatically. If you are already logged into a Microsoft account, the Request a permission window will pop up directly — if the account is not the one you intended, it is recommended to log out first, then follow the steps below. If you are not logged in, a login page will appear, and after logging in the Request a permission window will pop up. Choose Accept. The page will then say it is safe to close, while the command line outputs the authorization code; copy it and paste it back on the server.

    But if you are using the VSCode terminal, you can directly choose the default Yes for this step, and the following content will be output:

    1
    2
    3
    4
    
    2022/03/19 00:08:19 NOTICE: Make sure your Redirect URL is set to "http://localhost:53682/" in your custom config.
    2022/03/19 00:08:19 NOTICE: If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth?state=为避免泄露已打码
    2022/03/19 00:08:19 NOTICE: Log in and authorize rclone for access
    2022/03/19 00:08:19 NOTICE: Waiting for code...
    

    VSCode forwards this port automatically. Then hold down CTRL and click the link on the 2nd line to open it automatically, or copy it and open it in the browser on your local computer. The Request a permission window will pop up in the browser on your local computer, and the remaining steps are the same as above.

    When done, the following content is output:

    1
    
    2022/03/19 00:08:31 NOTICE: Got code
    
  • Next, choose the type of OneDrive. Pick based on your own situation; since I use a business OneDrive, I chose 1.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    Option config_type.
    Type of connection
    Enter a string value. Press Enter for the default ("onedrive").
    Choose a number from below, or type in an existing value.
    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> config_type> 1
    

    It will then look up the corresponding account based on your input; after confirming it is correct, press Enter to accept the default:

    1
    2
    3
    4
    5
    6
    7
    8
    
    Drive OK?
    
    Found drive "root" of type "business"
    URL: https://呷脯呷脯.喵喵喵
    
    y) Yes (default)
    n) No
    y/n> 
    
  • At this point the configuration is finished. All the cloud drive config information will be output for confirmation; after confirming it is correct, press Enter to accept the default, then type q to quit. Then use this command to check whether the configuration succeeded; if successful, it will list the directories under the root of the cloud drive:

    1
    
    rclone lsd <配置的网盘名称>:/
    

3. Mount the OneDrive Cloud Drive

Most Chinese and foreign search results only give a brief OneDrive mount command template, and since I do not plan to mount it myself either, I just copied a template. However, if you need to configure the mount parameters, you can refer to my other blog post Linux: Mount Google Drive Cloud Drive with Rclone, as well as the relevant documentation on the official Rclone website.

1
rclone mount <配置的云盘名称>:<要挂载的云盘目录> <作为挂载点的本地目录> --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 --daemon

If it fails to mount, try the following command:

1
apt install fuse

After mounting, test with the following command; if the cloud drive directory appears, it succeeded:

1
df -h

To unmount, use the following command:

1
fusermount -qzu <挂载路径>

References