Linux上使用Rclone挂载OneDrive或GoogleDrive并设置开机自启

Linux上使用Rclone挂载OneDrive或GoogleDrive并设置开机自启

1. 获取Token

在Windows电脑上下载Rclone,下载地址:rclone.org/downloads/ ,然后解压,使用cmd进入解压后的文件夹:

rclone.exe authorize "onedrive"



整个大括号就是token,包括大括号。

2. 在Linux上安装Rclone

直接使用官方提供的一键脚本:

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



3. 配置

安装完成后执行:

rclone config

 rclone config
2021/03/03 15:00:17 NOTICE: Config file "/root/.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
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, and Tencent COS
   \ "s3"
 5 / Backblaze B2
   \ "b2"
 6 / Box
   \ "box"
 7 / Cache a remote
   \ "cache"
 8 / Citrix Sharefile
   \ "sharefile"
 9 / Compress a remote
   \ "compress"
10 / Dropbox
   \ "dropbox"
11 / Encrypt/Decrypt a remote
   \ "crypt"
12 / Enterprise File Fabric
   \ "filefabric"
13 / FTP Connection
   \ "ftp"
14 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
15 / Google Drive
   \ "drive"
16 / Google Photos
   \ "google photos"
17 / Hadoop distributed file system
   \ "hdfs"
18 / Hubic
   \ "hubic"
19 / In memory object storage system.
   \ "memory"
20 / Jottacloud
   \ "jottacloud"
21 / Koofr
   \ "koofr"
22 / Local Disk
   \ "local"
23 / Mail.ru Cloud
   \ "mailru"
24 / Mega
   \ "mega"
25 / Microsoft Azure Blob Storage
   \ "azureblob"
26 / Microsoft OneDrive
   \ "onedrive"
27 / OpenDrive
   \ "opendrive"
28 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
29 / Pcloud
   \ "pcloud"
30 / Put.io
   \ "putio"
31 / QingCloud Object Storage
   \ "qingstor"
32 / SSH/SFTP Connection
   \ "sftp"
33 / Sugarsync
   \ "sugarsync"
34 / Tardigrade Decentralized Cloud Storage
   \ "tardigrade"
35 / Transparently chunk/split large files
   \ "chunker"
36 / Union merges the contents of several upstream fs
   \ "union"
37 / Webdav
   \ "webdav"
38 / Yandex Disk
   \ "yandex"
39 / Zoho
   \ "zoho"
40 / http Connection
   \ "http"
41 / premiumize.me
   \ "premiumizeme"
42 / seafile
   \ "seafile"
Storage> 26
** See help for onedrive backend at: https://rclone.org/onedrive/ **

OAuth Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id> 
OAuth Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> 
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 # 按照你的实际情况选择
Edit advanced config? (y/n)
y) Yes
n) No (default)
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 (default)
n) No
y/n> n
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"

