0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

UbuntuでGoogle Driveをrcloneでマウントする手順【自前OAuth Client ID対応】

0
Posted at

Linux環境からGoogle Driveを通常のディレクトリのように扱いたかったので、rclone + FUSEを使ってUbuntuへマウントしました。

今回はrcloneの共通Client IDではなく、Google Cloudで自分専用のOAuth Client IDを作成して接続します。

最終的な構成は以下です。

Google Drive
    ↓
  rclone
    ↓
   FUSE
    ↓
~/GoogleDrive

環境

項目 内容
OS Ubuntu / Linux
接続ツール rclone
接続先 Google Drive
マウント FUSE
認証 Google OAuth 2.0

1. rclone / FUSEをインストール

Ubuntuの場合は以下を実行します。

sudo apt update
sudo apt install -y rclone fuse3

インストール確認。

rclone version

2. Google Drive APIを有効化

Google Cloud Consoleでプロジェクトを作成、または既存プロジェクトを選択します。

以下の順に進みます。

Google Cloud Console
  ↓
APIとサービス
  ↓
APIライブラリ
  ↓
Google Drive API
  ↓
有効にする

3. Google Auth Platformを設定

Google Cloud Consoleの

Google Auth Platform

を開きます。

ブランディング情報として、最低限以下を設定します。

アプリ名
ユーザーサポートメール
デベロッパーの連絡先メール

アプリ名は任意ですが、今回は以下のような名前にしました。

rclone

または

rclone-linux

4. Google Drive用スコープを登録

Google Auth Platformから、

データアクセス
  ↓
スコープを追加または削除

を開きます。

「スコープの手動追加」に以下を登録します。

https://www.googleapis.com/auth/docs
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.metadata.readonly

まとめて入力する場合は以下です。

https://www.googleapis.com/auth/docs,https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/drive.metadata.readonly

追加後、

テーブルに追加
  ↓
更新
  ↓
Save

と進みます。


5. OAuthクライアントを作成

Google Auth Platformから、

クライアント
  ↓
クライアントを作成

を選択します。

アプリケーションの種類は、

デスクトップ アプリ

を選択します。

名前は任意です。

例:

rclone-linux

作成すると以下が発行されます。

Client ID
Client Secret

この2つを控えておきます。

注意

Client Secret はGitHubやQiitaなどに公開しないようにします。


6. rcloneの設定を開始

ターミナルから以下を実行します。

rclone config

新しいRemoteを作成します。

n) New remote
q) Quit config

n/r/c/s/q> n

Remote名を設定します。

name> gdrive

7. Google Driveを選択

StorageとしてGoogle Driveを選択します。

Storage> drive

8. Client IDを入力

Google Cloud Consoleで作成したClient IDを入力します。

client_id> xxxxxxxxxxxxx.apps.googleusercontent.com

続いてClient Secretを入力します。

client_secret> ********

9. アクセス権を選択

以下のような選択肢が表示されます。

Option scope.
Scope that rclone should use when requesting access from drive.

1 / Full access all files
2 / Read-only access
3 / Access to files created by rclone only
4 / Application Data folder
5 / Metadata only

scope>

今回はGoogle Driveを通常のディレクトリとして読み書きしたいため、

scope> 1

を選択しました。


10. Service Account

以下が表示されます。

Option service_account_file.
Service Account Credentials JSON file path.
Leave blank normally.

service_account_file>

通常のGoogleアカウントでログインするため、何も入力せずEnterを押します。

service_account_file>

11. Advanced Config

Edit advanced config?

y) Yes
n) No (default)

y/n>

今回は特別な設定は行わないため、

y/n> n

を選択します。


12. Googleアカウントで認証

GUIのあるUbuntu端末なので、

Use auto config?

y) Yes
n) No

y/n>

は、

y/n> y

を選択します。

ブラウザが起動し、Googleアカウントの認証画面が表示されます。

Google Driveを利用するアカウントを選択し、アクセスを許可します。

成功するとターミナル側に、

NOTICE: Got code

と表示されます。


13. Shared Driveの設定

続いて、

Configure this as a Shared Drive (Team Drive)?

y) Yes
n) No (default)

y/n>

と表示されます。

通常の「マイドライブ」を利用する場合は、

y/n> n

を選択します。

Google Workspaceの共有ドライブを利用する場合のみ y を選択します。


14. Remoteを保存

最後に、

Keep this "gdrive" remote?

y) Yes this is OK
e) Edit this remote
d) Delete this remote

y/e/d>

と表示されるので、

y/e/d> y

を入力します。

以下のように表示されれば設定完了です。

Current remotes:

Name                 Type
====                 ====
gdrive               drive

q で終了します。

e/n/d/r/c/s/q> q

15. Google Driveへの接続確認

まずはマウントせず、Google Driveのフォルダ一覧を取得します。

rclone lsd gdrive:

