CentOS 通过 rclone 挂载 OneDrive

写在前面

挂载 OneDrive 需要浏览器登录授权,而 CentOS 一般作为服务器使用,是没有操作界面的,因此我们需要先在 Windows 或其他桌面操作系统下使用 rclone 获取授权密钥,然后再在 CentOS 上完成挂载。

获取授权密钥

rclone 官网下载对应的版本软件

下载后解压,并用 CDM 运行

1
rclone authorize "onedrive"

在自动打开的浏览器中登录授权,授权成功后返回 CMD 窗口,会看到以下界面:

授权成功界面

复制授权密钥保存备用

1
2
3
4
5
Paste the following into your remote machine --->

复制这里面的所有内容

<---End paste

CentOS 挂载 OneDrive

软件安装

安装rclone

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

安装fuse

1
yum install fuse

rclone配置

启动配置

1
rclone config

创建一个新配置

1
2
3
4
5
6
7
8
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> n # 输入 n 创建一个新的配

自定义配置名称

1
name> OneDrive # 这里输入想要的名字

选择需挂载网盘类型

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
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 Provider (AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, etc)
\ "s3"
5 / Backblaze B2
\ "b2"
6 / Box
\ "box"
7 / Cache a remote
\ "cache"
8 / Citrix Sharefile
\ "sharefile"
9 / Dropbox
\ "dropbox"
10 / Encrypt/Decrypt a remote
\ "crypt"
11 / FTP Connection
\ "ftp"
12 / Google Cloud Storage (this is not Google Drive)
\ "google cloud storage"
13 / Google Drive
\ "drive"
14 / Google Photos
\ "google photos"
15 / Hubic
\ "hubic"
16 / In memory object storage system.
\ "memory"
17 / JottaCloud
\ "jottacloud"
18 / Koofr
\ "koofr"
19 / Local Disk
\ "local"
20 / Mail.ru Cloud
\ "mailru"
21 / Mega
\ "mega"
22 / Microsoft Azure Blob Storage
\ "azureblob"
23 / Microsoft OneDrive
\ "onedrive"
24 / OpenDrive
\ "opendrive"
25 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
\ "swift"
26 / Pcloud
\ "pcloud"
27 / Put.io
\ "putio"
28 / QingCloud Object Storage
\ "qingstor"
29 / SSH/SFTP Connection
\ "sftp"
30 / Sugarsync
\ "sugarsync"
31 / Transparently chunk/split large files
\ "chunker"
32 / Union merges the contents of several remotes
\ "union"
33 / Webdav
\ "webdav"
34 / Yandex Disk
\ "yandex"
35 / http Connection
\ "http"
36 / premiumize.me
\ "premiumizeme"
Storage> 23 #这里OneDrive是23,不同的rclone版本数字可能不同

client_id 和 client_scret 直接留空

1
2
3
4
Microsoft App Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id> # 无需输入,直接回车
1
2
3
4
Microsoft App Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> # 无需输入,直接回车

不使用高级配置

1
2
3
4
Edit advanced config? (y/n)
y) Yes
n) No (default)
y/n> n # 不使用,输入n

不使用远程配置

1
2
3
4
5
6
7
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 # 不使用,输入n

输入授权密钥

1
2
3
4
5
For this to work, you will need rclone available on a machine that has a web browser available.
Execute the following on your machine (same rclone version recommended) :
rclone authorize "onedrive"
Then paste the result below:
result> # 这里粘入前面获取的授权密钥

选择类型为 onedrive

1
2
3
4
5
6
7
8
9
10
11
12
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 # 选择onedrive

选择驱动

1
2
3
Found 1 drives, please select the one you want to use:
0: (personal) id=xxxxxxxxxxxx
Chose drive to use:> 0 # 这里只有个人版,输入0

确认并退出配置

1
2
3
4
5
Found drive 'root' of type 'personal', URL: https://onedrive.live.com/?cid=xxxxxxxxxxxx
Is that okay?
y) Yes (default)
n) No
y/n> y # 确认选择
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
--------------------
[onedrive-chuqidecha]
type = onedrive
token = {"access_token":"xxxxxxxxxxxxxxxx"}
drive_id = xxxxxxxxxxxxx
drive_type = personal
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y # 如果配置没问题,继续确认
Current remotes:

Name Type
==== ====
onedrive-yuanzheng 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 # 退出配置

挂载

创建挂载目录

1
mkdir -p /本地目录

后台挂载运行

1
2
3
rclone mount 驱动名字:网盘目录 /本地目录 &

#驱动名字为前面配置填写的`name`

检查是否挂载成功

1
df -h

卸载已挂载磁盘

1
fusermount -qzu /本地目录

进阶教程

设置开机自启

添加到 systemd 中,使用 systemctl 管理应用程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#以下命令如上一步中的挂载命令
command="mount DriveName:Folder LocalFolder --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

查看 rclone 状态

1
systemctl status rclone

设置 rclone 开机自启

1
systemctl enable rclone

启动 rclone

1
systemctl start rclone
重启 rclone
1
systemctl restart rclone

停止 rclone

1
systemctl stop rclone