Linux使用Rclone挂载OneDrive网盘

相信不止是我,很多人为了省钱在选服务器时选择的是基础配置,存储容量只有可怜的几十GB甚至只有10GB。但是有时候又确实需要一个较大的存储空间,这时候提供了API的OneDrive、Google Drive等网盘,可以通过挂载到服务器作为本地存储使用,从而一定程度上帮助我们缓解容量焦虑。


步骤

1. 下载安装

本文默认使用的Linux系统,但是操作其实都差不多,其他系统也可以借鉴。

运行以下命令,下载并安装Rclone

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

2. 配置Rclone

  • 安装完成后输入:

    1
    
    rclone config
    
  • 输出如下:

    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>
    

    输入n新建配置,名字随意,此处我输入的是:

    1
    
    name> ODrive
    
  • 然后会要求选择挂载的网盘服务类型:

     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
    

    此处OneDrive序号为27,故输入27

  • 然后会要求输入OAuth client ID,包含client_idclient_secret,可前往我的另一篇文章《创建微软OneDrive API》了解获取方法:

     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> 填入密钥
    
  • 然后要选择OneDrive的地区,这里选择全球,即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
    
  • 此步默认即可:

    1
    2
    3
    4
    
    Edit advanced config?
    y) Yes
    n) No (default)
    y/n> 
    
  • 注意此步需要登录,故选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
    

    回显如下:

     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> 
    

    授权码需要在本地电脑获取,根据电脑系统版本在此Rclone downloads下载对应的Rclone。下载后解压并进入该目录,按住shift点击鼠标右键,选择在此处打开 Powershell 窗口。在弹出的窗口中黏贴上面程序给出的命令,格式大致如下:

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

    然后浏览器会自动打开,已登录微软账户会直接弹出请求征得的许可窗口,如果不是你所选的账户建议事先退出登录,然后按后面的操作进行;未登录时会弹出登录页面,登录后弹出请求征得的许可窗口。选择接受即可。然后网页会提示可以关闭,同时命令行中会输出授权码,复制后回到服务器进行黏贴。

    但如果使用的是VSCode的终端,此步操作可直接选默认的Yes,然后会输出如下内容:

    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会自动对这个端口进行转发,然后按住CTRL点击第2行的链接自动打开,或者复制到本地电脑的浏览器打开,就会在本地电脑的浏览器弹出请求征得的许可窗口,之后的步骤同上。

    完成后会输出以下内容:

    1
    
    2022/03/19 00:08:31 NOTICE: Got code
    
  • 接着要选择OneDrive的类型,根据自己的具体情况进行选择,此处我用的是企业OneDrive,故选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
    

    然后会根据你的输入查找对应账户,确认无误后按回车默认即可:

    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> 
    
  • 至此配置结束,会输出所有的网盘配置信息进行确认,确认无误后按回车默认同意,接着输入q退出。然后使用此命令检测是否配置成功,若成功会输出网盘根目录下的目录信息:

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

3. 挂载OneDrive网盘

全网中外文搜索结果大多只给出了简短的OneDrive挂载指令模板,另外我也并不打算挂载,所以照抄一个模板过来。不过需要配置挂载参数的可以参考我的另一篇博客《Linux使用Rclone挂载Google Drive网盘》,以及Rclone官网的相关文档。

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

如果无法挂载,请使用如下指令再试:

1
apt install fuse

挂载后使用以下命令测试,看到网盘目录即为成功:

1
df -h

如果需要取消挂载,使用如下命令:

1
fusermount -qzu <挂载路径>

引用