以下のようにGoogle Drive上のフォルダが表示されれば成功です。

-1 2026-01-20 19:02:46 -1 Chrome から保存
-1 2013-09-07 03:37:32 -1 Google Buzz
-1 2018-07-25 23:16:44 -1 Screencastify

Google DriveをUbuntuにマウントする

16. マウントポイントを作成

ホームディレクトリ配下にGoogle Drive用ディレクトリを作成します。

mkdir -p ~/GoogleDrive

17. Google Driveをマウント

以下を実行します。

rclone mount gdrive: ~/GoogleDrive \
  --vfs-cache-mode full \
  --daemon

マウント後、確認します。

ls -la ~/GoogleDrive

Google Drive上のファイル・フォルダが表示されれば成功です。


18. マウント状態を確認

mountpoint ~/GoogleDrive

正常にマウントされていれば、

/home/<USER>/GoogleDrive is a mountpoint

のように表示されます。

これで、

cd ~/GoogleDrive
ls

のようにGoogle DriveをLinux上の通常のディレクトリに近い形で利用できます。

例えばローカルファイルをGoogle Driveへコピーできます。

cp README.md ~/GoogleDrive/

19. アンマウント

Google Driveをアンマウントする場合は以下です。

fusermount3 -u ~/GoogleDrive

systemdでログイン時に自動マウントする

毎回rclone mountを実行するのは面倒なので、systemdのユーザーサービスとして登録します。

20. 必要なディレクトリを作成

mkdir -p ~/.cache/rclone
mkdir -p ~/.config/systemd/user

rcloneの設定ファイルの場所を確認します。

rclone config file

通常は以下です。

~/.config/rclone/rclone.conf

実行ファイルの場所も確認しておきます。

which rclone
which fusermount3

21. systemdサービスを作成

nano ~/.config/systemd/user/rclone-gdrive.service

以下を記述します。

[Unit]
Description=Rclone Google Drive Mount

[Service]
Type=simple

ExecStart=/usr/bin/rclone mount gdrive: %h/GoogleDrive \
    --config=%h/.config/rclone/rclone.conf \
    --vfs-cache-mode=full \
    --cache-dir=%h/.cache/rclone \
    --dir-cache-time=1h \
    --poll-interval=15s \
    --umask=022

ExecStop=/usr/bin/fusermount3 -u %h/GoogleDrive

Restart=on-failure
RestartSec=10

[Install]
WantedBy=default.target

保存します。

nanoの場合:

Ctrl + O
Enter
Ctrl + X

22. systemdへ設定を反映

systemctl --user daemon-reload

サービスを有効化し、そのまま起動します。

systemctl --user enable --now rclone-gdrive.service

23. サービス状態を確認

systemctl --user status rclone-gdrive.service

以下のようになっていればOKです。

Active: active (running)

Google Driveも確認します。

mountpoint ~/GoogleDrive
ls -la ~/GoogleDrive

PCを再起動またはログアウト・ログインしたあとも自動マウントされれば完成です。


systemd運用コマンド

状態確認

systemctl --user status rclone-gdrive

再起動

systemctl --user restart rclone-gdrive

停止

systemctl --user stop rclone-gdrive

ログ確認

journalctl --user -u rclone-gdrive -n 100

リアルタイムにログを見る場合:

journalctl --user -u rclone-gdrive -f

セキュリティ上の注意

OAuth関連では、以下の情報を公開しないよう注意します。

Client Secret
access_token
refresh_token
rclone.conf

特に、

~/.config/rclone/rclone.conf

にはGoogle Driveへアクセスするための認証情報が保存されています。

ファイル権限を確認します。

ls -l ~/.config/rclone/rclone.conf

必要に応じて、自分だけが読み書きできるようにします。

chmod 600 ~/.config/rclone/rclone.conf

また、rcloneの設定中に表示されたログをQiitaやGitHub、チャットなどへ貼る場合、

"refresh_token":"..."

などが含まれていないか必ず確認します。

もし誤って公開してしまった場合は、Googleアカウント側で当該アプリのアクセス権を取り消し、再認証した方が安全です。


まとめ

今回の構成は以下になりました。

Google Drive
     │
     │ Google Drive API / OAuth 2.0
     ▼
   rclone
     │
     │ FUSE
     ▼
~/GoogleDrive
     │
     ├─ Shell
     ├─ Python
     ├─ VS Code
     └─ その他Linuxアプリ

一度マウントしてしまえば、

~/GoogleDrive

を通常のLinuxディレクトリに近い感覚で扱えます。

さらにsystemdのユーザーサービスに登録することで、Ubuntuへのログイン時にGoogle Driveを自動マウントできます。

Linux上のスクリプトや開発ツールからGoogle Drive上のファイルを扱いたい場合、rclone mountは便利な選択肢だと思います。

参考


Qiitaタグ候補

Ubuntu
Linux
GoogleDrive
rclone
OAuth
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?