Then paste the result below:
result> {"access_token":qqqqqqqqqq","expiry":"2021-03-03T15:54:34.7442912+08:00"} # 这里就是上面获得的token
Choose a number from below, or type in an existing value
 1 / OneDrive Personal or Business
   \ "onedrive"
 2 / Root Sharepoint site
   \ "sharepoint"
 3 / Sharepoint site name or URL (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"
 7 / Sharepoint server-relative path (advanced, e.g. /teams/hr)
   \ "path"
Your choice> 1
Found 1 drives, please select the one you want to use:
0: OneDrive (business) id=b!B6yHyawk7kq599Kb4wYXQFe1jafkFoFBqrwS7ZFYLJsUbhule-g-Rax7IpAhk6c8
Chose drive to use:> 0
Found drive 'root' of type 'business', URL: https://imissyou5201314-my.sharepoint.com/personal/codehero_imissyou5201314_onmicrosoft_com/Documents
Is that okay?
y) Yes (default)
n) No
y/n> y
--------------------
[onedrive]
type = onedrive
region = global
token = {"access_token":"eyJ0eXAiOiJKV1QiLCJub25jZSI6InVJck1QemlSb1plblFKVTdwNnRfbGlhQWNCRTdVVVp5bVZEbkIzOU5xOUUiLCJhbGciOiJSUzI1NiIsIng1dCI6Im5PbzNaRHJPRFhFSzFqS1doWHNsSFJfS1h
FZyIsImtpZCI6Im5PbzNaRHJPRFhFSzFqS1doWHNsSFJfS1hFZyJ9.eyJhdWQiOiIwMDAwMDAwMy0wMDAwLTAwMDAtYzAwMC0wMDAwMDAwMDAwMDAiLCJpc3MiwwwwwwwOp70KH5hAG6I4V8XwtfSx2PntC1OjT0pxgH7gffmzD7y32CSOCQfBRjLNGHIUAGBnuB6DU9rmDSi_l6o7uaOE7M_SCqm_PnG_d0AHxfmqMUjBBJ6tHyjCZnrzteGdfIiPmAnAPbD9Dz9_BDAcDnOSFP7EvqtnvlO_qSXHBc4tyu2Z38_ZfAtmweQWpsje72XOupniZTd-fzqWHlA","token_type":"Bearer","refresh_token":"0.AAAA2z4XoCKiQ0KZVYSJROV5fdllVrGm7ZJAhTkO7Ddq_VlTAKw.AgABAAAAAAD--DLA3VO7QrddgJg7WevrAgDs_wQA9P_9M5dddRYJB40BWPAWV55_fqiuGzdgnnwz2_WgNGA36fnDpDndjC2LOmFSoeLFokluSW9CpK9bf3NhOVgg4I04Sex-O2bSfUr74YzUssTNQqL2bUzCbxlZMNHa5OEdJwZuOXPfv6IdCdk_E1jWMmjxHNIi7NLZ6GWqaOUARsqq_5JLTDEpo6F5VS5vbTOOVuDP3MhAcvL9aSXBk7BKxXu6yMoZp7uaynmCESg4ADFEFSSefwefwefsefwfgsalkfhsehjfg2bewo_EASPdtlIkjW0v5lCliU1cXxuPGZ_Lq1eOYyjPLMvAJ9pMiKg2-Iw1-844H67E0V9FCY8cBzH3qSui8rgQwt9nBI2PI0uaJ6KlVpQb0IiTQGapw2hgubwprnSVM39B5Z58WUon2ObcJ76CP7VPMvxIFIVnP5oq2gi1PRTxPgN3z64z3Q1hbwM59HqOrwX84uWIvCk63_i6ocVb0nasiY7EaLoF9fJHAnAw2qC3dOng","expiry":"2021-03-03T15:54:34.7442912+08:00"}drive_id = b!B6yHyawk7kq599Kb4wYXQFe1jafkFoFBqrwS7ZFYLJsUbhule-g-Rax7IpAhk6c8
drive_type = business
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y
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> q
e/n/d/r/c/s/q>

4. 安装fuse

apt install fuse

5. 挂载OneDrive

  1. 新建挂载目录
    ```bash mkdir /usr/local/onedrive
    根据个人喜好选位置
    ```
  2. 挂载
    ```bash
    挂载为磁盘,下面的DriveName、Folder、LocalFolder参数根据说明自行替换
    --daemon为守护进程参数
    rclone mount DriveName:Folder LocalFolder --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000
    我的命令为
    rclone mount onedrive:/ /usr/local/onedrive --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000
    ```
  • DriveName: 执行rclone config


    • Folder: 在OneDrive的文件夹
    • LocalFolder: linux本地的文件夹 ,即为第一步创建的目录


  1. 查看是否成功
    使用df -h命令查看是否成功


6. 开机自启

适用于使用systemctl命令的linux

#将后面修改成你上面手动运行命令中,除了rclone的全部参数
command=" mount onedrive:/ /usr/local/onedrive  --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000"
#以下是一整条命令,一起复制到SSH客户端运行
cat > /etc/systemd/system/rclone.service <<EOF
[Unit]
Description=Rclone
After=network-online.target

[Service]
Type=simple
ExecStart=$(command -v rclone) ${command}
Restart=on-abort
User=root

[Install]
WantedBy=default.target
EOF

现在就可以使用systemctl来挂载OneDrive了

# 开机自启
systemctl enable rclone
# 启动
systemctl start rclone
# 重启
systemctl restart  rclone
# 停止
systemctl status rclone



7. 复制和同步命令

#本地上传到网盘 
r clone [指令名称] [本地路径] [网盘名称:网盘路径] [flags] [flags]
# 网盘下载到本地
rclone [指令名称] [网盘名称:网盘路径] [本地路径] [flags]
# 网盘上传/下载到网盘
rclone [指令名称] [网盘名称:网盘路径] [网盘名称:网盘路径] [flags] [flags]
# 显示速度 -v
# 指令copy sync

常用指令

  • rclone copy – 复制
  • rclone sync – 同步 将目录内容同步到网盘目录
  • rclone move – 移动 将目录内文件移动到网盘,默认删除路径文件
  • rclone purge – 删除路径及所有文件
  • rclone delete – 删除路径所有文件但不删除路径
  • rclone mkdir – 创建目录
  • rclone rmdir – 删除目录
  • rclone check – 检查目标文件与源文件是否匹配
  • rclone ls – 列出指定路径下的所有的文件以及文件大小
  • rclone lsl – 仅列出对象的修改时间,大小和路径
  • rclone lsd – 仅列出目录
  • rcloen lsf – 以易于解析的格式列出对象和目录
  • rcloen lsjson – 以JSON格式列出对象和目录
  • rclone lsd– 列出路径下的所有目录
  • rclone size – 打印目录中对象的总大小和数量
  • rclone mount – 将远程文件路径挂载到本地路径
编辑于 2021-11-26 15:55